Hello all,
I have some questions regarding some simple arduino R3 code. I am playing around with a little “arduino start up kit” that has numerous push buttons, leds, resistors etc. and am trying to get a simple circuit to work.
What I am aiming to do:
Use one push button to turn the green LED on only while its being pressed (red LED must be off)
Use another push button to turn the red LED on only while its being pressed (green LED must be off)
Use a motion sensor to trigger a sequence of red and green LED on off states if its tripped
note: the priority is the push buttons, the code must check their state FIRST continuously, and then if nothing is pushed it can check the motion sensor
I have the circuit built correctly, so that both LEDs do turn on and the push buttons will control them (each one respectively), the problem that I am having is that I can only get each button to turn either LED on indefinitely, with only a single push. I have tried if/else statements, while loops, functions and am flat out too inexperienced to make this thing work the way I want it to. Any thoughts?
//Begin serial communications at 9600 bits/sec
const int forward = 3;
const int reverse = 4;
const int msense = 6;
const int Bforward = 11;
const int Breverse = 10;
int yellow = 13;
void setup() {
Serial.begin(9600);
pinMode(msense, INPUT_PULLUP); //This is the motionsensor
pinMode(forward, OUTPUT); //This is the green LED
pinMode(reverse, OUTPUT); //This is the red LED
pinMode(yellow, OUTPUT); //This is the built in LED that I am using to monitor when the code is working
pinMode(Bforward, INPUT); //This is the green LED button
pinMode(Breverse, INPUT); //This is the red LED button
digitalWrite(forward, LOW);
digitalWrite(reverse, LOW);
}
void loop() {
int detect = digitalRead(msense);
int Bpress = digitalRead(Bforward);
int Bpress1 = digitalRead(Breverse);
delay(5);
// I'm trying to check both buttons at the same time with this code... Could be a //better way?
if (Bpress == LOW || Bpress1 == LOW){
digitalWrite(yellow, HIGH);
if (Bpress == LOW){
digitalWrite(reverse, LOW);
digitalWrite(forward, HIGH);
}
else if (Bpress1 == LOW){
digitalWrite (forward, LOW);
digitalWrite(reverse, HIGH);
}
else {
digitalWrite(reverse, LOW);
digitalWrite(forward, LOW);
digitalWrite(yellow, LOW);
}
}
//This is the motion sensor sequence that should run after motion is detected and no buttons are pressed
else if (detect == LOW){
digitalWrite(yellow, LOW);
delay(1000);
digitalWrite (yellow, HIGH);
delay(1000);
digitalWrite(forward, HIGH);
delay(5000);
digitalWrite(forward, LOW);
delay (5000);
digitalWrite(reverse, HIGH);
delay(5000);
digitalWrite(reverse, LOW);
delay(1000);
digitalWrite(yellow, LOW);
}
}