xl97:
Its not clear (without code) when you think button 3 should do?
You state: " release button = LED turns off" for button 3..
So it would seem that it is working as intended?
Explain your secnario(s)
If button 1 turns on the led.. and then button 3 is pressed.. what should happen? What should happen when button 3 is released then?
Without knowing your desired outcome/results.... I can only suggest then when button 3 is pressed you also check the state of led (or perhaps button 1 if it is truly a latching switch)..
If the led is ON when button 3 is pressed, and you do NOT want it to turn off when released.. then in effect button 3 has no bearing on the project/led if its already on.....
actually saving the led STATE to a variable in all button action is probably the best approach.. or even just saving the button that triggered the led... (so you know when its ok for button 3 to turn things off or not)
maybe something along the lines like: (not tested, sitting at work still)
const int button1 = 12; // manual on
const int button2 = 8; // stop
const int button3 = 7; // auto on
const int led = 13;
int buttonTrigger = 0;
void setup(){
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
}
void loop(){
if(digitalRead(button1) == HIGH){ // i.e. latch the LED on until stop button is pressed
digitalWrite(led, HIGH);
ledState = 1;
buttonTrigger = 1;
}
if(digitalRead(button2) == HIGH){
digitalWrite(led, LOW);
ledState = 0;
buttonTrigger = 2;
}
}
if(digitalRead(button3) == HIGH){
//check led state
if(digitalRead(led) == HIGH){
//behave accordingly:
//already on, do nothing
}else{
//behave accordingly:
//led if currently off, its ok to turn it on)
buttonTrigger = 3;
digitalWrite(led, HIGH);
}
}else{
//button 3 is low/released
if(buttonTrigger == 3){
//button 3 turned it on, so its ok to turn off
digitalWrite(led, LOW);
}
}
explain more and you can get help.
Ok this works perfectly! declaration of led state and a couple extra brackets added:
const int button1 = 12; // manual on
const int button2 = 8; // stop
const int button3 = 7; // auto on
const int led = 13;
int buttonTrigger = 0;
int ledState = 0;
void setup(){
pinMode(led, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
}
void loop(){
if(digitalRead(button1) == HIGH){ // i.e. latch the LED on until stop button is pressed
digitalWrite(led, HIGH);
ledState = 1;
buttonTrigger = 1;
}
if(digitalRead(button2) == HIGH){
digitalWrite(led, LOW);
ledState = 0;
buttonTrigger = 2;
}
if(digitalRead(button3) == HIGH){
//check led state
if(digitalRead(led) == HIGH){
//behave accordingly:
//already on, do nothing
}else{
//behave accordingly:
//led if currently off, its ok to turn it on)
buttonTrigger = 3;
digitalWrite(led, HIGH);
}
}else{
//button 3 is low/released
if(buttonTrigger == 3){
//button 3 turned it on, so its ok to turn off
digitalWrite(led, LOW);
}
}
}
Thanks!!