Last active
March 25, 2018 11:28
-
-
Save efe/a3d9b6d132b2e51153f3eb9dc8a86e55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // CONFIGURATIONS | |
| const int digital_pin_of_servo = 9; | |
| const int servo_idle_state_degree = 90; | |
| const int servo_active_state_degree = 70; | |
| #include <DS3231.h> | |
| #include <Servo.h> | |
| // initialize the real time clock object. | |
| DS3231 rtc(SDA, SCL); | |
| // initialize the servo | |
| Servo servo; | |
| void setup() { | |
| rtc.begin(); | |
| servo.attach(digital_pin_of_servo); | |
| } | |
| void loop(){ | |
| String time_string = rtc.getTimeStr(); // "HH:MM:SS" | |
| String hour_string = time_string.substring(0, 2); // "HH" | |
| String minute_string = time_string.substring(3, 5); // "MM" | |
| String second_string = time_string.substring(6, 8); // "SS" | |
| long hour_int = hour_string.toInt(); // 0-23 | |
| long minute_int = minute_string.toInt(); // 0-59 | |
| long second_int = second_string.toInt(); // 0-59 | |
| // time to bang! HH:00:00 | |
| if (second_int == 0 && minute_int == 0){ | |
| if (hour_int == 0){ | |
| // bang 12 times at the midnight, not 0 time. | |
| hour_int = 12; | |
| } | |
| else if (hour_int > 12){ | |
| // bang 3 times when the time is 3pm, not 15 times. | |
| hour_int = hour_int - 12; | |
| } | |
| // bang number of hour times. | |
| for (int i = 0; i < hour_int; i++){ | |
| servo.write(servo_idle_state_degree); | |
| delay(1100); | |
| servo.write(servo_active_state_degree); | |
| delay(150); | |
| } | |
| } | |
| // set the servo to idle degree again. | |
| servo.write(servo_idle_state_degree); | |
| // Wait one second before repeating | |
| delay (1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment