Hey guys I've been having trouble editing some code with arduino and I was wondering if you guys could help me. I am trying to hook up a PIR sensor so that it turns on an indicator light. The indicator light will then stay on until I use a pushbutton or a switch to reset it. Then the PIR sensor can sense motion again to turn it back on.
I edited the code from this website http://www.ladyada.net/wiki/tutorials/learn/sensors/pir.html which is:
/*
* PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
I got that code to work and I tried to edit it to what I specified here:
/*
* PIR sensor tester
*/
int indicator = 12; // choose the pin for the LED
int motion = 4; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int buttonPin3 = 6;
int reading3 = 0;
void setup() {
pinMode(indicator, OUTPUT); // declare LED as output
pinMode(motion, INPUT); // declare sensor as input
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(motion); // read input value
reading3 = digitalRead(buttonPin3);
if (val == HIGH) { // check if the input is HIGH
digitalWrite(indicator, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
}
if (reading3 == HIGH) { // check if the input is HIGH
digitalWrite(indicator, LOW); // turn LED OFF
}
}
But the light will only switch off after clicking the pushbutton a bunch of times and sometimes it turns off by itself. Any help or advice you guys could give would be appreciated. Thanks.