In this page I will be summarizing my final project and writing down steps that if you follow you can make it.
Some people will be more interested to see what mistakes I have done and the process I did before reaching the point where I know what exactly to do, you can go to my Project development week.
Also, to check the project proposal, components and were to buy them and cost ..... etc, you can check my Applications and Implications week.
I started with PCB design, as I though that the Enclosure design will depend on the size of the PCB, also, programming and test will be finally tested when there's a PCB.
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.
// 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);
}
After Assembling all components together and uploading the final code to the board, it worked fine, here's some hero shots and final presentation slide and video.
Smart Clock by Omar Seif El Eslam is marked with CC0 1.0