Hello:
I am trying to make a very simple two-button interface with an analog input (all the other digital pins are occupied). For that I made a very simple stair of resistors, so when I press the button 1 I receive a signal from 1000 to 1019 and when I press the other I receive the full 1023. The problem comes when I try to “save” the last button state and change when I press the other button. It gets stuck in the “sip” state and doesn’t change more. Here’s the code:
int ao_input;
int val;
int old_val;
int val1;
int old_val1;
int state;
int state1;
void setup() {
// put your setup code here, to run once:
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
ao_input = analogRead(A0);
if(ao_input >1000 and ao_input<1019){
val = 1;
}
else if(ao_input == 1023){
val1 = 1;
}
if ((val == 1) and (old_val == 0)){
state = 1-state;
delay(10);
}
old_val = val;
if ((val1 == 1) and (old_val1 == 0)){
state1 = 1-state1;
delay(10);
}
old_val1 = val1;
if(state == 1){
Serial.println(“sip”);
state1 = 0;
}
if(state1 == 1){
Serial.println(“nope”);
state = 0;
}
}
I will apreciate a lot your help!!