/* Ahmed Arduino Clock Instructable Step 1 - Software Clock First, we use the millis() command to track the number of milliseconds since power-on or reset. When 1000 ms passes, we increment the seconds. When 60 seconds pass, we increment the minutes, etc. You can set the hour and minute in the sketch, but after reset or power loss, the current time will be lost and revert to the set time. This method is accurate to about a minute per day. MakersBox.blogspot.com 648.ken@gmail.com */ // time variables long lastMillis; int hours = 0; int minutes = 0; int seconds = 0; void setup() { Serial.begin(9600); // Open channel to serial console Serial.println("Arduino Clock Step 1 - Software Clock"); } void loop() { long curMillis = millis(); // get current counter in milliseconds if (curMillis > (lastMillis + 1000)){ // If one second has passed: lastMillis = lastMillis + 1000; seconds = seconds + 1; // increment seconds if (seconds > 59){ seconds = 0; minutes = minutes + 1; // increment minutes if (minutes > 59){ minutes = 0; hours = hours + 1; // increment hours if (hours > 23){ hours = 0; } } } // Display time on serial console if (hours < 10){ Serial.print(" "); // pad hours } Serial.print(hours); Serial.print(":"); if (minutes < 10){ Serial.print("0"); // pad minutes} } Serial.print(minutes); Serial.print(":"); if (seconds < 10){ Serial.print("0"); // pad seconds } Serial.println(seconds); } }