LCD menu

Hello,

I am making a menu to control different LED’s however when the name of each led pops up as you scroll through the menu it needs to clear for the next one, is there a way I can clear every time a button is pushed or anything like this?

#include <LiquidCrystal.h>

const byte  buttonPin3 = 10;
const byte  buttonPin2 = 7;
const byte  buttonPin1 = 6;
const byte ledPin = 13;
const byte ledPin2 = 8;
const byte ledPin3 = 9;
byte buttonPushCounter = 1;   
byte buttonState1 = 0;
byte buttonState2 = 0;
byte buttonState3 = 0;
byte Ledstate = 0;
byte Ledstate2 = 0;
byte Ledstate3 = 0;
byte lastButtonState1 = 0;
byte lastButtonState2 = 0; 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  lcd.print("LED Menu");
  lcd.setCursor(0,1);
}

void loop() 
//This part is to set up a count to conrol the basic menu system
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 != lastButtonState1) 
  {
    if (buttonState1 == HIGH) 
    {
      buttonPushCounter++;
      if(buttonPushCounter > 4)
      buttonPushCounter = 1;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);  
    } 
    else 
    {
      Serial.println("off"); 
    }
  }
  lastButtonState1 = buttonState1;
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 != lastButtonState2) 
  {
    if (buttonState2 == HIGH)
    {
      if(buttonPushCounter > 1)
      buttonPushCounter--;
            else
      {
   buttonPushCounter = 4;
    }
    }
  }
  //This section controlls what the button will do when pressed on the selection option
  lastButtonState2 = buttonState2; 
  buttonState3 = digitalRead(buttonPin3);
  Ledstate = digitalRead(ledPin);
  if (buttonPushCounter == 1 && buttonState3 == HIGH && Ledstate == LOW)
  {
    digitalWrite(ledPin, HIGH);
    delay (200);
  }
 buttonState3 = digitalRead(buttonPin3);
  Ledstate = digitalRead(ledPin);
  if (buttonPushCounter == 1 && buttonState3 == HIGH && Ledstate == HIGH)
{
    digitalWrite(ledPin, LOW);
    delay (200);
  }
  buttonState3 = digitalRead(buttonPin3);
  Ledstate2 = digitalRead(ledPin2);
  if (buttonPushCounter == 2 && buttonState3 == HIGH && Ledstate2 == LOW)
  {
    digitalWrite(ledPin2, HIGH);
    lcd.setCursor(7,1);
    lcd.print("LED TWO");
    delay (200);
  }
 buttonState3 = digitalRead(buttonPin3);
  Ledstate2 = digitalRead(ledPin2);
  if (buttonPushCounter == 2 && buttonState3 == HIGH && Ledstate2 == HIGH)
{
    digitalWrite(ledPin2, LOW);
    delay (200);
  }
 buttonState3 = digitalRead(buttonPin3);
  Ledstate3 = digitalRead(ledPin3);
  if (buttonPushCounter == 3 && buttonState3 == HIGH && Ledstate3 == LOW)
  {
    digitalWrite(ledPin3, HIGH);
    delay (200);
  }
 buttonState3 = digitalRead(buttonPin3);
  Ledstate3 = digitalRead(ledPin3);
  if (buttonPushCounter == 3 && buttonState3 == HIGH && Ledstate3 == HIGH)
{
    digitalWrite(ledPin3, LOW);
    delay (200);
  }
// this part is job is to show different this on the screen when scrolling on the menu
 if (buttonPushCounter == 1)
 {
   lcd.setCursor(7,1);
      lcd.print("LED ONE");
 }
  if (buttonPushCounter == 2)
 {
   lcd.setCursor(7,1);
      lcd.print("LED TWO");
 }
  if (buttonPushCounter == 3)
 {
   lcd.setCursor(7,1);
      lcd.print("LED THREE");
 }
  if (buttonPushCounter == 4)
 {
   lcd.setCursor(7,1);
      lcd.print("LED FOUR");
 }
}

You can write spaces to wipe the line or part of it. Set the cursor one the line and character position print spaces to erase the number of character needed. Reset the cursor back and print your data. This is much faster than doing LCD,clear().

it needs to clear for the next one

It? What is it? The next what?

is there a way I can clear every time a button is pushed or anything like this?

