Making a simple LCD counter

Hey there,

I’m relatively new with Arduino’s and C++, so please bear with me. I’m working on a school project for an engineering course and we are currently using Tinkercad to make virtual circuits. Our current project is to use an arduino and an LCD to make a counter that displays the seconds, minutes, hours, and days since the program began running using millis().

I have my display working and numbers counting up, but my trouble is properly resetting seconds back to zero after it reaches 60. Here is my code:

#include <LiquidCrystal.h>

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

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  lcd.begin(16,2);
  
  Serial.begin(9600);
}

void loop()
{
  delay(500);
  
  int secs = millis()/1000;
  int mins = secs/60;
  int hrs = mins/60;
  int dys = hrs/24;  
  
  lcd.setCursor(0, 0);
  lcd.print(String(dys) + String(":days"));
  lcd.setCursor(8, 0);
  lcd.print(String(hrs) + String(":hours"));
  lcd.setCursor(0, 1);
  lcd.print(String(mins) + String(":mins"));
  lcd.setCursor(8, 1);
  lcd.print(String(secs) + String(":secs"));
  
  if (secs >= 60)
  {
    secs = 0;
    lcd.setCursor(9, 1);
    lcd.print(String(secs));
  }  
}

So as you can see, when seconds reach 60 the minutes go up by one, and when the minutes reach 60, the hours go up by one etc. I know that the if statement at the bottom doesn’t work, I’ve just been experimenting and trying whatever I can find online. So I just need each time to reset to zero after they have hit either 60 (for seconds and minutes) or 24 (for hours).

If anyone could give me any advice that would be very much appreciated, thanks!

Well, the obvious problem is that in your division chain, you are reducing your time to the total numbers of seconds, minutes, hours etc, rather than the number remaining relative to the larger units.

You need to research the remainder function.

Tinkercad? Blecch!

"String" functions will likely work on Tinkercad, but not on an actual Arduino. :astonished:

Hi and welcome.

Apart from the lack of long integers for your variables, there's no too much wrong with your code.

You are creating your numbers to display.
Then you display those numbers.
Next you do a check to see whether the seconds are overflowing, and handle that too.
Now you attempt to overwrite the already printed value, and you are successful at it, no doubt.

Your problem is that you see the seconds count up to 69, and after that jump to 10 seconds.
This is caused by not erasing the 6 from 60 which was already printed.
Add some (2) smart lines to the last part of your code to handle that.

Add a similar solution for the hours, it's missing in the code shown.

Is the String method really necessary ?

Tips: take a real good look at your display and try to analyse what you're exactly seeing agianst what you expected.
Perhaps you could have seen what went wrong yourself.

When asking questions like these, always include what you expected to see, and what you see instead.
It helps potential responders to sooner recognise (from experience, we all made mistakes in the past) which problem may be occurring.

Thanks for all the advice guys,

Used modulus to get my variables working properly (thanks Paul__B), and didn't have to worry about any if statements, thank you so much!