I got the following code sample from user johnwasser in another thread on the same subject. I'd like to use this code in a project, but I need to take it a step further and make use of the millisecond(s) remainder that resulted from the initial division of millis by 1000, instead of rounding it up to the next second as was done below. I'm brand new to Arduino programming and have not been able to find a reference solution. Thanks in advance for any help!
unsigned long currentMillis = millis();
unsigned long seconds = currentMillis / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
unsigned long days = hours / 24;
currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
Thank you,
I attempted to implement your solution in the following manner, but the time exceeds the condition of the if statement at the bottom and does not toggle the LED on. I added the if statement to show what I'm trying to do with the time information.
unsigned long currentMillis = millis(); //unsigned long
unsigned long seconds = currentMillis / 1000; //unsigned long
unsigned long remainder = currentMillis -(seconds * 1000); //added 11/27/2021
unsigned long minutes = seconds / 60; //unsigned long
unsigned long hours = minutes / 60; //unsigned long
unsigned long days = hours / 24; //unsigned long
//currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
if(minutes == 3 && seconds == 36 && remainder == 476)
{
digitalWrite(ledPin, HIGH);
}
minutes has value 3
seconds has value 36
and remainder is calculated
unsigned long remainder = currentMillis -(seconds * 1000);
example numbers
currentMillis shall hold the number two-hundred-thousand 475 milliseconds
this means your sketch is running for 200 seconds three minutes and 20 seconds
200475 - ( 200 * 1000) = 475 this does not match == 476
200477 - ( 200 * 1000) = 477 this does not match == 476
Does your code check this if-condition faster than every millisecond ?
It is surely possible to check at this high speed.
post your complete sketch
And write down a description of the functionality that you want to have
write it in normal words to assure it is easy to understand
best regards Stefan
red_car,
You are correct! I found that I had to alter the if statement to say (currentMillis >476) in order to get the LED to turn on at a time close enough to the desired time. I played with the number in order to verify it's effect on the result. Thank you! I appear to be back in business.
unsigned long currentMillis = millis(); //unsigned long
unsigned long seconds = currentMillis / 1000; //unsigned long
unsigned long minutes = seconds / 60; //unsigned long
unsigned long hours = minutes / 60; //unsigned long
unsigned long days = hours / 24; //unsigned long
currentMillis %= 1000;
seconds %= 60;
minutes %= 60;
hours %= 24;
if(minutes == 3 && seconds == 36 && currentMillis >476)
{
digitalWrite(ledPin, HIGH);
}
Thank you for chiming in! One of the other responders has provided part of the solution with one minor addition by me. The code seems to be working for the moment.