I have this existing project that I would like to modify.
existing, a push button counts and toggles through different combination of LEDS.
I would like to add a IR sensor that drops all LEDS to red when the sensor is HIGH.
where do I place the IR Sensor If statement?
[code]
// this constant won't change:
const int buttonPin = 7;
const int ledPinredTop = 4;
const int ledPinyellowTop = 3;
const int ledPingreenTop = 2;
const int ledPingreenBtm = 5;
const int ledPinredBtm = 6;
const int IRSensor = 8;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPinredTop, OUTPUT);
pinMode(ledPinyellowTop, OUTPUT);
pinMode(ledPingreenTop, OUTPUT);
pinMode(ledPingreenBtm, OUTPUT);
pinMode(ledPinredBtm, OUTPUT);
pinMode(IRSensor, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
//added
if (buttonPushCounter == 1) {
digitalWrite(ledPinredTop, LOW);
digitalWrite(ledPinyellowTop, HIGH);
digitalWrite(ledPingreenTop, HIGH); //STOP - RED OVER RED
digitalWrite(ledPinredBtm, LOW);
digitalWrite(ledPingreenBtm, HIGH);
} else if (buttonPushCounter == 2) {
digitalWrite(ledPinredTop, HIGH);
digitalWrite(ledPinyellowTop, HIGH);
digitalWrite(ledPingreenTop, LOW); //CLEAR - GREEN OVER RED
digitalWrite(ledPinredBtm, LOW);
digitalWrite(ledPingreenBtm, HIGH);
} else if (buttonPushCounter == 3) {
digitalWrite(ledPinredTop, HIGH);
digitalWrite(ledPinyellowTop, LOW);
digitalWrite(ledPingreenTop, HIGH); //APPROACH DIVERGING - YELLOW OVER GREEN
digitalWrite(ledPinredBtm, LOW);
digitalWrite(ledPingreenBtm, HIGH);
} else if (buttonPushCounter == 4) {
digitalWrite(ledPinredTop, HIGH);
digitalWrite(ledPinyellowTop, LOW);
digitalWrite(ledPingreenTop, HIGH); //APPROACH - YELLOW OVER RED
digitalWrite(ledPinredBtm, HIGH);
digitalWrite(ledPingreenBtm, LOW);
}
else {
digitalWrite(ledPinredTop, HIGH);
digitalWrite(ledPinyellowTop, LOW);
digitalWrite(ledPingreenTop, HIGH); //When it powers up and button has not been pushed.
digitalWrite(ledPinredBtm, LOW);
digitalWrite(ledPingreenBtm, HIGH);
buttonPushCounter =0;
}
}
[/code]