Hello! I'm trying to make a function which would detect if a button was pressed and return which one was it and zero if none of the buttons was pressed. It should detect only when button went from LOW to HIGH and return which one was pressed no matter how long I hold the button. I have 6 buttons connected on analog pin 5 and have checked which range of numbers is associated with each button. Can you tell if it is OK, since when I use it, it seems that it doesn't work properly:
int old_b;
void setup () {...}
int readButtons (int pin) {
int b, c;
c = analogRead(pin);
Serial.print("analogRead = ");
Serial.println(c);
if (c > 1015) b = 0;
else if (c > 70 && c < 76) b = 1;
else if (c > 122 && c < 128) b = 2;
else if (c > 169 && c < 175) b = 3;
else if (c > 209 && c < 217) b = 4;
else if (c > 247 && c < 256) b = 5;
else if (c > 280 && c < 291) b = 6;
else b = 0;
if (b == old_b) {
return 0;
old_b = b;
} else {
return b;
old_b = b;
}
}
void loop () {...}