Problem with millis()

Hello arduino giants out there. This a humble doubt form a newbie.
OK to start with, I am using a normal arduino UNO board and I wanted to turn on/off a latching solenoid valve for say 1 hour duration at a frequency of one day.
And the Millis() came in handy to implement this without any fuss. But unfortunately from my observations the arduino millis() is not working after 7.2 hours.
I wanted to test the millis() and i developed a code where in I just diplayed the millis() value on a LCD.Here goes my code

#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long val=0;
void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(4,0);
  lcd.print("TIME:");
  lcd.display();
}

void loop() 
{
  lcd.setCursor(0,2);
  val=millis();
  lcd.print(val);
  lcd.display();
 }

I have also attached a photo of the lcd screen when it got stuck.
My question is, did the lcd get stuck by any means or the controller got stuck. Or can anybody explain me what happened. Even in the worst case of an overflow, the values should have at least been reset to 0. But nothing logical happened.

I request you friends to please help me with a fix and suggest me any reliable alternatives if any.

Thanks in advance.

Does lcd.print() handle long integers properly? I suspect that it doesn't and it's trying to use an int version of that function. The question mark in the middle of the numbers looks very odd. What could put a question mark there?

You sketch does not match with what is shown in the display. You have used a different sketch for that :smiley_cat:

The first row is '0' and the second row is row '1', you use '2'.

By the way, millis() is working good. It rolls over after 50 days. https://www.arduino.cc/en/Reference/Millis A rollover problem can be avoided when unsigned long variables are used for millis() and it is programmed the right way. It means that you can do the hour delay every day, and will work perfect even when millis() does a rollover.

(While I was typing this, MorganS suspects that lcd.print might not print unsigned long very well. The LiquidCrystal library uses the Print class, which is also used by Serial.print. It should print an unsigned long without problem).

The demo Several Things at a Time illustrates the use of millis()

...R

Robin2: The demo Several Things at a Time illustrates the use of millis()

...R

As does the millis() tutorial on the documentation page. But it only demonstrates "one thing at a time".

Guys I appreciate your response. But I think I have found something new to bother your minds. When i use an arduino standlaone with its internal RC at 1MHz, and the same program, for the past 74 hours, the program is running as smooth as a ferrari with no glitches. But when ever i used the external clock(16 MHz) for timing, roughly after 9 hours I get a problem. Can any one give a reasonable justification.

And for your kind information, I have used the same conventional millis() formula. i.e. current -previous. to tackle the overflow issues. Yet I have tried it for nearly six or seven times, I progrmmed the arduino to turn on a relay after 12 hours. But all the six times, it was turned on roughly at 9 hours it self.

And also to add pepper to the soup, the same relay(program) works fine if i program the arduino within 9 hours delay.

Its really strange, there must be some valid reason for this. I am pretty exhausted after hunting down reasons, but none are satisfying.

Please share your experience and suggestions guys.

Thanks in advance.

Without seeing your work as it is now, it is quite impossible to help you.

since you do nothing with val, why not just print millis() ?

#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long val=0;
void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(4,0);
  lcd.print("TIME:");
  lcd.display();
}

void loop() 
{
  lcd.setCursor(0,2);
  //  ===================== commented out  val=millis();
  lcd.print(millis());
  lcd.display();
 }

ji_bala94: And for your kind information, I have used the same conventional millis() formula. i.e. current -previous. to tackle the overflow issues. Yet I have tried it for nearly six or seven times, I progrmmed the arduino to turn on a relay after 12 hours. But all the six times, it was turned on roughly at 9 hours it self.

And also to add pepper to the soup, the same relay(program) works fine if i program the arduino within 9 hours delay.

Its really strange, there must be some valid reason for this. I am pretty exhausted after hunting down reasons, but none are satisfying.

Please share your experience and suggestions guys.

Thanks in advance.

The code you posted only prints time to a screen. But you keep asking about code that has something to do with a relay. Can you show us the code that you actually want help with? If we can't see it, we can't help.