Trying 5 buttons with analog input

Hi to all, this is my first post here :slight_smile:
I'm trying to read wich button is pressed (of five) with an analog input... here's the circuit I'm using:


but I'm using 5 buttons and only 1k? resistors (instead of 1 and 2 k?, like in the image)

and here's the code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int b=0;
  int sensorValue = analogRead(0);
  if (sensorValue<505)
    {
      b=0;
    }
  else if (sensorValue>505 && sensorValue<515)
        {
          b=1;
        }
      else if (sensorValue>675 && sensorValue<685)
            {
              b=2;
            }
          else if (sensorValue>760 && sensorValue<780)
              {
                b=3;
              }
              else if (sensorValue>815 && sensorValue<825)
                  {
                    b=4;
                  }
                  else if (sensorValue>850 && sensorValue<860)
                      {
                        b=5;
                      }
  if (b=0)
    { 
      Serial.println("No button pressed");
    }
  else
    {
      Serial.print("Button "); Serial.print(b); Serial.println(" pressed");
    }
  delay(1000);
}

but what the only thing I get is:

"Button 0 pressed"

:frowning:

p.s.

I set the values using this example code:

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

thank you :slight_smile:

 if (b=0)
    { 
      Serial.println("No button pressed");
    }
  else
    {
      Serial.print("Button "); Serial.print(b); Serial.println(" pressed");
    }

You mean "if (b==0)", not "if (b=0)". As it's written it sets 'b' to zero which is always false so it prints "Button 0 pressed".

ehehe I'm sorry for being so weird... thank you very much! :smiley:

FYI:

That circuit as shown forms what is known as an "R/2R" ladder - a basic DAC circuit. It relies on the ratio between 1 resistance and double it's resistance (10K? / 20K?) to give good stable results and the ability to detect multiple concurrent button presses.

Using resistors of all the same value (the actual value is irrelevant) will not give such good results as multiple concurrent button presses will give skewed values.

  if (sensorValue<505)
    {
      b=0;
    }
  else if (sensorValue>505 && sensorValue<515)
        {
          b=1;
        }

If the sensorValue is 505 what happens? You should not leave gaps. If the value is less than 505, the test for it being greater than (or equal, when you get the test right) is not needed.

  if (sensorValue<505)
    {
      b=0;
    }
  else if (sensorValue<515)
        {
          b=1;
        }

And don't indent each block more.

@majenko

I don't need to detect more than one button at the same time, but thank you very much for the explanation (anyway I have to buy 2k? resistors, I'm just starting)

@PaulS

I don't need to consider the value 505, because it shouldn't "happen", and even if it happens it doesn't matter... when I push a button I generally get a precise value wich is in the middle of range (for example 510 is between 505 and 515) but circuits aren't perfect so I gave the range.
For: "If the value is less than 505, the test for it being greater than isn't needed" you're right! :slight_smile:

Thank you very much guys

I don't need to consider the value 505, because it shouldn't "happen", and even if it happens it doesn't matter

There is a world of difference between shouldn't happen and won't happen. The resistors you are using. What tolerance are they? Generally, they are +/- 10% of the rated resistance. A 2k ohm resistor can measure anything between 1800 and 2200. That WILL affect the output from analogRead when a given voltage is applied.

I don't understand the "it doesn't matter" part. A reading of 505 doesn't correspond to no switch pushed. It means that some switch was pushed. The key is determining which one.

It is 5% rated. Anyway you're right... I'm going to correct the code as you told me, I think it's a better solution :slight_smile: thank you