Weird LCD display

Hi

I have written down the program as below in order to blink led on 30 s and led off 30s.But after some time I see that LCD display shows a negative value.Please advise.

This sketch prints counter value to the LCD and switching on and off for the relay.

The circuit:

  • LCD RS pin to digital pin 8
  • LCD Enable pin to digital pin 9
  • LCD D4 pin to digital pin 4
  • LCD D5 pin to digital pin 5
  • LCD D6 pin to digital pin 6
  • LCD D7 pin to digital pin 7
  • Relay A1 pin to digital pin 15

*/

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

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
lcd.begin(16, 2);
pinMode(15, OUTPUT);
// set cursor position to start of first line on the LCD

}

void loop() {
lcd.clear();
lcd.setCursor(0,0);
//text to print
lcd.print(" COUNTER");
delay(3000);
int a=0;
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(a);
while(a<=999999)
{
a=a+1;
delay(30000);
digitalWrite(15, HIGH);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(a);
a=a+1;
delay(30000);
digitalWrite(15, LOW);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(a);
}
}

Hi, welcome to the forum. Please have a look at How to use the forum and edit you post accordingly.

And for the problem, have a look at the reference for int, especially the values it can hold.

And as for solving it, remove the while-loop. You already have loop() which is looping just fine :slight_smile:

All explained here;