Pages: [1]   Go Down
Author Topic: Adafruit PIR Sensor - troubleshooting code  (Read 143 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I'm trying to send a serial string when the sensor detects motion and then every ten seconds thereafter.  I can't find what's wrong with my code, but the sensor works with other sketches and the serial connection works.  Any ideas?  Much obliged.

Code:
const int pirPin = A0;
long lastmotion;
long now;
int LED = 13;
bool pirON = false;


void setup()
{
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}
 
void loop()
{
  now = millis(); // assign counter to current time...ish
  if (digitalRead(pirPin) == HIGH){  // if motion detected
    digitalWrite(LED, HIGH);
    if (pirON = false){    //if previous motion didn't happen
      Serial.println("item=motionevent;");
      pirON = true;      // establish that a previous event happened
      lastmotion = now;  // establish time of last event
    }
  }
 
 
  if (pirON = true){    //if event happened
    if (now > (lastmotion+10000l)){   // and it was more than ten seconds ago
      pirON = false;                 // reset previous motion event
      digitalWrite(LED, LOW);
    }
  }
}
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can't find what's wrong with my code

You haven't said what it's doing that's wrong....
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Classic C pitfall. This:
Code:
  if (pirON = true){    //if event happened
should be:
Code:
  if (pirON == true){    //if event happened
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's probably it - thank you!  Will test now...
Logged

Pages: [1]   Go Up
Jump to: