I gotten quite far with creating my Arduino Uno powered alarm clock, with a Waveshield as well. I need a little help with setting the alarm time.
I am fine with adjusting the time, using adjustTime(1) etc. However, how could I do this for a variable such as 'Alarm'?
Another part of this, the alarm variable needs to be stored in such a way so that it can be displayed on the 4digit 7seg display.
Here is my code so far:
Code: Select all
#include <WaveHC.h>
#include <WaveUtil.h>
#include <Time.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the volumes root directory
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
dir_t dirBuf; // buffer for directory reads
Adafruit_7segment matrix = Adafruit_7segment();
int hour1;
int hour2;
int minute1;
int minute2;
/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
int inPin = 17; //Input pin for button minute
int inPin2 = 16; //Input pin for button hour
int inPin3 = 15; //Input pin for button set alarm
int inPin4 = 14; //Input pin for alarm button LED
int inPin5 = 6; //Input pin for button cancel alarm
int val; // variables for reading the pin status, for buttons
int val2;
int val3;
int val4;
int val5;
int alarm1=1;
int alarm2=2;
int alarm3=2;
int alarm4=0;
int brightness1=1;
int brightness2=10;
int rate=2;
int Counter=0;
int AlarmOn=0;
//////////////////////////////////// SETUP
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps for debugging
matrix.begin(0x70);
matrix.setBrightness(brightness2);
setTime(12,00,0,1,1,11); // set time to ... Jan 1 2011
if (!card.init()) { //play with 8 MHz spi (default faster!)
error("Card init. failed!"); // Something went wrong, lets print out why
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
error("No valid FAT partition!"); // Something went wrong, lets print out why
}
// Try to open the root directory
if (!root.openRoot(vol)) {
error("Can't open root dir!"); // Something went wrong,
}
}
//////////////////////////////////// LOOP
void loop() {
//Dimming the display between 9pm and 8am
if (hour()>20 || hour()<8){
matrix.setBrightness(brightness1);
}
else {
matrix.setBrightness(brightness2);
}
//Displaying the time
matrix.writeDigitNum(0, hour()/10);
matrix.writeDigitNum(1, hour()%10);
matrix.writeDigitNum(3, minute()/10);
matrix.writeDigitNum(4, minute()%10);
matrix.drawColon(true);
matrix.writeDisplay();
//Adjusting current time, using the pushbuttons to add 1 to the minute and hour
val = digitalRead(inPin); // read input value
if (val == LOW) { // check if the input is HIGH (button released)
adjustTime(60); // add 1
delay(200);
}
// hour adjusting
val2 = digitalRead(inPin2); // read input value
if (val2 == LOW) { // check if the input is HIGH (button released)
adjustTime(3600); // add 1 hour
delay(200);
}
//Code to set the alarm. Press button, digits start to flash, then use normal adjustment time buttons
//to change the alarm time (as well as display whilst changing). When alarm button is pressed again,
// Save the displayed time as the alarm time and then revert back to the current time.
Off:
val3 = digitalRead(inPin3);
if (val3 == LOW) {
Serial.print(":");
delay(500);
Counter=1;
if (AlarmOn==0){
AlarmOn=1;
alarm1=1;
alarm2=2;
alarm3=0;
alarm4=0;
digitalWrite(inPin4, HIGH);
}
else{
AlarmOn=0;
digitalWrite(inPin4, LOW);
goto Off;
}
while(Counter==1) {
matrix.blinkRate(rate);
val2 = digitalRead(inPin2); // read input value
if (val2 == LOW) { // check if the input is HIGH (button released)
delay(200);
// change alarm time hours
alarm1=2;
alarm2=3;
}
val = digitalRead(inPin); // read input value
if (val == LOW) { // check if the input is HIGH (button released)
delay(200);
// change alarm time minutes
alarm3=1;
alarm4=6;
}
val4 = digitalRead(inPin3); // read input value
if (val4== LOW) {
delay(200);
matrix.blinkRate(0);
Counter=0;
continue;
}
matrix.writeDigitNum(0, alarm1);
matrix.writeDigitNum(1, alarm2);
matrix.writeDigitNum(3, alarm3);
matrix.writeDigitNum(4, alarm4);
matrix.drawColon(true);
matrix.writeDisplay();
}
}
// Setting the alarm off, needs to be coded for variable alarm time
if (AlarmOn==1 && hour()/10==alarm1 && hour()%10==alarm2 && minute()/10==alarm3 && minute()%10==alarm4 && second()==0){
root.rewind();
play(root);
}
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
/////////////////////////////////// HELPERS
/*
* print error message and halt
*/
void error_P(const char *str) {
PgmPrint("Error: ");
SerialPrint_P(str);
sdErrorCheck();
while(1);
}
/*
* print error message and halt if SD I/O error, great for debugging!
*/
void sdErrorCheck(void) {
if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: ");
Serial.print(card.errorCode(), HEX);
PgmPrint(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
/*
* play recursively - possible stack overflow if subdirectories too nested
*/
void play(FatReader &dir) {
FatReader file;
while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time
// Skip it if not a subdirectory and not a .WAV file
if (!DIR_IS_SUBDIR(dirBuf)
&& strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
continue;
}
Serial.println(); // clear out a new line
for (uint8_t i = 0; i < dirLevel; i++) {
Serial.write(' '); // this is for prettyprinting, put spaces in front
}
if (!file.open(vol, dirBuf)) { // open the file in the directory
error("file.open failed"); // something went wrong
}
if (file.isDir()) { // check if we opened a new directory
putstring("Subdir: ");
printEntryName(dirBuf);
Serial.println();
dirLevel += 2; // add more spaces
// play files in subdirectory
play(file); // recursive!
dirLevel -= 2;
}
else {
// Aha! we found a file that isnt a directory
putstring("Playing ");
printEntryName(dirBuf); // print it out
if (!wave.create(file)) { // Figure out, is it a WAV proper?
putstring(" Not a valid WAV"); // ok skip it
} else {
Serial.println(); // Hooray it IS a WAV proper!
wave.play(); // make some noise!
uint8_t n = 0;
while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
putstring(".");
if (!(++n % 32))Serial.println();
delay(100);
val5 = digitalRead(inPin5); // read input value
if (val5== LOW) {
delay(200);
return;
}
}
sdErrorCheck(); // everything OK?
// if (wave.errors)Serial.println(wave.errors); // wave decoding errors
}
}
}
}
Sorry for the essay,
Tom