You have our permission to do that.

When scrolling through the LED’s the screen will come up with LED One, LED Two etc, however I want it the clear the last state of the menu before moving on to the next one,

and if i where to use spacers to over write what was last shown on the screen will i have to use a delay? for example

 {
   lcd.setCursor(7,1);
      lcd.print("              ");
Delay (100)
   lcd.setCursor(7,1);
      lcd.print("LED FOUR");
 }

however I want it the clear the last state of the menu before moving on to the next one,

The last "state of the menu" and the last line shown on the LCD are two different things.

and if i where to use spacers to over write what was last shown on the screen will i have to use a delay?

No. Certainly not one with a capital D and no semicolon.

I was just doing a quick example Paul :stuck_out_tongue: So could I have the same code but with no delay? and I meant to write the last line shown on the display :slight_smile:

could I have the same code but with no delay?

You do not need to wait after writing the spaces before writing the next string in the same place.

You do need to leave the string in place for a while, though.

what do you mean by "leave the string in place"? and how do i do this? :slight_smile:

so I changed the code and now it just keeps flashing between the two :stuck_out_tongue:

// this part is job is to show different this on the screen when scrolling on the menu
 if (buttonPushCounter == 1)
 {
   lcd.setCursor(7,1);
      lcd.print("          ");
   lcd.setCursor(7,1);
      lcd.print("LED ONE");
 }
  if (buttonPushCounter == 2)
 {
   lcd.setCursor(7,1);
      lcd.print("          ");
   lcd.setCursor(7,1);
      lcd.print("LED TWO");
 }
  if (buttonPushCounter == 3)
 {
   lcd.setCursor(7,1);
      lcd.print("          ");
   lcd.setCursor(7,1);
      lcd.print("LED THREE");
 }
  if (buttonPushCounter == 4)
 {
   lcd.setCursor(7,1);
      lcd.print("          ");
   lcd.setCursor(7,1);
      lcd.print("LED FOUR");
 }
}

so I changed the code and now it just keeps flashing between the two

I don’t know what you mean.

In my new bit of code where it deletes the previous shown text before the new one it keeps bouncing back and forth, so I need a way to make it stick to the second park of the code.

// this part is job is to show different this on the screen when scrolling on the menu
 if (buttonPushCounter == 1)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED ONE");
 }
  if (buttonPushCounter == 2)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED TWO");
 }
  if (buttonPushCounter == 3)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED THREE");
 }
  if (buttonPushCounter == 4)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED FOUR");
 }

Do you guys know what the correct syntax is?

The second of four parts? Only you can actually see what is happening. Describe exactly what two things you see when "it bounces back and forth".

It would be useful to know how the switches are wired, so we don't assume "incorrectly", and attribute your problems to that and move on.

so I wrote the code so when the button is pressed and the count changes it will wipe every thing and then read the value and display what it is told, so if you look at the code I posted it will display a bunch of spaces to clear the perviously shown text and then strait after it will show the new text. the problem is when the count is lets say 1 it will first display the spacers then "LEDONE" now I need it to stop on "LEDONE" instead it keeps jumping between the two. I need to know how to hold it on "LEDONE" till the count is changed?

 if (buttonPushCounter == 1)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED ONE");
 }
  if (buttonPushCounter == 2)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED TWO");
 }
  if (buttonPushCounter == 3)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED THREE");
 }
  if (buttonPushCounter == 4)
 {
   lcd.setCursor(0,0);
      lcd.print("          ");
   lcd.setCursor(0,0);
      lcd.print("LED FOUR");

the problem is when the count is lets say 1 it will first display the spacers then "LEDONE" now I need it to stop on "LEDONE" instead it keeps jumping between the two.

You need to deal with your frustration in some other way. Pissing and moaning does not help here. You need to be VERY specific as to what "jumping between the two" means.

If you mean that the spaces show while the cursor is being repositioned, and then the text shows, while loop() iterates and the cursor is repositioned, then the simple solution is to write the spaces only when the value of buttonPushCounter changes.

I'm not annoyed Paul :stuck_out_tongue: Neither am I moaning just trying to find the best way to describe my problem so I can get the most relevant advice :slight_smile: I will try writing the spaces every time the button is pressed for changing the count.