Hi. This is about a binary coded decimal clock.
This is the RTC library I'm using.
More info about the library.
I have a spare push-button which I included in my circuit from the previous setup of my project, which is not doing anything right now. I would like to use that button to switch the clock from clock mode to alarm mode so that I can set the alarm time using the same "set hour/min" buttons that I normally use to set the time. And then, once the alarm is set, using the same "mode change"button, switch it back to the clock mode. Right now, A0 and A3 analog input pins are available on the arduino. I might use one of those as my "mode change" button.
I may also need to use PIN 0 digital output on arduino to activate the buzzer for the alarm sound. I think PIN 0 digital output is available in my code, is that right? And perhaps, the buzzer would sound for 20 seconds and switch itself off.
I think I would need a buzzer for the alarm clock and some changes in the code.
Can anyone help?
Here is my code:
/*
An open-source binary clock for Arduino.
Based on the code from by Rob Faludi (http://www.faludi.com)
Code under (cc) by Lucas Berbesson
http://creativecommons.org/license/cc-gpl
*/
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
// Init a Time-data structure
Time t;
int second = 0, minute = 0, hour = 0; //start the time on 00:00:00
int munit, hunit, minuteTens, hourTens, valm = 0, valh = 0, ledstats, i;
// LEDS positions matrix
int leds[4][4] = {
{-1, 11, -1, 4},
{-1, 10, 7, 3},
{13, 9, 6, 2},
{12, 8, 5, 1}
};
void setup() {
//set outputs
for (int k = 0; k <= 13; k++) {
pinMode(k, OUTPUT);
digitalWrite(k, LOW);
}
pinMode(A1, INPUT);
pinMode(A2, INPUT);
rtc.begin();
}
void loop() {
t = rtc.getTime();
second = t.sec;
minute = t.min;
hour = t.hour;
munit = minute % 10; //sets the variable munit and hunit for the unit digits
hunit = hour % 10;
minuteTens = (int)(minute / 10);
hourTens = (int)(hour / 10);
//minutes units
if (munit & 1) {
digitalWrite(leds[3][3], HIGH);
} else {
digitalWrite(leds[3][3], LOW);
}
if (munit & 2) {
digitalWrite(leds[2][3], HIGH);
} else {
digitalWrite(leds[2][3], LOW);
}
if (munit & 4) {
digitalWrite(leds[1][3], HIGH);
} else {
digitalWrite(leds[1][3], LOW);
}
if (munit & 8) {
digitalWrite(leds[0][3], HIGH);
} else {
digitalWrite(leds[0][3], LOW);
}
//minutes
if (minuteTens & 1) {
digitalWrite(leds[3][2], HIGH);
} else {
digitalWrite(leds[3][2], LOW);
}
if (minuteTens & 2) {
digitalWrite(leds[2][2], HIGH);
} else {
digitalWrite(leds[2][2], LOW);
}
if (minuteTens & 4) {
digitalWrite(leds[1][2], HIGH);
} else {
digitalWrite(leds[1][2], LOW);
}
//hour units
if (hunit & 1) {
digitalWrite(leds[3][1], HIGH);
} else {
digitalWrite(leds[3][1], LOW);
}
if (hunit & 2) {
digitalWrite(leds[2][1], HIGH);
} else {
digitalWrite(leds[2][1], LOW);
}
if (hunit & 4) {
digitalWrite(leds[1][1], HIGH);
} else {
digitalWrite(leds[1][1], LOW);
}
if (hunit & 8) {
digitalWrite(leds[0][1], HIGH);
} else {
digitalWrite(leds[0][1], LOW);
}
//hour
if (hourTens & 1) {
digitalWrite(leds[3][0], HIGH);
} else {
digitalWrite(leds[3][0], LOW);
}
if (hourTens & 2) {
digitalWrite(leds[2][0], HIGH);
} else {
digitalWrite(leds[2][0], LOW);
}
valm = digitalRead(A1); // add one minute when pressed
if (valm == LOW) {
minute++;
if (minute == 60) {
hour++;
if (hour == 24) hour = 0;
minute = 0;
}
second = 0;
rtc.setTime(hour, minute, second);
delay(250);
}
valh = digitalRead(A2); // add one hour when pressed
if (valh == LOW) {
hour++;
if (hour == 24) {
hour = 0;
minute = 0;
}
second = 0;
rtc.setTime(hour, minute, second);
delay(250);
}
delay(50);
}