Hi,
thanks for all your help.
This is my shortened code..
//Al there is to loop
void loop()
{
if(checkKeyboard() || alarmState > 3)
{
changeAlarmState();
}
if(alarmState == 2)
{
if(!checkZones())
{
entryTime(getActiveZone());
}
}
if(millis() - GSMpreviousMillis > 20000)
{
checkGSMConnected();
GSMpreviousMillis = millis();
}
}
//I didn't include other functions, as they arent the source of problem
//
void entryTime(int activeZone)
{
Serial.println(F("Objekt narusen, spoustim prichodovy cas!"));
unsigned long previousMillis = millis();
while(millis() - previousMillis <= entryTimeVal*1000){
if (checkKeyboard() == 1){
Canceled();
return;
}
tone(outputBuzzer,1100);
leds(1,1);
delay(200);
noTone(outputBuzzer);
leds(0,1);
delay(200);
}
//if no keyboard input
Serial.println(F("Alarm aktiviovan!"));
tone(outputBuzzer, 1100);
leds(0,1);
alarmState = 3;
alarm(1);
sendSMStoAll(String(F("Objekt narusen, pricina: zona ")) + activeZone + " " + loadZoneName(activeZone));
// /\ this blocks execution for about 10 s, while the user cant cancel the alarm
}
//the multiple SMS sending function
void sendSMStoAll(String text)
{
char textSMS[160];
text.toCharArray(textSMS, 160);
char phoneNum[20];
for(int i = 0; i < 5; i++)
{
loadPhoneNumber(i+1).toCharArray(phoneNum,20);
if(sms.SendSMS(phoneNum , textSMS))
{
Serial.println("SMS odeslana!");
}
}
}
//But this fatty is also a problem (up to 10s)
void checkGSMConnected()
{
ledsGSM(1,1);
boolean GSMregistered = gsm.CheckRegistration();
if(AT_RESP_OK == gsm.SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)) //this alone takes about a second
{
Serial.println(F("GSM OK."));
char sms_position = sms.IsSMSPresent(SMS_UNREAD);
if (sms_position)
{
char phone_number[20];
char sms_textCH[160];
Serial.println(F("\nPrijata SMS:"));
sms.GetSMS(sms_position, phone_number, sms_textCH, 100);
Serial.println(phone_number);
Serial.println(sms_textCH);
String sms_text = String(sms_textCH);
sms_text.toLowerCase();
if(sms_text.indexOf(pinSMS) > -1)
{
Serial.println(F("PIN OK!"));
String reply = "Nebyl rozpoznán příkaz!";
if(sms_text.indexOf("entrytime") > -1) // I don't really know how to make those shorter, or less resource consuming
{
sms_text.replace(pinSMS, "");
sms_text.replace("entrytime", "");
EEPROM.update(3, sms_text.toInt());
entryTimeVal = sms_text.toInt();
reply = String(F("Prichodovy cas nastaven na ")) + entryTimeVal + " sekund!";
} else
if(sms_text.indexOf("exittime") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("exittime", "");
EEPROM.update(4, sms_text.toInt());
exitTimeVal = sms_text.toInt();
reply = String(F("Odchodovy cas nastaven na ")) + exitTimeVal + " sekund!";
} else
if(sms_text.indexOf("pin") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("pin", "");
sms_text.trim();
savePin(sms_text);
pinSMS = sms_text;
reply = String(F("SMS PIN nastaven na ")) + pinSMS;
} else
if(sms_text.indexOf("output1") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("output1", "");
sms_text.trim();
digitalWrite(outputGSM1, sms_text.toInt());
reply = String(F("GSM vystup 1 nastaven na ")) + digitalRead(outputGSM1);
} else
if(sms_text.indexOf("output2") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("output2", "");
sms_text.trim();
digitalWrite(outputGSM2, sms_text.toInt());
reply = String(F("GSM vystup 2 nastaven na ")) + digitalRead(outputGSM2);
} else
if(sms_text.indexOf("setzonename") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("setzonename", "");
sms_text.trim();
int zoneNum = String(sms_text.charAt(0)).toInt();
sms_text.replace(String(zoneNum),"");
saveZoneName(zoneNum, sms_text);
reply = String(F("Nazev zony ")) + zoneNum + " nastaven na " + loadZoneName(zoneNum);
} else
if(sms_text.indexOf("getzonename") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("getzonename", "");
sms_text.trim();
int zoneNum = String(sms_text.charAt(0)).toInt();
reply = String(F("Nazev zony ")) + zoneNum + " je " + loadZoneName(zoneNum);
} else
if(sms_text.indexOf("setphonenumber") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("setphonenumber", "");
sms_text.trim();
int orderNum = String(sms_text.charAt(0)).toInt();
sms_text.replace(String(orderNum),"");
savePhoneNumber(orderNum, sms_text);
reply = String(F("Tel. cislo na pozici ")) + orderNum + " nastaveno na " + loadPhoneNumber(orderNum);
} else
if(sms_text.indexOf("getphonenumber") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("getphonenumber", "");
sms_text.trim();
int orderNum = String(sms_text.charAt(0)).toInt();
reply = String(F("Tel. cislo na pozici ")) + orderNum + " je " + loadPhoneNumber(orderNum);
} else
if(sms_text.indexOf("setstate") > -1)
{
sms_text.replace(pinSMS, "");
sms_text.replace("setstate", "");
sms_text.trim();
alarmState = sms_text.toInt();
reply = String(F("Stav alarmu byl zmenen na ")) + alarmState;
} else
if(sms_text.indexOf("getstate") > -1)
{
reply = String(F("Stav alarmu: ")) + alarmState + ", GSM1 ve stavu " + digitalRead(outputGSM1) + ", GSM2 ve stavu " + digitalRead(outputGSM2) + ", prichodovy/odchodovy cas " + entryTimeVal + "/" + exitTimeVal + "."; ;
} else
if(sms_text.indexOf("getsensorstates") > -1)
{
reply = String(F("Stav senzoru: ")) + alarmState + ", 1: " + analogRead(alarmZone1) + ", 2: " + analogRead(alarmZone2) + ", 3: " + analogRead(alarmZone3) + ", 4: " + analogRead(alarmZone4);
}
char textSMS[160];
reply.toCharArray(textSMS, 160);
sms.SendSMS(phone_number, textSMS);
Serial.println(reply);
}
else
{
Serial.println(F("SMS zprava neobsahuje spravny PIN!"));
}
sms.DeleteSMS(sms_position);
Serial.println("Smazano");
} else {
Serial.println(F("Nebyla prijata zadna SMS"));
}
}else{
Serial.println(F("GSM bylo odpojeno, pripojuji..."));
ledsGSM(0,1);
if (gsm.begin(4800))
{
Serial.println(F("GSM pripraveno!"));
ledsGSM(1,0);
}else{
Serial.println(F("GSM nepripojeno!"));
}
}
ledsGSM(1,0);
}