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:
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int secs = millis()/1000;
int mins = secs/60;
int hrs = mins/60;
int dys = hrs/24;
lcd.print(String(dys) + String(":days"));
lcd.print(String(hrs) + String(":hours"));
lcd.print(String(mins) + String(":mins"));
lcd.print(String(secs) + String(":secs"));
if (secs >= 60)
secs = 0;
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.
"String" functions will likely work on Tinkercad, but not on an actual Arduino.
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!
This topic was automatically closed after 74 days. New replies are no longer allowed.