Hello gents,
I am trying to make a project with ARDUINO and SIM800l.
The whole system consist of 4 switches (each switch is responsible for ON/OFF external relay connected to Arduino board).
-When I press switch 1 it should turn relay 1 ON for 6 seconds. Relay should turn OFF after 6 seconds or when I press switch again.
-When I press switch 2 it should turn relay 2 ON for 8 seconds. Relay should turn OFF after 8 seconds or when I press switch again.
-When I press switch 3 it should turn relay 3 ON for 6 seconds. Relay should turn OFF after 6 seconds or when I press switch again.
-When I press switch 4 it should turn relay 4 ON for 7 seconds. Relay should turn OFF after 7 seconds or when I press switch again.
Additionally I have SIM800l, which should turn ON all relays simultaneously. Relay 1 should turn off after 6 seconds, relay 2 after 8 seconds, relay 3 after 6 seconds and relay 4 after 7 seconds.
The problem is my code. I belive delay() is killing it. When I am trying to switch ON relay by switch due to delay between if(mySerial.available()) brackets I am not able to switch it on.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,12); // (Rx,Tx > Tx,Rx)
char incomingByte;
String inputString;
const int buttonPinSyrena = 0;
const int buttonPinPompa = 1;
const int buttonPinOswietlenie = 2;
const int buttonPinWyciag = 3;
const int ledPinSyrena = 9;
const int ledPinPompa = 10;
const int ledPinOswietlenie = 5;
const int ledPinWyciag = 6;
const unsigned long periodSyrena = 6000;
const unsigned long periodPompa = 8000;
const unsigned long periodOswietlenie = 6000;
const unsigned long periodWyciag = 7000;
int buttonStateSyrena = 0;
int buttonStatePompa = 0;
int buttonStateOswietlenie = 0;
int buttonStateWyciag = 0;
unsigned long startMillisSyrena;
unsigned long currentMillisSyrena;
unsigned long startMillisPompa;
unsigned long currentMillisPompa;
unsigned long startMillisOswietlenie;
unsigned long currentMillisOswietlenie;
unsigned long startMillisWyciag;
unsigned long currentMillisWyciag;
boolean oldSwitchStateSyrena = LOW;
boolean newSwitchStateSyrena1 = LOW;
boolean newSwitchStateSyrena2 = LOW;
boolean newSwitchStateSyrena3 = LOW;
boolean LEDstatusSyrena = LOW;
boolean oldSwitchStatePompa = LOW;
boolean newSwitchStatePompa1 = LOW;
boolean newSwitchStatePompa2 = LOW;
boolean newSwitchStatePompa3 = LOW;
boolean LEDstatusPompa = LOW;
boolean oldSwitchStateOswietlenie = LOW;
boolean newSwitchStateOswietlenie1 = LOW;
boolean newSwitchStateOswietlenie2 = LOW;
boolean newSwitchStateOswietlenie3 = LOW;
boolean LEDstatusOswietlenie = LOW;
boolean oldSwitchStateWyciag = LOW;
boolean newSwitchStateWyciag1 = LOW;
boolean newSwitchStateWyciag2 = LOW;
boolean newSwitchStateWyciag3 = LOW;
boolean LEDstatusWyciag = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPinSyrena, OUTPUT);
pinMode(ledPinPompa, OUTPUT);
pinMode(ledPinOswietlenie, OUTPUT);
pinMode(ledPinWyciag, OUTPUT);
pinMode(buttonPinSyrena, INPUT_PULLUP);
pinMode(buttonPinPompa, INPUT_PULLUP);
pinMode(buttonPinOswietlenie, INPUT_PULLUP);
pinMode(buttonPinWyciag, INPUT_PULLUP);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
Serial.begin(9600);
mySerial.begin(9600);
mySerial.println("AT+CMGF=1");
delay(700);
mySerial.println("AT+CNMI=1,2,0,0,0");
}
void loop() {
buttonStateSyrena = digitalRead(buttonPinSyrena);
buttonStatePompa = digitalRead(buttonPinPompa);
buttonStateOswietlenie = digitalRead(buttonPinOswietlenie);
buttonStateWyciag = digitalRead(buttonPinWyciag);
// SYRENA
newSwitchStateSyrena1 = buttonStateSyrena;
delay(1);
newSwitchStateSyrena2 = buttonStateSyrena;
delay(1);
newSwitchStateSyrena3 = buttonStateSyrena;
if ( (newSwitchStateSyrena1==newSwitchStateSyrena2) && (newSwitchStateSyrena1==newSwitchStateSyrena3) )
{
if ( newSwitchStateSyrena1 != oldSwitchStateSyrena )
{
// has the button switch been closed?
if ( newSwitchStateSyrena1 == HIGH )
{
if ( LEDstatusSyrena == LOW ) { digitalWrite(9, HIGH); LEDstatusSyrena = HIGH; } //zamiast 13 wpisac 9 po testach
else { digitalWrite(9, LOW); LEDstatusSyrena = LOW; startMillisSyrena = millis(); } //zamiast 13 wpisac 9 po testach
}
oldSwitchStateSyrena = newSwitchStateSyrena1;
}
}
if(LEDstatusSyrena == LOW)
{
currentMillisSyrena = millis();
if(currentMillisSyrena - startMillisSyrena >= periodSyrena)
{
oldSwitchStateSyrena = LOW;
}
}
// POMPA
newSwitchStatePompa1 = buttonStatePompa;
delay(1);
newSwitchStatePompa2 = buttonStatePompa;
delay(1);
newSwitchStatePompa3 = buttonStatePompa;
if ( (newSwitchStatePompa1==newSwitchStatePompa2) && (newSwitchStatePompa1==newSwitchStatePompa3) )
{
if ( newSwitchStatePompa1 != oldSwitchStatePompa )
{
// has the button switch been closed?
if ( newSwitchStatePompa1 == HIGH )
{
if ( LEDstatusPompa == LOW ) { digitalWrite(10, HIGH); LEDstatusPompa = HIGH; } //zamiast 13 wpisac 10 po testach
else { digitalWrite(10, LOW); LEDstatusPompa = LOW; startMillisPompa = millis(); } //zamiast 13 wpisac 10 po testach
}
oldSwitchStatePompa = newSwitchStatePompa1;
}
}
if(LEDstatusPompa == LOW)
{
currentMillisPompa = millis();
if(currentMillisPompa - startMillisPompa >= periodPompa)
{
oldSwitchStatePompa = LOW;
}
}
// OSWIETLENIE
newSwitchStateOswietlenie1 = buttonStateOswietlenie;
delay(1);
newSwitchStateOswietlenie2 = buttonStateOswietlenie;
delay(1);
newSwitchStateOswietlenie3 = buttonStateOswietlenie;
if ( (newSwitchStateOswietlenie1==newSwitchStateOswietlenie2) && (newSwitchStateOswietlenie1==newSwitchStateOswietlenie3) )
{
if ( newSwitchStateOswietlenie1 != oldSwitchStateOswietlenie )
{
// has the button switch been closed?
if ( newSwitchStateOswietlenie1 == HIGH )
{
if ( LEDstatusOswietlenie == LOW ) { digitalWrite(5, HIGH); LEDstatusOswietlenie = HIGH; }
else { digitalWrite(5, LOW); LEDstatusOswietlenie = LOW; startMillisOswietlenie = millis(); }
}
oldSwitchStateOswietlenie = newSwitchStateOswietlenie1;
}
}
if(LEDstatusOswietlenie == LOW)
{
currentMillisOswietlenie = millis();
if(currentMillisOswietlenie - startMillisOswietlenie >= periodOswietlenie)
{
oldSwitchStateOswietlenie = LOW;
}
}
// WCYIAG
newSwitchStateWyciag1 = buttonStateWyciag;
delay(1);
newSwitchStateWyciag2 = buttonStateWyciag;
delay(1);
newSwitchStateWyciag3 = buttonStateWyciag;
// if all 3 values are the same we can continue
if ( (newSwitchStateWyciag1==newSwitchStateWyciag2) && (newSwitchStateWyciag1==newSwitchStateWyciag3) )
{
if ( newSwitchStateWyciag1 != oldSwitchStateWyciag )
{
if ( newSwitchStateWyciag1 == HIGH )
{
if ( LEDstatusWyciag == LOW ) { digitalWrite(6, HIGH); LEDstatusWyciag = HIGH; }
else { digitalWrite(6, LOW); LEDstatusWyciag = LOW; startMillisWyciag = millis(); }
}
oldSwitchStateWyciag = newSwitchStateWyciag1;
}
}
if(LEDstatusWyciag == LOW)
{
currentMillisWyciag = millis();
if(currentMillisWyciag - startMillisWyciag >= periodWyciag)
{
oldSwitchStateWyciag = LOW;
}
}
if(mySerial.available()){
startDelay1 = millis();
delay(30);
while(mySerial.available()){
incomingByte = mySerial.read();
inputString += incomingByte;
}
delay(30);
Serial.println(inputString);
inputString.toUpperCase();
if (inputString.indexOf("ON") > -1){
if(LEDstatusSyrena == HIGH){oldSwitchStateSyrena = LOW;}
if(LEDstatusPompa == HIGH){oldSwitchStatePompa = LOW;}
if(LEDstatusOswietlenie == HIGH){oldSwitchStateOswietlenie = LOW;}
if(LEDstatusWyciag == HIGH){oldSwitchStateWyciag = LOW;}
}
delay(20);
if (inputString.indexOf("OK") == -1){
mySerial.println("AT+CMGDA=\"DEL ALL\"");
delay(1000);
}
inputString = "";
}
}