Pages: [1]   Go Down
Author Topic: Sensor indicator light reset  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
/*
 * 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:

Code:
/*
 * 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.
Logged

Southern California
Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you have a floating input.  Have you read this?  http://arduino.cc/en/Tutorial/DigitalReadSerial
Logged

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

I have a pull-down resistor and I have rechecked my connections to voltage and ground and made sure they're solid. I don't think that is the problem but thank you for the reply.
Logged

Southern California
Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int buttonPin3 = 6;
...
Code:
pinMode(buttonPin3, INPUT);
...
Code:
reading3 = digitalRead(buttonPin3);
...
Code:
if (reading3 == HIGH) {            // check if the input is HIGH

With pin 6 set to INPUT on one side of the button, and a pull-down resistor on the other side of the button, how will digitalRead() ever be HIGH?  Try it like this:

Code:
pinMode(buttonPin3, INPUT_PULLUP);
...
Code:
if (reading3 == LOW)
Logged

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

Hmm...so I tried that and it only worked some of the time. After I restarted the light with the pushbutton, the light only stays on temporarily when the motion is sensed. Thanks though.
Logged

Southern California
Offline Offline
Full Member
***
Karma: 0
Posts: 108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That's interesting because I used your code with my changes (below) and it worked fine on my Uno.

Quote
Code:

pinMode(buttonPin3, INPUT_PULLUP);

...
Code:

if (reading3 == LOW)
Logged

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

Yea that is weird. I've been fooling around with it making sure all the connections are good and correct but it doesn't work properly for me. Thanks for your input.
Logged

Pages: [1]   Go Up
Jump to: