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

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

4
  • Ok so if I wanted to get a well-defined voltage reading then I would tie the rightmost button to 5v Correct? Oct 5 '20 at 7:54
  • Good point, I stopped looking after I saw the problem in the code. Guess a combination of your and my solution is needed (doing your solution first). Oct 5 '20 at 7:55
  • So I am going to add in another picture which is the reason for my confusion Oct 5 '20 at 7:57
  • I understand now if I tie it low then I can get nice increasing values. Thanks for the help it really helped a bunch Oct 5 '20 at 8:04
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
}
2
  • Ok so that fixes some issues but the buttons are not giving nice values like that. When no buttons are pushed A0 is reading in 340 and the first button gives a value of 860. This makes me question the circuit itself. Oct 5 '20 at 7:46
  • Yes, you first need to fix the problem that is solved by Edgar, then using my solution once you know the correct value ranges. Oct 5 '20 at 7:56

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .