/*
Alarm Clock
By Carson Gamble
*/
#include <pitches.h>
#include <IRremote.h>
#include <IRremoteInt.h>
//#include <SevenSegmentExtended.h>
#include <SevenSegmentFun.h>
//#include <SevenSegmentTM1637.h>
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
unsigned long currentTime;
unsigned long previousTime = 0;
int mill = 0;
byte sec = 0;
int minute = 0;
int hour = 0;
int alarmHour = 100;
int alarmMinute = 100;
boolean alarming = false;
int alarmed = 0;
int noteDuration=-1;
boolean fastMode = false;
int melody[] = {
2637, 2637, 0, 2637,
0, 2093, 2637, 0,
3136, 0, 0, 0,
1568, 0, 0, 0,
2093, 0, 0, 1568,
0, 0, 1319, 0,
0, 1760, 0, 1976,
0, 1865, 1760, 0,
1568, 2637, 3136,
3520, 0, 2794, 3136,
0, 2637, 0, 2093,
2349, 1976, 0, 0,
2093, 0, 0, 1568,
0, 0, 1319, 0,
0, 1760, 0, 1976,
0, 1865, 1760, 0,
1568, 2637, 3136,
3520, 0, 2794, 3136,
0, 2637, 0, 2093,
2349, 1976, 0, 0
};
int tempo[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
const byte PIN_BUZZ = 2;
const byte PIN_IR = 3;
IRrecv irrecv(PIN_IR); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results' the ir assistant
const byte PIN_CLK = 5; // define CLK pin (any digital pin)
const byte PIN_DIO = 6; // define DIO pin (any digital pin)
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
void setup() {
// put your setup code here, to run once:
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
display.begin(); // initializes the display
display.setBacklight(40); // set the brightness to 100 %
delay(1000); // wait 1000 ms
}
void loop() {c
// put your main code here, to run repeatedly:
noteDuration=-1;
checkTime();
display.printTime(hour, minute, false);
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
if((hour == alarmHour) && (minute == alarmMinute)&&(sec < 2)){
alarming = true;
}
alarmFunction();
}
void checkTime() { //This function updates the time based on the arduino keeping track of how long its been since the program has started. The unsigned long keeps track of milliseconds and goes bad after ~50 days)
currentTime = millis();
mill += currentTime - previousTime;
if (mill >= 1000) {
sec++;
mill = mill % 1000;
}
if(fastMode){
if(sec>=6){
minute++;
sec = sec%6;
}
}else if (sec >= 60) {
minute++;
sec = sec % 60;
}
if (minute >= 60) {
hour++;
minute = minute % 60;
}
if (hour >= 24) {
hour = hour % 24;
// sec = -6;
}
previousTime = currentTime;
Serial.print(hour);
Serial.print(" : ");
Serial.print(minute);
Serial.print(" : ");
Serial.println(sec);
}
void alarmFunction(){ //Dictates Alarm Logic and alarm noise and delay for clock
if(alarming){
//display.blink();
//this is just how the mario theme could be played based on a tutorial I found, other song arrays may not work with this method.
noteDuration = 1000 /tempo[alarmed];
tone(PIN_BUZZ, melody[alarmed], noteDuration);
alarmed++;
if(alarmed>77){
alarmed=0;
}
}else{
alarming = false;
alarmed = 0;
}
if(noteDuration==-1){//this is the main delay for the loop
delay(100);
}else{
delay(noteDuration*1.30);
}
}
//generic retriving time function
//returns a string with the time needed, in 4 character order
//0 is a required input
String changeTime(){
delay(500);
irrecv.resume(); // receive the next value
String newTime = "----";
for(int i = 0; i<4; i++){
byte temp;
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFF9867: newTime.setCharAt(i, '0'); temp = 0; break;
case 0xFFA25D: newTime.setCharAt(i, '1'); temp = 1; break;
case 0xFF629D: newTime.setCharAt(i, '2'); temp = 2; break;
case 0xFFE21D: newTime.setCharAt(i, '3'); temp = 3; break;
case 0xFF22DD: newTime.setCharAt(i, '4'); temp = 4; break;
case 0xFF02FD: newTime.setCharAt(i, '5'); temp = 5; break;
case 0xFFC23D: newTime.setCharAt(i, '6'); temp = 6; break;
case 0xFFE01F: newTime.setCharAt(i, '7'); temp = 7; break;
case 0xFFA857: newTime.setCharAt(i, '8'); temp = 8; break;
case 0xFF906F: newTime.setCharAt(i, '9'); temp = 9; break;
}
irrecv.resume(); // receive the next value
}
if(newTime.charAt(i)=='-'){
i--;
}
//A Time Check can be placed here to see if valid times are being input.
delay(100);
Serial.println(newTime);
display.print(newTime);
checkTime();
}
return newTime;
}
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
// switch statement takes in input, case decyphers it
switch(results.value)
{
case 0xFF4AB5: Serial.println("DOWN"); break;
case 0xFF18E7: Serial.println("UP"); break;
case 0xFF10EF: Serial.println("LEFT"); fastMode = !fastMode; display.blink(); break;
case 0xFF5AA5: Serial.println("RIGHT"); break;
case 0xFF38C7:{ Serial.println("OK/Alarm Off"); alarming = false; break;}
case 0xFF6897: /*Serial.println("Asterisk/SetAlarm"); break; */{ //This starts the alarm sequence
String newAlarmTime= changeTime(); //retrives string from keypad
alarmHour = (newAlarmTime.substring(0,2)).toInt();
alarmMinute = (newAlarmTime.substring(2)).toInt();
break;}
case 0xFFB04F: {Serial.println("Pound/SetTime");
String newTime = changeTime();
hour = (newTime.substring(0,2)).toInt();
minute = (newTime.substring(2)).toInt();
sec = 0;
break;}
case 0xFF9867: Serial.println("0"); break;
case 0xFFA25D: Serial.println("1"); break;
case 0xFF629D: Serial.println("2"); break;
case 0xFFE21D: Serial.println("3"); break;
case 0xFF22DD: Serial.println("4"); break;
case 0xFF02FD: Serial.println("5"); break;
case 0xFFC23D: Serial.println("6"); break;
case 0xFFE01F: Serial.println("7"); break;
case 0xFFA857: Serial.println("8"); break;
case 0xFF906F: Serial.println("9"); break;
//case 0xFFFFFFFF: Serial.println(" REPEAT");break;
default:
Serial.println("Error" + results.value);
}// End Case
delay(500); // Do not get immediate repeat (Basically Debouncing)
}
//END translateIR
Comments