5

So I am trying to use one input to control an LCD display. The goal being that once one of the buttons is pressed it delivers a message. I have watched quite a few tutorials and can get the program to work sometimes. Yet, it is still pretty buggy and I am wondering why that is. To get this make this project work somewhat used this code.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonPin = A0;     //Button and LED pin numbers
    void setup() 
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  //lcd.print("hello, world!");
  Serial.begin(9600);           //Serial monitor used to determine limit values

}

void loop() { int temp = analogRead(buttonPin); //Read the analogue input Serial.println(temp); //Display the read value in the Serial monitor if (temp = 340) //Lower limit for first button - if below this limit then no button is pushed and LEDs are turned off { lcd.print("No Button Pushed \n"); } else if (temp = 867) //First button limit - if below this limit but above previous limit then the first button is pressed { lcd.print("Button 1 Pushed"); } else if (temp = 156 ) //Second button limit { lcd.print("Button 2 Pushed"); }

else //If none of the previous buttons are pressed, then the third button must be pressed { lcd.print("Button 3 Pushed"); } delay(100); //Delay for stability }

The output sometimes is correct other times it is off. I think it has something to do with the circuit. Yet, I am scratching my head to find out what.

Circuit

2 Answers2

4

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.

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
3

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
}
Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58