Hello,
I have 4 push buttons (from left to right in the attached image: AutoRaise, Limit Switch, Stop, ManualRaise.) and 3 LED's. The general premise of the system is:
-
AutoRaise: When pressed and released, the orange and green LED's remain on, i.e. latched, unless the limit switch or stop buttons are pressed.
-
ManualRaise: When pressed and held, the orange led remains on. if the button is released, the orange LED turns off.
-
If the limit switch is pressed whilst the manual raise button is held, the orange LED turns off.
The following code worked last night, when i've tried it again today it no longer works, i.e. pressing the manual raise button does nothing and i can't figure out why since nothing has changed. All other functions work as normal, therefore it's not the arduino. I've replaced all wires, resistors, tried different pins, nothing seems to work.
const int AutoRaise = 12;
const int Stop = 8;
const int ManualRaise = 7;
const int led = 4;
const int LimitSwitch = 11;
const int ledGreen = 3;
const int ledRed = 2;
int buttonTrigger = 0;
int ledState = 0;
void setup(){
pinMode(led, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(AutoRaise, INPUT);
pinMode(Stop, INPUT);
pinMode(LimitSwitch, INPUT);
pinMode(ManualRaise, INPUT);
}
void loop(){
///////////////AUTO-RAISE///////////////////
if(digitalRead(AutoRaise) == HIGH){ // if the auto raise button is pressed
digitalWrite(led, HIGH); // turn on the LED
ledState = 1; // set the led state variable to 1
buttonTrigger = 1; // set the button trigger variable to 1
}
/////////////////STOP//////////////////////
if(digitalRead(Stop) == HIGH){ // if the stop button is pressed
digitalWrite(led, LOW); // turn off the led
ledState = 2; // set the led state variable to 2
buttonTrigger = 2; // and set the button trigger variable to 2
}
///////////////LIMIT SWITCH////////////////
if(digitalRead(LimitSwitch) == HIGH){ // if the limit switch is pressed
digitalWrite(led, LOW); // turn off the led
ledState = 2; // set the led state variable to 2
buttonTrigger = 4; // and set the button trigger variable to 2
}
////////////////MANUAL-RAISE////////////////
// if the manual raise button is pressed
if(digitalRead(ManualRaise) == HIGH){
// then check led state. if LED on and limit switch isn't made then do nothing
if(digitalRead(led) == HIGH && digitalRead(LimitSwitch == LOW)){
}
// however if the manual raise is held, and the limit switch is made
else if(digitalRead(ManualRaise) == HIGH && digitalRead(LimitSwitch == HIGH)){
// turn off the led
digitalWrite(led, LOW);
}
// otherwise if led is not on and the limit switch is not made
else{
// set the buttonTrigger variable to "3"
buttonTrigger = 3;
// and turn on the LED
digitalWrite(led, HIGH);
}
// if the manual raise button is now not pressed
}else{
// but the LED was turned on by the manual raise button (ensures that other buttons can turn on the led)
// (otherwise the led would remain off as long as the manual raise wasnt pressed)
if(buttonTrigger == 3){
// then turn off the LED.
digitalWrite(led, LOW);
}
}
//////GREEN LED TO INDICATE TEST START//////
if(ledState == 1){ // led state can only equal one if the auto raise button was pressed
digitalWrite(ledGreen, HIGH); // therefore turn on the green led to indicate the test is in progress
}else{ // as soon as the test is stopped, i.e. led state is 2, or "not" 1,
digitalWrite(ledGreen,LOW); // turn the green led off.
}
/////RED LED TO INDICATE STOP BUTTON PRESSED////
if(ledState == 2){ // on pressing the stop button, led state variable is set to 2
digitalWrite(ledRed, HIGH); // therefore check if led state is equal to 2, and if so turn on the red led
delay(3000); // for 3 seconds (note: 3000 milliseconds = 3 seconds)
digitalWrite(ledRed, LOW); // then turn off the red led
ledState = 0; // and put the led state back to 0 (possibly not required to do this?)
}
}
The line of code that is causing issues is:
else if(digitalRead(ManualRaise) == HIGH && digitalRead(LimitSwitch == HIGH)){
digitalWrite(led, LOW);
}
Removing this part of the code allows the manual raise button to work as required, however pressing the limit switch whilst manual raise is held no longer turns off the orange LED of course.
Can anyone spot why this might not be working?
Thanks in advance,
Carl