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.
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);
}
}
}