I do not want the void loop to constantly repeat the text to my display

Hi, I am trying to work with a lcd screen, pushbutton, and LED. When I upload my code the "Button is waiting to be pushed" text constantly repeats itself. I was hoping someone could help me make it so it only displays once. When the button gets pressed it should switch the text to "Button has been pressed, LED should be on" and when it is not pressed go back to "Button is waiting to be pushed. I hope I posted this correctly as it is my first time posting to the forum. Thank you.


#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>

LCDWIKI_KBV my_lcd(ILI9486, A3, A2, A1, A0, A4);

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define BUTTON_PIN 53
#define LED_PIN 51




void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  my_lcd.Init_LCD();
  Serial.println(my_lcd.Read_ID(), HEX);

  // Welcome code for the first screen shown
  my_lcd.Fill_Screen(RED);
  my_lcd.Set_Text_colour(CYAN);
  my_lcd.Set_Text_Size(3);
  my_lcd.Print_String("Hello Joey!", 65, 40);

  delay(1000);

  // Another test of the screen chage
  my_lcd.Set_Text_colour(CYAN);
  my_lcd.Set_Text_Size(3);
  my_lcd.Print_String("Senior Design", 35, 75);
  delay(1000);



  // Setting pin devices for the project
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}



void loop() {
  // put your main code here, to run repeatedly:

  // Looks to see if the button is pushed and turns on the led
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    my_lcd.Set_Text_colour(CYAN);
    my_lcd.Set_Text_Size(2);
    my_lcd.Print_String("Button has been "
                        "\npushed, LED should be on",
                        5, 115);
  }
  // Looks to see if the button is not pushed, displays the button is ready to be pushed
  else {
    digitalWrite(LED_PIN, LOW);
    my_lcd.Fill_Screen(RED);
    my_lcd.Set_Text_colour(CYAN);
    my_lcd.Set_Text_Size(2);
    my_lcd.Print_String("Button is waiting"
                        "\nto be pushed",
                        5, 115);
  }
}

need to keep track of the button last state..

#include <LCDWIKI_GUI.h>
#include <LCDWIKI_KBV.h>

LCDWIKI_KBV my_lcd(ILI9486, A3, A2, A1, A0, A4);

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define BUTTON_PIN 53
#define LED_PIN 51

byte ButtonLastState = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  my_lcd.Init_LCD();
  Serial.println(my_lcd.Read_ID(), HEX);

  // Welcome code for the first screen shown
  my_lcd.Fill_Screen(RED);
  my_lcd.Set_Text_colour(CYAN);
  my_lcd.Set_Text_Size(3);
  my_lcd.Print_String("Hello Joey!", 65, 40);

  delay(1000);

  // Another test of the screen chage
  my_lcd.Set_Text_colour(CYAN);
  my_lcd.Set_Text_Size(3);
  my_lcd.Print_String("Senior Design", 35, 75);
  delay(1000);



  // Setting pin devices for the project
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}



void loop() {
  // put your main code here, to run repeatedly:

  // Looks to see if the button is pushed and turns on the led
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    if (ButtonLastState!=1){
      ButtonLastState = 1;
    my_lcd.Set_Text_colour(CYAN);
    my_lcd.Set_Text_Size(2);
    my_lcd.Print_String("Button has been "
                        "\npushed, LED should be on",
                        5, 115);
    }
  }
  // Looks to see if the button is not pushed, displays the button is ready to be pushed
  else {
    digitalWrite(LED_PIN, LOW);
    if (ButtonLastState != 0){
      ButtonLastState = 0;
    my_lcd.Fill_Screen(RED);
    my_lcd.Set_Text_colour(CYAN);
    my_lcd.Set_Text_Size(2);
    my_lcd.Print_String("Button is waiting"
                        "\nto be pushed",
                        5, 115);
    }
  }
}
2 Likes

Welcome to the forum.

Instead of looking at the state of a button, you can do something in the event of pressing or releasing a button.

See the State Change Detection example: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection

There are also libraries that debounce a button and also do the State Change Detection.

You can also find it in the Arduino IDE:

1 Like

Thank you both for the help, I got it solved!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.