Multiple Push buttons to one input

Michel Keijzers pointed out the issues you have in your code. But then there is also an issue with your circuit: if you press any of the push buttons, the analog input pin gets connected to one of the nodes of the resistor ladder, and you get a well defined voltage reading. Good. However, if none of the buttons is pressed, the input is not connected to anything: it is floating. In this case the reading you get is unpredictable, as it depends on parasitic influences of stray capacitances, environmental noise, etc. You should modify your circuit to make sure that the input voltage is well defined even when no button is pressed.

Then, regarding the program, I recommend you remove everything after Serial.println(temp);. Just make it work like this, and take note of the readings you get in each case. Make sure they are consistent. Once you know the values you expect, you can decide on the threshold to apply, which should be about halfway between the typical readings. E.g. if the two lowest values you get are 0 and 240, you would set a threshold at 120.


You made a few mistakes with the three lines, first one being:

if (temp = 340)   

First, the single '=' is an assignment operator instead of a comparing operator. You want to use '=='.

However, you need to check if it is within a range, so you need to use <= (to see if it is smaller or equal). Also you need to put them in the correct order, so you get something like:

if (temp <= 156)
{
   // Process value if temp is smaller or equal than 156
}
else if (temp <= 340)
{
   // Process value if temp is smaller or equal than 340 (and higher than 156)
}
else if (temp <= 860)
{
   // Process value if temp is smaller or equal than 860(and higher than 340)
}
else 
{
   // Process value if temp is higher than 860
}

Tags:

Input

Lcd

Analog