Go Down

Topic: Sensor indicator light reset (Read 415 times) previous topic - next topic

scv88

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: [Select]

/*
* 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: [Select]
/*
* 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.

JavaMan


scv88

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.

JavaMan

Code: [Select]
int buttonPin3 = 6;
...
Code: [Select]
pinMode(buttonPin3, INPUT);
...
Code: [Select]
reading3 = digitalRead(buttonPin3);
...
Code: [Select]
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: [Select]
pinMode(buttonPin3, INPUT_PULLUP);
...
Code: [Select]
if (reading3 == LOW)

scv88

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.

JavaMan

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)

scv88

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.

Go Up