LCD Responding to Buttons

Hi there,

My goal is to write a code that will display various LCD messages in response to the press of three different buttons.

If the first button is pressed, I want the screen to say “Forward”
If the second, “Backward”
If the third, “Auto”

I’ve been looking through similar feeds, yet everyone seems to recommend that a variable is created to recognize the change of state of a button. However, why is that better than just using an “if statement” and printing a message based on the state of the button, LOW or HIGH?

Right now, it responds to the buttons and prints the correct words, yet the screen is not cleared and it does not always print from the upper left corner. I’ve attached a photo of what this looks like. Additionally, there are non-letter characters that I haven’t seen before.

I’ve tested “Hello world” and other sample LCD codes. Everything non-button related has worked completely fine.

// Testing three buttons (holding) in tiny breadboard
// Naomi Durand, 7-1

const int autoPin = 13;
const int forwardPin = 11;
const int backwardPin = 12;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
LiquidCrystal lcd(1,2,3,4,5,6);

void setup() {
  // put your setup code here, to run once:

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Ready");

 pinMode(autoPin, INPUT_PULLUP);
 pinMode(forwardPin, INPUT_PULLUP);
 pinMode(backwardPin, INPUT_PULLUP);
 Serial.begin(9600);

}

void loop() {
  
  if(digitalRead(autoPin) == HIGH){
    lcd.clear();
    lcd.print("Auto");
    delay(100);
  }
    
  if(digitalRead(forwardPin) == HIGH){
    lcd.clear();
    lcd.print("Forward");
    delay(100);
  }
  if(digitalRead(backwardPin) == HIGH){
    lcd.clear();
    lcd.print("Backward");
    delay(100);
  }
  lcd.clear();

}

IMG_4024 (1).jpg

Hello ndurand212, Welcome to the forum.

It drives people on here nuts when new people can't be bothered to post their code correctly, so, for posting code in code tags ++Karma.

How have you wired your buttons?

Given that you are correctly using:

 pinMode(autoPin, INPUT_PULLUP);

What do you think each input state is when no button is pressed?

Once you have worked that out, what do you think this:

  if(digitalRead(autoPin) == HIGH){
    lcd.clear();
    lcd.print("Auto");
    delay(100);
  }

Does when no button is pressed?

Same question with all the other ifs...

When you have sorted that out time to move on to a state machine.

Do not use pin 1. :astonished:

Paul__B: Do not use pin 1. :astonished:

That's a pretty strong statement that deserves some explanation.

Don

Well, I figure it has a fair chance of solving her problem for one thing, but it has been over 24 hours now since her post.

Yes, of course it needs explanation. I figure if she ever returns to this forum - as some or many never do - I will be more than happy to discuss the pros and obvious cons of using pin 1.

So it's "tit or tat". Tell me whether that was or was not the problem, and I will tell why. :grinning:

But if she never comes back I’ll never get your explanation. I’m curious why you think it will solve her problem.

Don

I am reasonably sure you know why I made the statement, so I doubt you need my explanation. :grinning:

But putting it back to you, why do you suppose it will not solve her problem and what else do you think it might be? :roll_eyes:

Well generally the recommendation is to stay away from using pins 0 and 1 since they are used for serial communication.

This means that whatever is connected to either of those pins can possibly:

  • interfere with the uploading of the program
  • interfere with using the serial monitor It also means that the internal components used for uploading the program can possibly:

  • interfere with the operation of your device Well it looks like she has successfully uploaded her program since she is getting some legitimate output on her LCD.

She is not using the serial monitor.

She has pin 1 connected to RS and she has successfully been able to control the LCD enough to initialize it and subsequently send data. I suppose that any possible interference between the internal components and the RS pin could be hit and miss and that it could be causing her problem at some times and not at others.

I do not take exception to recommending that pin 1 not be used but I think that a blanket statement to not use that pin is inappropriate. At the same time I would also recommend not using pin 0.

Don

54 hours and counting ...

Will we ever know? :roll_eyes: