Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- July 20, 2019
- This is a sketch to trigger a pin to go high once every 24 hours.
- There is one button to shut off the alarm if it is going off.
- There is a second button to reset the timer to zero, so that the alarm triggers immediately and then every 24 hours afterward.
- There are also some serial messages to verify that most events are occurring properly.
- */
- // Note: you cannot use pins zero and 1 if you use Serial messages!
- int alarm_output_pin = 2; // This pin is the one that goes high every 24 hours.
- int alarm_shut_off_button_pin = 3; // This is to shut off whatever the alarm pin does.
- int reset_timer_button_pin = 4; // This is the button to reset the timer at the current time.
- int LED_pin = 13; // This is the usual I/O pin that has the LED connected.
- int seconds_counter = 0;
- int minutes_counter = 0;
- int hours_counter = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(alarm_output_pin, OUTPUT);
- pinMode(LED_pin, OUTPUT);
- pinMode(alarm_shut_off_button_pin, INPUT_PULLUP);
- pinMode(reset_timer_button_pin, INPUT_PULLUP);
- digitalWrite(LED_pin, LOW); // Some clone boards need you to write this pin low or else it will stay high.
- digitalWrite(alarm_output_pin, LOW);
- Serial.println("Exiting setup");
- }
- void loop() {
- check_button_inputs();
- check_alarm();
- delay_one_second();
- check_seconds_counter();
- check_minutes_counter();
- check_hours_counter();
- }
- void check_button_inputs() {
- // The part below is for shutting off the alarm.
- if (digitalRead(alarm_shut_off_button_pin) == LOW) {
- digitalWrite(alarm_output_pin, LOW);
- Serial.println("Alarm shut-off button pressed");
- }
- // The part below resets the timer to all zero values, and the alarm should immediately trigger.
- if (digitalRead(reset_timer_button_pin) == LOW) {
- seconds_counter = 0;
- minutes_counter = 0;
- hours_counter = 0;
- Serial.println("Alarm reset button pressed");
- }
- }
- void check_alarm() {
- // This alarm is set to go off when all three countr values are zero.
- // This is done so that when you press the timer-reset button (that resets all three values to zero) the alarm should go off immediately and you don't need to wait 24 hours to see if it works.
- if (hours_counter == 0) {
- if (minutes_counter == 0) {
- if (seconds_counter == 0) {
- // Anything that should happen when the alarm goes off, goes here.
- digitalWrite(alarm_output_pin, HIGH);
- Serial.println("Alarm is activated");
- }
- }
- }
- }
- void check_hours_counter() {
- if (hours_counter == 24) {
- hours_counter = 0;
- }
- }
- void check_minutes_counter() {
- if (minutes_counter == 60) {
- minutes_counter = 0;
- hours_counter++;
- }
- }
- void check_seconds_counter() {
- if (seconds_counter == 60) {
- seconds_counter = 0;
- minutes_counter++;
- }
- }
- void delay_one_second() {
- delay(1000);
- seconds_counter++;
- check_to_blink_led();
- }
- void check_to_blink_led() {
- // This function is for blinking the pin-13 LED on and off once every ten seconds.
- int blinkCheck = seconds_counter % 10; // Modulo: If seconds_counter is evenly divisible by ten, then turn the LED on. Otherwise, turn the LED off.
- if (blinkCheck == 0) {
- digitalWrite(LED_pin, HIGH);
- }
- else {
- digitalWrite(LED_pin, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement