Push button print text to LCD.

Ok, this is some really basic stuff but I am trying to learn Arduino by pasting together stuff I learned in the different tutorials.

This is what I want Arduino to do.

When I press my button (HIGH) the LCD text should say “Knappen aro nedtryckt!”
If it is not pressed (LOW), it should say “Tryck pa knappen!”
However, right now it displays both messages at the same time, flashing in between the overlaying text. When I do press the button the message I want to display is showing steady (flashing stopped), when I let go the flashing between the two messages start again.
How do I get it to not execute the “if” text line when the button is LOW?

Here is my code.

#include <LiquidCrystal.h>

const int knapp=8;
int buttonState = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(knapp, INPUT);
  
  lcd.begin(16, 2);
}

void loop() {
 buttonState = digitalRead(knapp);
  if (buttonState == HIGH){   // This is executed even if the button is NOT pressed.
    lcd.setCursor(0, 0);
    lcd.print("Knappen aro");
    lcd.setCursor(0, 1);     // Here I changed so the text is displayed on the next row.
    lcd.print("nedtryckt!");
  }
  else {                             
  lcd.setCursor(0, 1);
    lcd.print("Tryck pa knappen!");  
  }
  

}

Like I said very basic, but I am trying to learn. :stuck_out_tongue:

How is your switch wired? You're not using the built-in pullups. It sounds like you have a floating input.

AWOL:
How is your switch wired?
You’re not using the built-in pullups.
It sounds like you have a floating input.

I have the button connected from 5V to digital pin 8, with a 10K resistor. Like in this tutorial, but without the led.

The button is a push-button which makes a connection when pushed.

I see now that I forgot to add the ground. :0

Well now it displays "Tryck på knappen" (else command) But not the HIGH command even when the button is pushed.

OK, for now scrap the LCD stuff and just write a simple sketch to print the state of the input pin to the serial monitor.

AWOL: OK, for now scrap the LCD stuff and just write a simple sketch to print the state of the input pin to the serial monitor.

Roger that. ;)

Ok, here goes.

const int knapp=8;
int buttonState=0;

void setup(){
  pinMode(knapp, INPUT);
  Serial.begin(9600);
  
}

void loop() {
  buttonState = digitalRead(knapp);
  if (buttonState == HIGH){
    Serial.println("HELLO!");
  }
  else {
    Serial.println("NOPE!");
    delay(100);
  }
}

Is this correct code?

This is what happens.

[u][u]NOPE is spammed.. UNTILL, I press the button, then is pauses for as long as the button is pressed. Hello is never shown.[/u][/u]

EDIT: It is working. One of the little cables had jumped out when I was messing about. Nope is shown when the button is depressed, HELLO is shown when the button is pressed. So that seems to work. ^_^ I tried the LCD sketch again but it didnt work. :/

  if (buttonState == HIGH){
    Serial.println("HELLO!");
  }
  else {
    Serial.println("NOPE!");
    delay(100);
  }

Delay() only if the switch is not pressed. Why?

Move the delay() out of the else block.

Then, unplug all the crap involved with the switch. Connect one side to ground. Connect the other side to the digital pin. Add a digitalWrite() statement, after the pinMode() statement (for the switch pin) to set the pin HIGH. That turns on the internal pullup resistor, making wiring switches so much easier.

PaulS:   if (buttonState == HIGH){     Serial.println("HELLO!");   }   else {     Serial.println("NOPE!");     delay(100);   }

Delay() only if the switch is not pressed. Why?

Move the delay() out of the else block.

Then, unplug all the crap involved with the switch. Connect one side to ground. Connect the other side to the digital pin. Add a digitalWrite() statement, after the pinMode() statement (for the switch pin) to set the pin HIGH. That turns on the internal pullup resistor, making wiring switches so much easier.

Delay because it was spamming so fast I couldnt see what was going on. I added it before I changed to println then forgot to remove the delay. Sorry about that, see my update it is working now, but my LCD text is still showing dual messages on the screen.

I will try the pullup, but I the switch as it is now seems to be working.

but my LCD text is still showing dual messages on the screen.

You are outputting text to 2 different lines on the LCD and not clearing the previous text. Clear the LCD or just the previous text before printing the new text.

UKHeliBob:

but my LCD text is still showing dual messages on the screen.

You are outputting text to 2 different lines on the LCD and not clearing the previous text. Clear the LCD or just the previous text before printing the new text.

Doh.. Didnt think about that.

It seems to be working, but did I place it in the correct place?

void loop() {
 buttonState = digitalRead(knapp);
  if (buttonState == HIGH){
    //lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Knappen aro");
    lcd.setCursor(0, 1);
    lcd.print("nedtryckt!");
  }
  else {
  
  lcd.setCursor(0, 1);
    lcd.print("Tryck pa knappen!");
    
  }
  delay(500);
  lcd.clear();
}

Also I had to add a delay to get rid of the strobing effect when the screen was refreshing. Any other way to do it?

Only clear the display and output the text when the button state has changed. That way the output only happens once, not every time through loop().

UKHeliBob:
Only clear the display and output the text when the button state has changed. That way the output only happens once, not every time through loop().

I should probably know this, but can you point me in the right direction? :slight_smile:
How do I get it to run only once and when the button has changed?

Thanks to UKHeliBob, PaulS and AWOL for helping me out. I was on the right track but a bit confused. But that is how we learn. :wink:

I should probably know this, but can you point me in the right direction?

You know which function is clearing the screen, right? You want that in the if block and in the else block.

What you really need to do is look at the state change detection example, to see how to print to the LCD only when the switch state CHANGES.

PaulS:

I should probably know this, but can you point me in the right direction?

You know which function is clearing the screen, right? You want that in the if block and in the else block.

What you really need to do is look at the state change detection example, to see how to print to the LCD only when the switch state CHANGES.

  1. Yes, lcd.clear() .. But if I put it in the if and else block it runs non stop (causes the screen to strobe) unless I put a delay on it.

  2. I haven't seen that tutorial, this is only a first attempt at doing this. That seems like a better way to do it. :)

Here a suggestion that works for one button.
I could not get it to work for more Thane one button. :slight_smile:
Button is set high by 1 kohm resistor to ground.
Notice the spaces in the:
("Knapp er Tryckt ");}
("Tryck pa knappen ");}
print, they delete leftover from Prev print.

// This constant won't change:
const int knapp = 8;

// Variables will change:
int ButtonState = 0;         // current state of the button
int oldButtonState = 0;     // previous state of the button


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(knapp, INPUT);
    digitalWrite(knapp, LOW);   

  
 lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.clear(); 
  lcd.setCursor(0, 0);
  delay (11);
  lcd.print("");
  delay (11);
  lcd.setCursor(0, 1);
  delay (11);
  lcd.print("");
  delay (11);
    lcd.clear();
}

void loop() {


ButtonState = digitalRead(knapp);



//### The Button
  if (ButtonState != oldButtonState) {
    if (ButtonState == HIGH) {

   lcd.setCursor(0,1);
     delay (11);
     lcd.print("Knapp er Tryckt   ");}
    } 
    else {
   lcd.setCursor(0,1);
     delay (11);
     lcd.print("Tryck pa knappen   ");}         
}

i addded more buttons in the code but its not working for the forth one and before giving the result on lcd the text is shuttering.

can you make the code small by using loops.
AIM:- i want different outputs on lcd for every button pressed.

i am new to arduino coding sorry for not using loops…

// This constant won’t change:
const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int led4 = 11;

// Variables will change:
int ButtonState = 0;
int ButtonState1 = 0;
int ButtonState2 = 0;
int ButtonState3 = 0;// current state of the button
int oldButtonState = 0;
int oldButtonState1 = 0;
int oldButtonState2 = 0;
int oldButtonState3 = 0; // previous state of the button

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
pinMode(led1, INPUT);
pinMode(led2, INPUT);
pinMode(led3, INPUT);
pinMode(led4, INPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);

Serial.begin(9600);
lcd.begin(16, 2);

}

void loop(){
{

ButtonState = digitalRead(led1);

//### The Button
if (ButtonState != oldButtonState) {
if (ButtonState == HIGH) {
lcd.clear();
lcd.setCursor(0,1);
delay (10);
lcd.print(“relay 1”);}
}
}

{

ButtonState1 = digitalRead(led2);

//### The Button
if (ButtonState1 != oldButtonState1) {
if (ButtonState1 == HIGH) {
lcd.clear();
lcd.setCursor(0,1);
delay (11);
lcd.print("relay 2 ");}

}
}

{

ButtonState2 = digitalRead(led3);

//### The Button
if (ButtonState2 != oldButtonState2) {
if (ButtonState2 == HIGH) {
lcd.clear();
lcd.setCursor(0,1);
delay (11);
lcd.print(“relay 3”);}

}
}

{
ButtonState3 = digitalRead(led4);

//### The Button
if (ButtonState3 != oldButtonState3) {
if (ButtonState == HIGH) {
lcd.clear();
lcd.setCursor(0,1);
delay (11);
lcd.print(“relay 4”);}

}
}
}

The code is crying out for the input pin numbers, their states, the LED pin numbers and messages to print to be put in arrays or even betteran array of structs. Doing that would allow you to iterate through the array to determine which, if any, button was currently pressed or had become pressed and to use its array index (the for loop variable) to access the associated message to be printed.

ButtonState = digitalRead(led1);

Why are you reading the state of a pin called led1, and calling the value ButtonState?

Here's where you're dealing with the fourth button.

I think that should probably be ButtonState3 in the line where I added the comment.

ButtonState3 = digitalRead(led4);

//### The Button
   if (ButtonState3 != oldButtonState3) 
   {
      if (ButtonState == HIGH) // Check this line
      {
         lcd.clear();
         lcd.setCursor(0,1);
         delay (11);
         lcd.print("relay 4");
      }
   }

PaulS: ButtonState = digitalRead(led1);

Why are you reading the state of a pin called led1, and calling the value ButtonState?

I am new to Arduino I just modified the code.