everyone I'm designing a vehicle security system, basically I can already read amd i control all the components (GSM, GPS, Magnetic Sensor, Giroscop, buzzer...) connected to the microcontroller.
But then the functions are basically three main ones (at void loop) that I need to be working in "simultaneous", any idea of how I can do that?
Below the code, half long but that's all I need...
there are some comments in portuguese. i apologize for that, but the important thing is in the code thanks in advance
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
SoftwareSerial sgsm(3,2);
String number = "25884xxxxxxx" ;
unsigned char Buff[250];
unsigned char BuffIndex;
#include<Wire.h>
int sensorMag = 5; //SENSOR MAGNETICO
TinyGPSPlus gps;
double latitude, longitude;
String response;
int lastStringLength = response.length();
String link;
const int MPU6050_addr=0x68;
int16_t AccX,AccY,AccZ,Temp,GyroX,GyroY,GyroZ;
#include <IRremote.h>
int RECV_PIN = 4; //REMOTE
IRrecv irrecv(RECV_PIN);
decode_results results;
int Sirene = 6; //BUZZER
int Motor = 7; //MOTOR
/////===================================================================== Inicializacao do sensores =============================================================================================================================
void setup()
{
Serial.begin(9600);
sgsm.begin(9600);
delay (1000);
Wire.begin();
Wire.beginTransmission(MPU6050_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
delay (1000);
pinMode(sensorMag, INPUT); //SENSOR MAGNETICO
pinMode(Sirene, OUTPUT); //Sirene
pinMode(Motor, OUTPUT); //MOTOR
irrecv.enableIRIn();
Serial.println("SISTEMA INICIALIZADO");
}
/////===================================================================== Inicio do Loop =============================================================================================================================
void loop()
{
//GPS();
remote ();
ReceberComando();
}
/////===================================================================== Remote controle =============================================================================================================================
void remote (){
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
Serial.println(" ");
if (results.value== 0xFF6897)
{
//while (results.value==0xFF5AA5){
Serial.println("SISTEMA INACTIVO");
digitalWrite(Motor, LOW);
Serial.println(" ");
//Acelerometro();
delay(500);
}
else
{
while (results.value==0xFF10EF)
{
Serial.println("Sistema Armado");
digitalWrite(Motor, HIGH);
delay(1000);
Serial.println(" ");
SensorMagnetico();
delay(1000);
Acelerometro();
delay(1000);
}
}
}
irrecv.resume(); // Receive the next value
delay(1000);
}
void SensorMagnetico()
{
int EstadosensorMag = digitalRead(sensorMag);
if (EstadosensorMag = digitalRead(sensorMag)== HIGH){
Serial.println("A porta N. 1 da sua viatura foi aberta!");
digitalWrite(Sirene, HIGH);
delay(1000);
mensagem2();
delay(1000);
enviolink();
delay (1000);
}
else
Serial.println("Portas Fechadas");
digitalWrite(Sirene, LOW);
delay(100);
//digitalWrite(Motor, HIGH);
}
void Acelerometro(){
Wire.beginTransmission(MPU6050_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_addr,14,true);
AccX=Wire.read()<<8|Wire.read();
AccZ=Wire.read()<<8|Wire.read();
Serial.print("AccX = ");
Serial.print(AccX);
Serial.print(" || AccZ = ");
Serial.println(AccZ);
delay(1000);
if (AccZ > 2000){
digitalWrite(Sirene, HIGH);
delay(2000);
}
else
digitalWrite(Sirene, LOW);
}
/////==================== coordenadas/ GPS=============================
void GPS(){
if(Serial.available()) {
gps.encode(Serial.read());
}
if(gps.location.isUpdated()) {
Serial.print("latitude:");
latitude = gps.location.lat();
Serial.print("longitude:");
longitude = gps.location.lng();
link = "www.google.com/maps/place/" + String(latitude, 6) + "," + String(longitude, 6) ;
Serial.println(link);
}
}
/////========= Receber Comandos de texto + localizacao ======================
void ReceberComando()
{
Serial.begin(9600);
sgsm.begin(9600);
Serial.print("Inicializado o GSM e apagando SMS");
clear_SMS_Memory();
delay(3000);
sgsm.println("AT+CMGF=1");
delay(3000);
sgsm.println("AT+CNMI=2,2,0,0,0");
memset(Buff, '\0', 250);// Initialize the string BuffIndex=5; }
delay (9000);
pinMode(Motor, OUTPUT); // controle do motor
while(1)
{
if(sgsm.available()>0)
{
Buff[BuffIndex] = sgsm.read();
if( (Buff[BuffIndex-2] == 'D') && (Buff[BuffIndex-1] == 'M') && (Buff[BuffIndex] == '1'))// Desativar Motor Agora
{
digitalWrite(Motor, HIGH);
send2("Motor da sua viatura desligado! aguarde pelas coordenadas...");
Serial.println ("Set sgsm Number");
delay(10000);
GPS();
delay(10000);
enviolink();
Serial.println(" ");
}
if( (Buff[BuffIndex-2] == 'A') && (Buff[BuffIndex-1] == 'R') && (Buff[BuffIndex] == 'M'))//Ativar Motor Agora
{
digitalWrite(Motor, LOW);
digitalWrite (Sirene, LOW);
send2("Motor Ativo....A viatura esta segura!, Aguarde a localizacao!");
Serial.println ("Set sgsm Number");
delay(1000);
GPS();
delay(1000);
enviolink();
Serial.println(" ");
}
if( (Buff[BuffIndex-2] == 'L') && (Buff[BuffIndex-1] == 'O') && (Buff[BuffIndex] == 'C'))//Retorna coordenadas
{
send2("Buscado a localizacao actual!");
GPS();
delay(10000);
enviolink();
Serial.println(" ");
delay(10000);
}
/// Pode se acrescentar mais condicoes
BuffIndex++;
if(BuffIndex>250)
{
BuffIndex=5;
}
}
}
}
/////=============== Mensagens de Texto =======
void send2(String SMS)
{
Serial.println ("Sending Message");
sgsm.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set sgsm Number");
sgsm.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
delay(1000);
sgsm.println(SMS);
delay(100);
sgsm.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void mensagem()// mensagens das variaveis; e inicializacao do carro
{
Serial.println ("Sending Message");
sgsm.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set sgsm Number");
sgsm.println("AT+CMGS=\""+ number + "\"\r"); //Mobile phone number to send message
delay(1000);
String SMS = "Sistema de Alarme habilitado"; //sgsm content
sgsm.println(SMS);
delay(100);
sgsm.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void mensagem2()// mensagens das variaveis; e inicializacao do carro
{
Serial.println ("Sending Message");
sgsm.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set sgsm Number");
sgsm.println("AT+CMGS=\""+ number + "\"\r"); //Mobile phone number to send message
delay(1000);
String SMS = "Alerta de intrução! Verifique a sua viatura."; //sgsm content
sgsm.println(SMS);
delay(100);
sgsm.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
////========================= Apaga mensagens no cartao ===========
void clear_SMS_Memory()
{
unsigned short i = 0;
for (i = 0; i <= 30; i++)
{
sgsm.print("AT+CMGD=");
sgsm.println(i);
delay(100);
}
while (sgsm.available() > 0) sgsm.read();
}
/////===================== Leitura de Inputs do GSM ==========
void updateSerial()
{
delay(500);
while (Serial.available())
{
sgsm.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(sgsm.available())
{
Serial.write(sgsm.read());//Forward what Software Serial received to Serial Port
}
}
/////======= localizacao ========================================
void enviolink()
{
sgsm.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
sgsm.println("AT+CMGS=\"+25884xxxxxxx\"\r"); // Replace x with mobile number
delay(1000);
sgsm.println(link);// The SMS text you want to send
delay(100);
sgsm.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}