Loop Not Functioning as Required

I am on day2 of learning Arduino. Trying to diagnose but cannot.
Could someone please suggest some better code.

When I run the program, the LCD says “ON” (On the top row)
When I press the button the LCD still says “ON” on the top row and OFF on the bottom row
The screen just says ON and OFF and will not change with the button press after this.

I realize this is super simple but I am totally stumped.

#include <LiquidCrystal.h>

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

int switchstate = 0;
void setup() {
lcd.begin(16, 2);
pinMode(6, OUTPUT);
}

void loop() {
switchstate = digitalRead(6);
if (switchstate == HIGH) {
lcd.setCursor(0, 0);
lcd.print(“ON”);
}
else {
lcd.setCursor(0, 1);
lcd.print(“OFF”);
}
}

Clear LCD screen before each print.

Were Shaun0 to Shaun255876541868540 already taken?

:wink:

  pinMode(6, OUTPUT);
}
     
void loop() {
  switchstate = digitalRead(6);

In almost all cases, digital pins that you READ should be set to mode INPUT or INPUT_PULLUP. For simple buttons you should use INPUT_PULLUP and wire the button between the pin and Ground. When the circuit is open the internal pull-up resistor will cause the input to read HIGH/1/true. When the circuit is closed, the path to Ground overcomes the pull-up resistor and the input reads LOW/0/false.

Clear LCD screen before each print.

But do not use the lcd.clear() function. It is slow and can lead the screen to flicker in may situations.

Print blank spaces where you want to clear previously written data.

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

int switchstate = 0; 
void setup() {
  lcd.begin(16, 2);
  pinMode(6, OUTPUT);
}
     
void loop() {
  switchstate = digitalRead(6);
  if (switchstate == HIGH) {
  lcd.setCursor(0,1);
  lcd.print("   ");//clear three spaces with OFF
  lcd.setCursor(0, 0);
  lcd.print("ON");
   }
 else {
  lcd.setCursor(0,0);
  lcd.print("  ");//clear two spaces with ON
  lcd.setCursor(0, 1);
  lcd.print("OFF");
  }
}

Shaun255876541868541:
When I run the program, the LCD says "ON" (On the top row)
When I press the button the LCD still says "ON" on the top row and OFF on the bottom row
The screen just says ON and OFF and will not change with the button press after this.

Yes, well that is what you told it to do! Nothing more, nothing less.

You told it to write "ON" on the top row and then to write "OFF" on the bottom row. You did not tell it not to write on either row; to do that you have to tell it to write something else on top of what it wrote before.

It would make much more sense to tell it to write either "ON " or "OFF" in the same place. Note I said "ON " with a space after to make it three characters because "OFF" is three characters, otherwise it will in fact write "ONF" when you tell it to write "ON".

So that is the obvious bit; what is not so obvious is to write code to make it write something only when it needs to be changed otherwise you are writing to the display too quickly and it will flicker and/ or show faded characters as it tries to re-write it too fast.

Noted johnwasser's explanation about the pushbutton.

alesam:
Clear LCD screen before each print.

Absolutely not!

Very bad suggestion. :astonished:

change:

  switchstate = digitalRead(6);
  if (switchstate == HIGH) {
  lcd.setCursor(0, 0);
  lcd.print("ON");
   }
 else {
  lcd.setCursor(0, 1);
  lcd.print("OFF");
  }
}

TO:
switchstate = digitalRead(6);
if (switchstate == HIGH) {
lcd.setCursor(0, 0);
lcd.print("ON");
lcd.setCursor(0, 1);
lcd.print(" "); //Tre empty space
}
else {
lcd.setCursor(0, 0);
lcd.print(" ");//Two empty space
lcd.setCursor(0, 1);
lcd.print("OFF");
}
}