I have spent much time thinking what should be my final project, that lead me to many questions like what do I like to do, what is my hoppy, what would I enjoy while doing.
Then I came to the important question, what will satisfy the Academy requirements and also be usefull for me, and the first thought was a smart alarm.
What do I mean with smart alarm, with help from our local mentor I decieded that the alarm should have a kind of a game and you will have to pass a level at this game to shut off the alarm.
Why alarm?, I like the stand alone alarms not the mobile alarms and I wanted to implement a fun idea into it.
So lets call my prject
Who will use it, I think everyone who likes cool stuff around and also doesn't like phone alarms.
With the help of my instructors We created a project tracking sheet where I listed down all tasks I imagine I need to make during my final project and distributed them on days to have a display of what I have done, what's left to finish which helped alot in time management.
Smart Alarm is a simple alarm which displays current time and data, and you can set your alarms, and there will be a game that you have to win to stop the alarm.
For a brief and direct steps of the final project you can go to my Final project page, here I will be writing down in details what I have been through and failed or abandoned trials.
I have changed the Display to 7 Segment as I wanted the clock to have a full screen display of the numbers also, I wanted to have a bigger display of the numbers.
I started with the SamD11D MCU, the idea were to use 2 of it one for display and one for the other components and communicate them with I2C.
After facing the memory size problem I decided to use the SamD21E.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//------Electronics-project-hub.com-----//
#include "SevSeg.h"
SevSeg Display;
//const int hrs_btn = 31;
//const int min_btn = 30;
//const int ledPin = 2;
//int Hrs = 12;
//int Min = 0;
//int Sec = 0;
int Time;
int ledState = LOW;
// Rotary Encoder Inputs
#define CLK 19
#define DT 18
#define SW 15
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
int pinState = HIGH;
int AH1 = 17;
int AM1 = 23;
int AH2 = 13;
int AM2 = 37;
int alarm1time;
int alarm2time;
char sitAlarm[][5] = {"a1--", "a2--"};
//speaker pin
#define SPKR 14
void setup () {
Serial.begin(9600);
// Set encoder pins as inputs
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode(SPKR, OUTPUT);
if (! rtc.begin()) {
//Serial.println("Couldn't find RTC");
//Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
//Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
byte numDigits = 4;
byte digitPins[] = {10, 8, 9, 11};
byte segmentPins[] = {3, 2, 4, 7, 6, 0, 1, 5};
bool resistorsOnSegments = true;
bool updateWithDelays = false;
byte hardwareConfig = COMMON_ANODE;
bool leadingZeros = true;
bool disableDecPoint = true;
Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);
Display.setBrightness(100);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
// Call updateEncoder() when any high/low changed seen
// on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(CLK, updateEncoder, CHANGE);
attachInterrupt(DT, updateEncoder, CHANGE);
//attachInterrupt(SW, updateEncoder, CHANGE);
}
void loop () {
if (digitalRead(SW) == 0)
{
while (digitalRead(SW) == 0)
Display.blank();
Serial.println("SW");
alarmx();
}
//out:
DateTime now = rtc.now();
int hh = now.hour();
int mm = now.minute();
Time = hh * 100 + mm;
//Serial.println(now.second());
Display.setNumber(Time);
Display.refreshDisplay();
alarm1time = AH1 * 100 + AM1;
Serial.println(alarm1time);
alarm2time = AH2 * 100 + AM2;
if (alarm1time == Time || alarm2time == Time)
speaker();
else
digitalWrite(SPKR, 0);
}
void updateEncoder() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir = "CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir = "CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
}
void alarmx() {
int i;
counter = 0;
while (1) {
//Display.setChars("nn");
//Display.setSegmentsDigit(2, Am10s);
//Display.setSegmentsDigit(3, Am1s);
Display.setChars(sitAlarm[i]);
Display.refreshDisplay();
Serial.println(sitAlarm[i]);
i = counter ;
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
goto swtch;
}
}
swtch:
switch (i) {
case 0:
while (1) {
Display.setChars("nn");
Display.refreshDisplay();
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
//Display.setChars("nn");
//Display.setSegmentsDigit(2, Am10s);
//Display.setSegmentsDigit(3, Am1s);
Display.setNumber(AM1);
Display.refreshDisplay();
Serial.println(AM1);
AM1 = counter ;
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
Display.setChars("hh");
Display.refreshDisplay();
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
//Display.setChars("hh");
//Display.setSegmentsDigit(2, Am10s);
//Display.setSegmentsDigit(3, Am1s);
Display.setNumber(AH1);
Display.refreshDisplay();
Serial.println(AH1);
AH1 = counter ;
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
goto out;
}
}
}
}
}
}
}
}
break;
case 1:
while (1) {
Display.setChars("nn");
Display.refreshDisplay();
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
//Display.setChars("nn");
//Display.setSegmentsDigit(2, Am10s);
//Display.setSegmentsDigit(3, Am1s);
Display.setNumber(AM2);
Display.refreshDisplay();
Serial.println(AM2);
AM2 = counter ;
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
Display.setChars("hh");
Display.refreshDisplay();
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
while (1) {
//Display.setChars("hh");
//Display.setSegmentsDigit(2, Am10s);
//Display.setSegmentsDigit(3, Am1s);
Display.setNumber(AH2);
Display.refreshDisplay();
Serial.println(AH2);
AH2 = counter ;
if (digitalRead(SW) == 0) {
while (digitalRead(SW) == 0)
Display.blank();
goto out;
}
}
}
}
}
}
}
}
default:
break;
}
out:
delay(1);
}
void speaker() {
digitalWrite(SPKR, 1);
}