Hi
The memue is changing but serial monitor is showing only case 0. After pressing the button the first time, the monitor is blank. After pressing the button four more times, the serial monitor displays case 0 again.
//UUP 2660 2500 2750
//UDOWN 100
//ULEFT 2300
//URIGHT 300
// SW 3180
const int btn1Min = 30, btn1Max = 150; //DOWN
const int btn2Min = 200, btn2Max = 400; // RIGHT
const int btn3Min = 2200, btn3Max = 2400; // LEFT
const int btn4Min = 2550, btn4Max = 2750; // UP
const int btn5Min = 3100, btn5Max = 3300; // SW
int botonState = 0;
void setup() {
Serial.begin(115200);
pinMode(36, INPUT);
}
void loop()
{
int adcValue = analogRead(36);
// Ignore noise or no-press states
if (adcValue > 20) {
if (adcValue >= btn1Min && adcValue <= btn1Max) {
Serial.println(" Button 1 DOWN");
}
else if (adcValue >= btn2Min && adcValue <= btn2Max) {
Serial.println(" Button 2 RIGHT");
}
else if (adcValue >= btn3Min && adcValue <= btn3Max) {
Serial.println(" Button 3 LEFT");
}
else if (adcValue >= btn4Min && adcValue <= btn4Max) {
Serial.println(" Button 4 UP");
}
else if (adcValue >= btn5Min && adcValue <= btn5Max) {
Serial.println(" Button 5 SW");
}
}
Serial.println();
if (adcValue >= btn5Min && adcValue <= btn5Max) {
botonState = (botonState - 1) % 5;
delay (200);
}
switch (botonState)
{
case 0:
{
Serial.print("C0 ");
Serial.println();
}
break;
case 1:
{
Serial.print("C1 a ");
Serial.println();
}
break;
{
//case 2:
Serial.print("C1 ");
Serial.println();
}
break;
case 2:
{
Serial.print("C2 ");
Serial.println();
}
break;
case 3:
{
Serial.print("C3 ");
Serial.println();
}
break;
case 4:
{
Serial.print("C4 ");
Serial.println();
}
break;
delay (300);
}
}
Other than the initialization to zero, the variable botonstate does not appear to be being set by the buttons. Don't know what values the ADC returns on your particular board, but just wondering whether the button value was supposed to be derived from those results somehow? For example, might you have intended:
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
How is your button wired?
Between input and gnd, with a 10K pullup resistor.
OR
Between input and 3V3, with a 10K pulldown resistor.
If its a button, why are you analog reading instead of digital reading.
Check that you can setup pin 36 as a digital input.
Button State = 1
Button State = 1
Button State = 1
Button State = 1
Button State = 0
Button State = 0
Button State = 0
Button State = 0
Button State = 0
Button State = 1
Button State = 1
Button State = 1
Button State = 1
The code in that tutorial is quite simple (if unfortunately presented in awful screenshots). The case range idea suggested in post #9 would be another option. I think you might be over-complicating it.
(BTW, @alto777 thanks for pointing that out. I wasn't aware that option existed.)