The serial monitor displays only case 0

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);
  }
}

maybe, try

Serial.print("botonstate = ");
Serial.println(botonstate);

right before your switch statement. I think you'll find botonstate is negative, if I didn't misread your code.

that might be better as

if (adcValue >= btn1Min && adcValue <= btn5Max) {

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:

botonState = (adcValue - 1) % 5;

?

I cleaned up the code


const int btn5Min = 3100, btn5Max = 3300;  // SW

int botonState = 0;


void setup() {
  Serial.begin(115200);

  pinMode(32, OUTPUT);
  digitalWrite(32, HIGH);
  pinMode(36, INPUT);
 
}

void loop()
{
 
  int adcValue = analogRead(36);
  if ((adcValue >= btn5Min && adcValue <= btn5Max)&&adcValue > 20) {
  
    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);
  }
}

After pressing the buttons 5 times serial monitor prints C0. This is the program using multi buttons on one wire.

I made these changes

  // if (digitalRead(0) == 0) {
 if (adcValue >= btn5Min && adcValue <= btn5Max){

Hi, @tom321

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.

Are you using an ESP32, if so what model?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

the circuit

https://forum.arduino.cc/t/multiple-buttons-single-pin/1324225/4

I think your point was to use btn1Min, or that's a typo.

Either way, all those read better rearranged a bit.

  if (btn1Min < adcValue && adcValue <= btn5Max) {

C/C++ has no mathematical

  if (a < x <= b) {  // half open interval

but that code will do something.

And check out case ranges:

  if (adcValue > 20) {
    switch (adcValue) {
    case btn1Min ... btn1Max:
      Serial.println("  Button 1 DOWN");
      break;

    case btn2Min ... btn2Max:
      Serial.println("  Button 2 RIGHT");
      break;

    case btn3Min ... btn3Max:
      Serial.println("  Button 3 LEFT");
      break;

    case btn4Min ... btn4Max:
      Serial.println("  Button 4 UP");
      break;

    case btn5Min ... btn5Max:
      Serial.println("  Button 5 SW");
      break;

    default:
      Serial.println("  Unknown button");
      break;
    }
  }

a7

I changed to one button, results are the same.

int botonState = 0;

void setup() {
  Serial.begin(115200);
  pinMode(14, INPUT_PULLUP);

}

void loop()
{

 // int adcValue = analogRead(36);
  // if ((adcValue >= btn5Min && adcValue <= btn5Max)&&adcValue > 20) {
  if (digitalRead(14) == 0) {

    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);
  }
}

Hi, @tom321
Try this simple code.

int botonState = 0;

void setup() {
  Serial.begin(115200);
  pinMode(14, INPUT_PULLUP);
}

void loop() {
  botonState = digitalRead(14);
  Serial.print(" Button State = ");
  Serial.println(botonState);
  delay(1000);
}

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

@camsysca suggested printing, as has @TomGeorge.

If you don't know how modulo (%) works, don't use it:

if (digitalRead(14) == 0) {

    botonState--;

    if (botonState < 0) {
        botonState = 4;
    }

    delay(200);
}

You shoukd also always have a default case in your switch statements, to catch those occasions where you are feeding the swtich w/ bad data.

HTH

a7

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

solved

const int btn5Min = 3100, btn5Max = 3300;
int botonState = 0;

void setup() {
  Serial.begin(115200); //

  pinMode(16, INPUT_PULLUP);  // sw
  pinMode(17, INPUT_PULLUP);  //
  pinMode(32, OUTPUT);
  digitalWrite(32, HIGH);

}

void loop() {
int adcValue = analogRead(36);

 // if (digitalRead(16) == 0)
  if ((adcValue >= btn5Min && adcValue <= btn5Max)&&adcValue > 20)
  {
    botonState = (botonState + 1) % 6;

    delay (200);
  }
  switch (botonState)
  {

    case 0: //

      {
        Serial.print("case 0 ");
        Serial.println( );
      }
      break;

    case 1: //


      {
        Serial.print("  case 1   ");
        Serial.println();
      }
      break;

    case 2: //
      {


        Serial.print("  case 2");
        Serial.println();
      }

      break;

    case 3: //


      {
        Serial.print("  case 3 =  ");
        Serial.println();
      }
      break;

    case 4: //
      {



        Serial.print("  case 4   ");
        Serial.println();
        break;

      }
  }
}

I think that's consistently going to land in case 1 regardless of button pressed ?

It looks like you are trying to do something like this:

https://www.instructables.com/Connecting-Multiple-Buttons-to-a-Single-Pin-on-Ard/

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.)

LOL.

I guess it was t important to you to go backwards.

botonState = (botonState - 1) % 5;

a7

Me neither 'til a minute or two. It's not standard until C++23, but we enjoy it here because GCC.

AI knows all:

a7

I have a button limit of 5, all of them are occupied

What was the solution?

Tom.... :smiley: :+1: :coffee: :australia:

To what does this refer? I am curious, but missing the obvious. Tnx.