Hello everybody.
I found an interesting schematic on internet(see attached file) that works very well.
Plugged to an analog read, four buttons activate their following OUTPUT. (S1 to Led1, S2 to Led2, etc...)
That works great but there is a major default , when pushing 2 or more buttons simultaneously the following inputs are ON, so several outputs are ON.
My question is, how to prevent several inputs to be activated as i want only one input (The first activated) to be ON even simultaneous pushing on 2 or more buttons.
Here is the code, maybe a function is enough i don't know.
int lcd_key = 0;
int analog = 0;
int read_buttons(){ // read the buttons
analog = analogRead(0); // read the analog input connected to pin 0
if (analog > 1000) return 0;
if (analog < 50) return 1;
if (analog < 250) return 2;
if (analog < 450) return 3;
if (analog < 650) return 4;
return 0; // when all others fail, return this.
}
void setup(){
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}
void loop(){
lcd_key = read_buttons(); // read the buttons
switch (lcd_key){
case 1:{
digitalWrite(12, HIGH);
break;
}
case 2:{
digitalWrite(11, HIGH);
break;
}
case 3:{
digitalWrite(10, HIGH);
break;
}
case 4:{
digitalWrite(9, HIGH);
break;
}
case 0:{
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
break;
}
}
}