Table Of Contents
Tinkercad project
Reading analog button value
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(readButtons());
}
int readButtons(){
return analogRead(A0);
}
/*
0 //2000R
145 //330R
329 //620R
505 //1000R
741 //3300R
871 //6200R
*/
Buttons with enums
enum Btns {NONE, ONE, TWO, THREE, FOUR, FIVE, SIX};
Btns readButtons();
Btns btn = NONE;
Btns lastBtn = NONE;
void setup() {
Serial.begin(9600);
}
void loop() {
btn = readButtons();
if(lastBtn != btn){
switch(btn){
case ONE:
Serial.println("First function to be executed");
break;
case TWO:
Serial.println("Second function to be executed");
break;
case THREE:
Serial.println("Third function to be executed");
break;
//.....
default:
break;
}
lastBtn = btn;
}
}
Btns readButtons(){
int adcKey= analogRead(A0);
if (adcKey> 1000) return NONE;
else if (adcKey< 50) return ONE;
else if (adcKey< 200) return TWO;
else if (adcKey< 400) return THREE;
else if (adcKey< 600) return FOUR;
else if (adcKey< 800) return FIVE;
else if (adcKey< 900) return SIX;
else return NONE;
}