Issues with PIR sensor code from Arduino

Hi,

I tried to get a very simple, standard code to work with the Spark. For hours i’ve been thinking the PIR motion sensors were broken, but now I know the code is the problem. Can someone tell me what to adjust in order to make this Arduino code work on a Spark core? And of course I know the pins should be changed etc.

This is the code i use:

int calibrationTime = 30;        

long unsigned int lowIn;         

long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

Pin declaration seems to be a problem: http://docs.spark.io/firmware/#input-output

You could take a look at this post for inspiration. It’s about testing the Spark.publish function, and uses a PIR sensor to do so. Great for learning both :slight_smile:

2 Likes

Thanks @Moors7 for helping me. Although, the example you gave me i’ve already tried and seemed to be not working as well. The sensors do give data, but random and not in relation to what the sensor really should see. Besides, im sure the sensors work properly.

Could you be more specific about the pin declaration? Since I declared all the pin’s in the void setup(), the only aspect i can think of is to change the declaration of the pirpin to:

pinMode(pirPin, INPUT_PULLUP);

or

pinMode(pirPin, INPUT_PULLDOWN);

I think there should be a really stupid mistake somewhere, but i;m missing it.

Regards

Hi @KLFSBK

These lines above are a bad idea on Spark. The pin names and numbers are not the same as on Arduino (even some Arduinos have different numbers) so using D3 instead of 3 and D7 instead of 13 will work much better.

Thanks for your comment, however I know that these should be changed, like i already wrote in my problem statement. So, that is definitely not the problem

try a simplified version first:

int calibrationTime = 30;
int pirPin = D3; 
int ledPin = D7;
byte lastState;

void setup()
{
  Serial.begin(9600);
  pinMode(pirPin, INPUT_PULLDOWN);
  pinMode(ledPin, OUTPUT);
  Serial.print("calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++)
  {
    Serial.print(".");
    delay(1000);
  }
  Serial.println(" done");
  Serial.println("SENSOR ACTIVE");
  delay(50);
}

void loop()
{
  byte motionState = digitalRead(pirPin);
  if(motionState == HIGH && lastState == LOW)
  {
      digitalWrite(ledPin, HIGH);
      Serial.println("---");
      Serial.print("motion detected at ");
      Serial.print(Time.timeStr());
  }

  else if(motionState == LOW && lastState == HIGH)
  {   
      digitalWrite(ledPin, LOW);
      Serial.print("motion ended at ");      //output
      Serial.print(Time.timeStr());
  }
  lastState = motionState;
}