|
| 1 | +/* |
| 2 | + This class uses the VariableTimedAction library to emulate a clock. |
| 3 | +*/ |
| 4 | + |
| 5 | +// VariableTimedAction - Version: Latest |
| 6 | +#include <VariableTimedAction.h> |
| 7 | + |
| 8 | +class Clock : public VariableTimedAction { |
| 9 | +private: |
| 10 | + int hours = 0, minutes = 0, seconds = 0; |
| 11 | + |
| 12 | + //updates the clock |
| 13 | + unsigned long run() { |
| 14 | + seconds++; |
| 15 | + if (seconds == 60) { |
| 16 | + seconds = 0; |
| 17 | + minutes++; |
| 18 | + if (minutes == 60) { |
| 19 | + minutes = 0; |
| 20 | + hours++; |
| 21 | + if (hours == 24) { |
| 22 | + hours = 0; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return 0; |
| 27 | + } |
| 28 | + |
| 29 | +public: |
| 30 | + int getHours() { return hours; } |
| 31 | + int getMinutes() { return minutes; } |
| 32 | + int getSeconds() { return seconds; } |
| 33 | + |
| 34 | + //starts the clock at 00:00:00 |
| 35 | + Clock() { start(1000); } |
| 36 | + |
| 37 | + //starts the clock with the specified time |
| 38 | + Clock(int hours, int minutes, int seconds) |
| 39 | + : hours(hours), minutes(minutes), seconds(seconds) { |
| 40 | + start(1000, false); |
| 41 | + } |
| 42 | + |
| 43 | + //sets the clock to the specified time |
| 44 | + setTime(int hours, int minutes, int seconds) { |
| 45 | + this->hours = hours; |
| 46 | + this->minutes = minutes; |
| 47 | + this->seconds = seconds; |
| 48 | + } |
| 49 | + |
| 50 | +} clock; |
| 51 | + |
| 52 | +class ClockDisplay : public VariableTimedAction { |
| 53 | +public: |
| 54 | + ClockDisplay(Clock &clock) { |
| 55 | + this->clock = &clock; |
| 56 | + } |
| 57 | +private: |
| 58 | + Clock * clock; |
| 59 | + unsigned long run() { |
| 60 | + Serial.print(clock->getHours()); |
| 61 | + Serial.print(":"); |
| 62 | + Serial.print(clock->getMinutes()); |
| 63 | + Serial.print(":"); |
| 64 | + Serial.println(clock->getSeconds()); |
| 65 | + return 0; |
| 66 | + } |
| 67 | +} display(clock); |
| 68 | + |
| 69 | +void setup() { |
| 70 | + //initialize Serial |
| 71 | + Serial.begin(9600); |
| 72 | + |
| 73 | + //obtain current time |
| 74 | + //prompt user input |
| 75 | + Serial.println("What is the current time in 24-hour time? (HH:mm:ss)"); |
| 76 | + char input[8]; |
| 77 | + //obtain user input |
| 78 | + int len = Serial.readBytesUntil(':', input, 8); |
| 79 | + //null terminate the string |
| 80 | + input[len] = '\0'; |
| 81 | + //convert string to integer |
| 82 | + int userHours = atoi(input); |
| 83 | + |
| 84 | + len = Serial.readBytesUntil(':', input, 8); |
| 85 | + //null terminate the string |
| 86 | + input[len] = '\0'; |
| 87 | + //convert string to integer |
| 88 | + int userMinutes = atoi(input); |
| 89 | + |
| 90 | + len = Serial.readBytesUntil('\n', input, 8); |
| 91 | + //null terminate the string |
| 92 | + input[len] = '\0'; |
| 93 | + //convert string to integer |
| 94 | + int userSeconds = atoi(input); |
| 95 | + |
| 96 | + //set the user's time |
| 97 | + clock.setTime(userHours, userMinutes, userSeconds); |
| 98 | + //update the time display every 15 seconds |
| 99 | + display.start(15 * 1000); |
| 100 | +} |
| 101 | + |
| 102 | +void loop() { |
| 103 | + //check if any actions must be executed |
| 104 | + VariableTimedAction::updateActions(); |
| 105 | +} |
0 commit comments