PIR Sensor with Arduino UNO goes back to setup() function...

I'm working on an Arduino project that involves Arduino UNO, PIR sensor and a GSM Module. The idea is to send an SMS when a motion is detected. I've given about 1 minute delays between reading values from the sensor. However, I've noticed that the control goes back to the setup() function every time it reads from the sensor. I'm wondering why this is happening. I don't have any interrupts, just delay(). And all the logic is coded inside the loop() function.

Any idea why the control goes back to the setup()?

nileshp: I've given about 1 minute delays between reading values from the sensor.

Why?

nileshp: However, I've noticed that the control goes back to the setup() function every time it reads from the sensor. I'm wondering why this is happening.

If your code is truly returning to setup(), it means that the Arduino has power cycled and rebooted. This is usually caused by too much power being demanded by external electronics supplied by the Arduino (such as servos), or by removing power to the Arduino.

Many motion sensors have a built in off delay that may be several minutes long so they don't constantly blink ON-OFF-ON-OFF. How do you know your Arduino is "going back to setup"?

Power_Broker: Why?

If your code is truly returning to setup(), it means that the Arduino has power cycled and rebooted. This is usually caused by too much power being demanded by external electronics supplied by the Arduino (such as servos), or by removing power to the Arduino.

I don't want the code to read the sensors value every single time. If it detects a movement, I want to wait for a minute and then check again. Without this, a single detection of movement is causing the code to send out SMSs too many times.

Power_Broker: If your code is truly returning to setup(), it means that the Arduino has power cycled and rebooted. This is usually caused by too much power being demanded by external electronics supplied by the Arduino (such as servos), or by removing power to the Arduino.

How do I fix this?

For now, I'm powering the Arduino through my laptop's USB. One more thing, with the same setup, I didn't see this before. It was working fine before.

outsider: Many motion sensors have a built in off delay that may be several minutes long so they don't constantly blink ON-OFF-ON-OFF. How do you know your Arduino is "going back to setup"?

I have a Serial.print() function in the setup that tells me that it's in setup(). Plus I have a couple of counters that get reset as a result of the control going back to the setup() function.

Can you post your program + wiring diagram + link to sensor? Please paste your code between tags, like this:

 [code] YOUR CODE; [/code]

Sure. Here is the code:

#include <SoftwareSerial.h>

SoftwareSerial GPRS(7, 8); // TX, RX
boolean state;
int SensorDetectionCount, phoneCount;
int count=0;
int sensorPin = 2;
int buzzerPin = 5; //Will be used later...

unsigned char buffer[64];
void setup()
{
  GPRS.begin(9600);
  Serial.begin(9600);
  GPRS.println("AT+CMGF=1");
  SensorDetectionCount=0;
   
  delay(60000);
  pinMode(sensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  state = digitalRead(sensorPin);
  Serial.print("First time :" );

  Serial.print("SensorDetectionCount value :");
  Serial.println(SensorDetectionCount);   
}

void loop()
{
state = digitalRead(sensorPin);
boolean sendSMS = false;

if (state == HIGH) {
  sendSMS = true;
}
Serial.println("=============================BEGIN==========================");
Serial.print("Movement:  ");
Serial.println(state ? "on" : "off");

  Serial.print("SensorDetectionCount value :");
  Serial.println(SensorDetectionCount);

  Serial.print("PhoneCount::");
  Serial.println(phoneCount);
  
while(GPRS.available()) {
    buffer[count++] = GPRS.read();
    if (count == 64) break;
  }
  Serial.write(buffer, count);
  clearBufferArray();
//  raiseAlarm();
  

if(sendSMS) { // 
     Serial.println(sendSMS ? "yes": "no");
     Serial.print("Sending SMS");
        GPRS.println("AT+CMGS=\"+91XXXXXXXXXX\"\n");
        GPRS.println("Bro, looks like I have an uninvited guest. Please check.");  
        GPRS.write( 0x1a ); // ctrl+Z character
  
        if (Serial.available()) {
            byte b = Serial.read();
            if (b == '*')
            GPRS.write(0x1a);
          else
            GPRS.write(b);
      }
  
  Serial.println("Sent SMS");
  SensorDetectionCount++;
  delay(60000);
      
      if (SensorDetectionCount >2) {
//   If SensorDetectionCount is > 2, place a phone call

              Serial.println("SensorDetectionCount value :");
              Serial.print(SensorDetectionCount);
              GPRS.println("ATD+91XXXXXXXXXX;");
              Serial.println("Placing call..");
              Serial.println("Placing call");
              SensorDetectionCount=0;
              Serial.println("SensorDetectioncount reset");
              Serial.println("SensorDetectionCount value :");
              Serial.print(SensorDetectionCount);

              phoneCount++;
  
              delay(60000);
      if (phoneCount > 2) {
        Serial.println("Raising Alarm...");
        digitalWrite(buzzerPin, HIGH);
        delay(1000);
      }
    }
   
   }
   Serial.println("=============================END=========================="); 
}


void clearBufferArray(){
  for (int i = 0; i < count; i++){
    buffer[i] = NULL;
  }
}