HELP!!! INFINITE LOOP in GSM PIR HOME AUTOMATION AND SECURITY SYSTEM

I really need help at my project in the PIR part… when it detects motion, it sends a infinite text messages even though there is no motion detected already. Anyone can help me please :frowning: here is the code:

char inchar;
int led = 53;
int led1 = 51;
int led2 = 49;
int buzzer = 47;
int smokeA0 = A5;
const int sensor = 2;
int sensorThres = 400;
int state;
int sms_count=0;
int sms2_count=0;
int flag = 0;
int flagpir = 0;
byte flag1 = 0;
byte flag2 = 0;
int pirstate;
int val = 0;
const int pir = 3;
int count = 0;
char inputButtonState; char inputButtonState2; char inputButtonState4; char inputButtonState6; char inputButtonState8;
char inputButtonState1; char inputButtonState3; char inputButtonState5; char inputButtonState7; char inputButtonState9;
void setup() {
// put your setup code here, to run once:
pinMode(sensor, INPUT_PULLUP);
pinMode( 17, INPUT_PULLUP );
pinMode( pir , INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzzer, OUTPUT );
Serial1.begin(9600);
delay(20000); // give time to log on to network.
Serial1.print(“AT+CMGF=1\r”); // set SMS mode to text
delay(100);
Serial1.print(“AT+CNMI=2,2,0,0,0\r”);
delay(100);
Serial1.println(“AT+CMGD=1,4”);
}

void loop() {

inputButtonState = digitalRead(34); inputButtonState4 = digitalRead(38); inputButtonState7 = digitalRead(41); //Automation Manual Switch
inputButtonState1 = digitalRead(35); inputButtonState5 = digitalRead(39); inputButtonState8 = digitalRead(42); //Automation Manual Switch
inputButtonState2 = digitalRead(36); inputButtonState6 = digitalRead(40); inputButtonState9 = digitalRead(43); //Automation Manual Switch
inputButtonState3 = digitalRead(37); //Automation Manual Switch

if (inputButtonState == HIGH) { //Automation Manual Switch
digitalWrite(led, HIGH); //Automation Manual Switch
}
if (inputButtonState1 == HIGH) { //Automation Manual Switch
digitalWrite(led, LOW); //Automation Manual Switch
}
if (inputButtonState2 == HIGH) { //Automation Manual Switch
digitalWrite(led1, HIGH); //Automation Manual Switch
}
if (inputButtonState3 == HIGH) { //Automation Manual Switch
digitalWrite(led1, LOW); //Automation Manual Switch
}
if (inputButtonState4 == HIGH) { //Automation Manual Switch
digitalWrite(led2, HIGH); //Automation Manual Switch
}
if (inputButtonState5 == HIGH) { //Automation Manual Switch
digitalWrite(led2, LOW); //Automation Manual Switch
}
if (inputButtonState6 == HIGH) { //Automation Manual Switch
digitalWrite(led, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH); //Automation Manual Switch
}
if (inputButtonState7 == HIGH) { //Automation Manual Switch
digitalWrite(led, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW); //Automation Manual Switch
}
if (inputButtonState8 == HIGH) { //Automation Manual Switch
flag = 1;
}

if (inputButtonState9 == HIGH) { //Automation Manual Switch

}
if(Serial1.available() >0)
{
if (inchar==’#’)
{
delay(10);
inchar=Serial1.read();
if (inchar==‘a’){
delay(10);
inchar=Serial1.read();
if (inchar==‘0’)
{
digitalWrite(led, LOW);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned off light1.”);
delay(1000);
Serial1.write(26);
}
else if (inchar==‘1’)
{
digitalWrite(led, HIGH);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned on light1.”);
delay(1000);
Serial1.write(26);
}
delay(10);
Serial1.println(“AT+CMGD=1,4”); // delete all SMS
return;
}
if (inchar==‘b’)
{
delay(10);
inchar=Serial1.read();
if (inchar==‘0’)
{
digitalWrite(led1, LOW);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned off light2.”);
delay(1000);
Serial1.write(26);
}
else if (inchar==‘1’)
{
digitalWrite(led1, HIGH);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned on light2.”);
delay(1000);
Serial1.write(26);
}
delay(10);
Serial1.println(“AT+CMGD=1,4”); // delete all SMS
return;
}
if (inchar==‘c’)
{
delay(10);
inchar=Serial1.read();
if (inchar==‘0’)
{
digitalWrite(led2, LOW);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned off light3.”);
delay(1000);
Serial1.write(26);
}
else if (inchar==‘1’)
{
digitalWrite(led2, HIGH);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently turned on light3.”);
delay(1000);
Serial1.write(26);
}
delay(10);
Serial1.println(“AT+CMGD=1,4”); // delete all SMS
return;
}
if (inchar==‘d’)
{
delay(10);
inchar=Serial1.read();
if (inchar==‘0’)
{
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
digitalWrite(led , LOW);
}
else if (inchar==‘1’)
{
digitalWrite(led2, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led , HIGH);
}
delay(10);
Serial1.println(“AT+CMGD=1,4”); // delete all SMS
return;
}
}
inchar=Serial1.read();
}
if (flag == 1){
doorsensor();
pirsensor();
}
if (flag1 == 0){
digitalWrite(buzzer, LOW);
sms_count = 0;
}
if (flag2 == 0){
digitalWrite(buzzer, LOW);
count = 0;
}
}

void sendsmspir() {
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently passed by!”);
delay(200);
Serial1.write(26);
delay(1000);
count++;
}
void sendsmsdoor() {
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT”);
delay(2000);
Serial1.println(“AT+CMGF=1”);
delay(1000);
Serial1.println("AT+CMGS=“09214878318"”);
delay(1000);
Serial1.print(“someone recently opened the door!”);
delay(200);
Serial1.write(26);
delay(1000);
sms_count++;
}
void doorsensor(){
state = digitalRead(sensor);
if (state == HIGH){
digitalWrite(buzzer, HIGH);
while(sms_count<1)
{
sendsmsdoor() ;
flag1 = 0;
}
}
}
void pirsensor(){

val = digitalRead(pir);
if (val == HIGH){
while(count<1)
{
digitalWrite(buzzer, HIGH);
sendsmspir();
flag2 = 0;
}
}

}

Thanks Godbless
it’s in the input buttonstate 8 part …

Learn to present code, using code tags </>, how to debounce switches (Debounce example) and detecting state changes (StateChangeDetection), how to use loops, and give your variables meaningful names.

The pirsensor() function will never stop, unless count is updated inside the while loop.

As already mentioned, your variable naming is hurting you - it's hard to see what's going on. However, take a look at flag. Once it is set, there is nothing in your code to turn it off.

It looks to me like you’ve got too many globals, and too few constants.

Please edit your original post and add the code tags - my scrolling finger hurts.