First Commit

This commit is contained in:
Christian Mittring
2018-04-01 12:29:12 +02:00
parent 3396da2cd6
commit 9d75a64173
8 changed files with 962 additions and 3 deletions

View File

@@ -3,3 +3,97 @@
This is an Alarm Clock (Radio) based on the Arduino Uno in combination with a MAX7219 LED-Matrix, Relay-Board, DS3231 RTC and a standalone Radio/Audio Module. This is an Alarm Clock (Radio) based on the Arduino Uno in combination with a MAX7219 LED-Matrix, Relay-Board, DS3231 RTC and a standalone Radio/Audio Module.
Note: This Project is under heavy development at the moment. Note: This Project is under heavy development at the moment.
## Wiring
Image how to wire the project will follow.
###### LED-Matrix:
| LED-Matrix | Arduino |
| -------- | -------- |
| VCC | Vin/5V+ |
| GND | GND |
| DIN | GPIO13 (HSPID) |
| CS | GPIO 10 |
| CLK | GPIO14 (HSPICLK) |
###### RTC DS3231:
| DS3231 | Arduino |
| -------- | -------- |
| 3,3V | 3,3V |
| GND | GND |
| SCL | GPIO5 |
| SDA | GPIO4 |
###### Relay-Module:
| Relay-Module | Arduino | Type |
| -------- | -------- | -------- |
| VCC | Vin/5V+ | Power |
| GND | GND | GND |
| In1 | GPIO16 | Power |
| In2 | GPIO12 | Input |
| In3 | GPIO12 | Input |
| In4 | GPIO12 | Input |
###### Switch:
The switch should connect the GPIO to ground if it is pressed down:
* power_sw: GPIO3
* input_sw: GPIO1
* alarm_sw: GPIO2
* sleep_sw: GPIO0
You can also change this at the beginning of the code:
```
//Pin Taster
const int pinTime = 3; //Pin fuer Toggle_Power und Einstellen der Uhrzeit (Lang)
const int pinSet = 0; //Pin fuer Sleep und Zeit_Einstellung im Alarm/Time Set Mode
const int pinAlarm = 2; //Pin fuer Alarm An/Aus und setzen der Alarmzeiten (Lang)
const int pinInput = 1; //Pin für die Eingangswahl
const int pinSleep = 9; //Pin Unbenutzt
//Pin Relay
const int outInput = 12;
const int outPower = 16;
```
## How does it work
The Clock can control 2 relays:
* Power Relay: is used to turn on/off a AMP, Radio or any other device.
* Input Relay: is to switch between two Inputs like Radio for Alarm and CD/MP3 or Aux for normal Music.
* If an alarm is triggerd both relays are switched on.
The Clock is controlled with 4 Buttons
* Power-Button: turn power relay on/off
* Power-Button (long-press): Set clock (Day, Month, Year, Hour, Minute, Second) with Sleep-Button
* Input-Button: turn input relay on/off
* Alarm-Button: toggle active alarm (Alarm 1 -> Alarm 2 -> Alarm 1 and Alarm 2 -> Alarm off)
* Alarm-Button (long-press): set alarm time (Alarm 1 Hour, Alarm 1 Minute, Alarm 2 Hour, Alarm 2 Minute) with Sleep-Button
* Sleep-Button : set time to turn power relay off (90 Min -> 60 -> 30 -> 15 -> Off)
## Issues
* Alarm 2 is not working like expected. It will be triggered every hour. (Alarm 1 is ok, once a day)
* Networkconnection is not used to update time over NTP. (not implemented yet)
* Long-Press blocks display until release.
## Dependencies
LedMatrix.h : [https://github.com/squix78/MAX7219LedMatrix](https://github.com/squix78/MAX7219LedMatrix)
DS3231.h : [https://github.com/NorthernWidget/DS3231](https://github.com/NorthernWidget/DS3231)

164
Wecker/Alarm.ino Normal file
View File

@@ -0,0 +1,164 @@
void run_alarm(){
if (test_alarm())
{
display_text("ALARM");
delay_gui(1000);
switch_radio(false);
setSleepTimer(30);
}
//Test if Summer->Winter time change (this should also change the Alarm times!)
bool dst = summertime_EU(y,M,d,h,1);
//Serial.println(String(dst) + ":" + String(summer));
if (dst != summer)
{
summer = dst;
set_alarm1();
set_alarm2();
write_eeprom();
}
}
//####################################################################################################
void switch_alarm_mode(){
bool A1 = Clock.checkAlarmEnabled(1);
bool A2 = Clock.checkAlarmEnabled(2);
if (A1 && A2){
Clock.turnOffAlarm(1);
Clock.turnOffAlarm(2);
}
else if (A1 && !A2){
Clock.turnOffAlarm(1);
Clock.turnOnAlarm(2);
}
else if (!A1 && A2){
Clock.turnOnAlarm(1);
Clock.turnOnAlarm(2);
}
else if (!A1 && !A2){
Clock.turnOnAlarm(1);
Clock.turnOffAlarm(2);
}
A1 = Clock.checkAlarmEnabled(1);
A2 = Clock.checkAlarmEnabled(2);
if (A1 && A2)
{
display_text("A1/2");
}
else if (A1)
{
display_text("A1");
}
else if (A2)
{
display_text("A2");
}
else
{
display_text("A Off");
}
delay_gui(1000);
}
void set_alarm_item(){
if (setItem == 0)
{
setItem = 10;
}
setItem = setItem + 1;
if(setItem == 13)
{
set_alarm1();
}
else if (setItem > 14)
{
set_alarm2();
setItem = 0;
}
}
//####################################################################################################
bool test_alarm(){
bool A1 = Clock.checkIfAlarm(1);
bool A2 = Clock.checkIfAlarm(2);
bool A1_en = Clock.checkAlarmEnabled(1);
bool A2_en = Clock.checkAlarmEnabled(2);
return ((A1 && A1_en) || (A2 && A2_en));
}
//########################################################################################################
//Helpers
void add_al1_hour(int i){
al1h = al1h + i;
if (al1h > 23){
al1h = 0;
}
}
void add_al1_minute(int i){
al1m = al1m + i;
if (al1m > 59){
al1m = al1m - 60;
}
}
void add_al2_hour(int i){
al2h = al2h + i;
if (al2h > 23){
al2h = 0;
}
}
void add_al2_minute(int i){
al2m = al2m + i;
if (al2m > 59){
al2m = al2m - 60;
}
}
void set_alarm1(){
int al1h_utc = al1h;
if (summertime_EU(y,M,d,h,1))
{
al1h_utc = al1h - 1;
if (al1h_utc < 0){
al1h_utc = 23;
}
}
Clock.setA1Time(0, al1h_utc, al1m, 0, 1000, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
Serial.println("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(A1Second) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
display_text("A1");
delay(1000);
display_text(String(A1Hour) + ":" +String(A1Minute));
delay(1000);
}
void set_alarm2(){
int al2h_utc = al2h;
if (summertime_EU(y,M,d,h,1))
{
al2h_utc = al2h - 1;
if (al2h_utc < 0){
al2h_utc = 23;
}
}
Clock.setA2Time(0, al2h_utc, al2m, 0b1000000, true, false, false);
//Clock.turnOnAlarm(1);
byte A1Day, A1Hour, A1Minute, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
Serial.println("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
display_text("A2");
delay(1000);
display_text(String(A1Hour) + ":" +String(A1Minute));
delay_gui(1000);
}

274
Wecker/Uhr.ino Normal file
View File

@@ -0,0 +1,274 @@
void show_current_time(unsigned long intervall, unsigned long currentMillis){
if(intervall > 16000){
previousMillis = currentMillis;
}
if (intervall > 14000){
display_date(d,M,y);
}
/*else if (intervall > 12000){
display_year(d,M,y);
}
else if (intervall > 10000){
display_text(dow);
}
else if (intervall > 9000){
display_text(readDHT());
}
else if (intervall > 8000){
display_text("Temp");
}*/
else{
display_time(h,m,s);
}
setBrightnes(h);
}
void show_time_infos(unsigned long intervall, unsigned long currentMillis){
if(setItem == 1 || setItem == 2){
display_date(d,M,y);
}
else if(setItem == 2){
display_date(d,M,y);
}
else if(setItem == 3){
display_year(d,M,y);
}
else if(setItem == 4 || setItem == 5 || setItem == 6){
display_time(h,m,s);
}
else if(setItem == 11 || setItem == 12){
display_time(al1h,al1m,0);
}
else if(setItem == 13 || setItem == 14){
display_time(al2h,al2m,0);
}
}
//####################################################################################################
//Setup
void switch_set_item(){
setItem = setItem + 1;
if(setItem > 6)
{
set_clock();
setItem = 0;
display_text(" OK ");
delay_gui(1000);
}
}
void set_time(){
if(setItem == 1){
add_day(1);
}
else if(setItem == 2){
add_month(1);
}
else if(setItem == 3){
add_year(1);
}
else if(setItem == 4){
add_hour(1);
}
else if(setItem == 5){
add_minute(1);
}
else if(setItem == 6){
add_second(1);
}
else if(setItem == 11){
add_al1_hour(1);
}
else if(setItem == 12){
add_al1_minute(1);
}
else if(setItem == 13){
add_al2_hour(1);
}
else if(setItem == 14){
add_al2_minute(1);
}
}
//###################################################################################################################################
//Time Code
void update_Time(){
DateTime now = RTC.now();
h = now.hour(); //24-hr
m = now.minute();
s = now.second();
d = now.day();
M = now.month();
y = now.year();
if (summertime_EU(y,M,d,h,1))
{
h = h + 1;
if (h == 24){
h = 0;
}
}
dow = daysOfTheWeek[Clock.getDoW()];
//Serial.println(String(h) + ":" + String(m) + ":" + String(s) + " - " + String(d) + "." + String(M) + "." + String(y) + " | DST: " + String(summertime_EU(y,M,d,h,1)));
}
//###################################################################################################################################
//Display Code
void display_year(int Day, int Month, int Year){
display_text(String(Year));
}
void display_time(int Stunde, int Minute, int Sekunde){
String Min = String(Minute);
String Stu = String(Stunde);
if (Minute < 10)
{
Min = "0" + Min;
}
if (Stunde < 10)
{
Stu = "0" + Stu;
}
if(setItem == 4)
{
display_text("h :" + Stu);
}
else if(setItem == 5)
{
display_text("m :" + Min);
}
else if(setItem == 6)
{
display_text("s :" + String(Sekunde));
}
else if(setItem == 11)
{
display_text("h1:" + Stu);
}
else if(setItem == 12)
{
display_text("m1:" + Min);
}
else if(setItem == 13)
{
display_text("h2:" + Stu);
}
else if(setItem == 14)
{
display_text("m2:" + Min);
}
else if (Sekunde % 2 == 0){
display_text(Stu + ":" + Min);
}
else{
display_text(Stu + "." + Min);
}
}
void display_date(int Day, int Month, int Year){
String Mon = String(Month);
String Da = String(Day);
if (Day < 10)
{
Da = "0" + Da;
}
if (Month < 10)
{
Mon = "0" + Mon;
}
if(setItem == 1)
{
display_text("T :" + Da);
}
else if(setItem == 2)
{
display_text("M :" + Mon);
}
else{
display_text(Da + "." + Mon);
}
}
//########################################################################################################
//Helpers
void add_second(int i){
s = s + i;
if (s > 59)
{
s = s - 60;
}
}
void add_hour(int i){
h = h + i;
if (h > 23){
h = 0;
}
}
void add_minute(int i){
m = m + i;
if (m > 59){
m = m - 60;
}
}
void add_month(int i){
M = M + i;
if (M > 12){
M = 1;
}
}
void add_day(int i){
d = d + i;
if (d > 31){
d = 1;
}
}
void add_year(int i){
y = y + i;
if (y > 2030){
y = 2017;
}
}
void set_clock(){
Clock.setClockMode(false); // set to 24h
//setClockMode(true); // set to 12h
Clock.setYear(y-2000);
Clock.setMonth(M);
Clock.setDate(d);
//Clock.setDoW(DoW);
if (summertime_EU(y,M,d,h,1))
{
Clock.setHour(h-1);
}
else{
Clock.setHour(h);
}
Clock.setMinute(m);
Clock.setSecond(s);
}
boolean summertime_EU(int year, byte month, byte day, byte hour, byte tzHours)
// European Daylight Savings Time calculation by "jurs" for German Arduino Forum
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
// return value: returns true during Daylight Saving Time, false otherwise
{
if (month<3 || month>10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if (month>3 && month<10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if (month==3 && (hour + 24 * day)>=(1 + tzHours + 24*(31 - (5 * year /4 + 4) % 7)) || month==10 && (hour + 24 * day)<(1 + tzHours + 24*(31 - (5 * year /4 + 1) % 7)))
return true;
else
return false;
}

164
Wecker/Wecker.ino Normal file
View File

@@ -0,0 +1,164 @@
#include <math.h>
#include <SPI.h>
#include "LedMatrix.h"
#include <DS3231.h>
#include <EEPROM.h>
//RealTimeClock
RTClib RTC;
DS3231 Clock;
//Display
#define NUMBER_OF_DEVICES 4
#define CS_PIN 10
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
//ConfigData (Name and Pin-Numbers)
const String NodeName = "Wecker";
//Pin Taster
const int pinTime = 2; //Pin fuer Toggle_Power und Einstellen der Uhrzeit (Lang)
const int pinSet = 3; //Pin fuer Sleep und Zeit_Einstellung im Alarm/Time Set Mode
const int pinAlarm = 4; //Pin fuer Alarm An/Aus und setzen der Alarmzeiten (Lang)
const int pinInput = 5; //Pin für die Eingangswahl
const int pinSleep = 6; //Pin Unbenutzt
//Pin Relay
const int outInput = 7;
const int outPower = 8;
//For Summertime change notification (change alarmtime) this should be saved in the EEPROM!
bool summer = false;
//Alarm
int al1h = 0;
int al1m = 0;
int al2h = 0;
int al2m = 0;
unsigned long time_on = -1;
int minutes_sleep = -1;
//Current Date and Time from RTC
int h = 0;
int m = 0;
int s = 0;
int d = 0;
int M = 0;
int y = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String dow = "-";
int setItem = 0;
long block_gui = -1;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200);
delay(10);
Wire.begin();
//Setup Display
ledMatrix.init();
ledMatrix.setTextAlignment(0);
ledMatrix.setRotation(true);
ledMatrix.setIntensity(0);
display_text("U");
delay(400);
display_text("Uh");
delay(400);
display_text("Uhr");
delay(400);
display_text("Uhr");
delay(500);
//restore data
get_eeprom();
//Pin Setup
pinMode(pinTime, INPUT_PULLUP); // set pin to input
digitalWrite(pinTime, HIGH); // turn on pullup resistors
pinMode(pinSet, INPUT_PULLUP); // set pin to input
digitalWrite(pinSet, HIGH); // turn on pullup resistors
pinMode(pinAlarm, INPUT_PULLUP); // set pin to input
digitalWrite(pinAlarm, HIGH); // turn on pullup resistors
pinMode(pinInput, INPUT_PULLUP); // set pin to input
digitalWrite(pinInput, HIGH); // turn on pullup resistors
pinMode(pinSleep, INPUT_PULLUP); // set pin to input
digitalWrite(pinSleep, HIGH); // turn on pullup resistors
pinMode(outPower, OUTPUT); // set pin to input
digitalWrite(outPower, HIGH); // turn on pullup resistors
pinMode(outInput, OUTPUT); // set pin to input
digitalWrite(outInput, HIGH); // turn on pullup resistors
delay(500);
//Set Alarm Times
DateTime now = RTC.now();
d = now.day();
M = now.month();
y = now.year();
h = now.hour(); //24-hr
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
bool A1Dy, A1h12, A1PM;
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
if (summer)
{
al1h = A1Hour + 1;
}
else{
al1h = A1Hour;
}
al1m = A1Minute;
Serial.println("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
if (summer)
{
al2h = A1Hour + 1;
}
else{
al2h = A1Hour;
}
al2m = A1Minute;
Serial.println("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
delay(1000);
previousMillis = millis();
}
void loop() {
unsigned long currentMillis = millis();
unsigned long intervall = currentMillis - previousMillis;
release_gui(currentMillis);
// put your main code here, to run repeatedly:
if(setItem == 0){
update_Time();
run_alarm();
if (block_gui < 0)
show_current_time(intervall,currentMillis);
}
else{
show_time_infos(intervall,currentMillis);
previousMillis = currentMillis;
}
test_pin();
handleSleep(intervall, currentMillis);
delay(80);
}

30
Wecker/display.ino Normal file
View File

@@ -0,0 +1,30 @@
void display_text(String text){
//Serial.println("Display:");
//Serial.println(text);
ledMatrix.clear();
ledMatrix.setText(text);
ledMatrix.drawText();
ledMatrix.commit();
}
void setBrightnes(int h){
if (h > 20 || h < 7){
ledMatrix.setIntensity(0);
}
else{
ledMatrix.setIntensity(4);
}
}
void delay_gui(unsigned long millis_s){
block_gui = millis() + millis_s;
}
void release_gui(unsigned long currentMillis){
if (block_gui < currentMillis)
block_gui = -1;
else
return;
}

27
Wecker/eeprom.ino Normal file
View File

@@ -0,0 +1,27 @@
//Code to save some settings
unsigned int addr = 0;
struct {
bool summertime = false;
} data;
void write_eeprom(){
//EEPROM.begin(512);
// load EEPROM data into RAM, see it
data.summertime = summer;
Serial.println("Writing to EEPROM: "+String(data.summertime));
// replace values in EEPROM
EEPROM.put(addr,data);
//EEPROM.commit();
//EEPROM.end();
}
bool get_eeprom(){
//EEPROM.begin(512);
EEPROM.get(addr,data);
Serial.println("Found: "+String(data.summertime));
//EEPROM.end();
summer = data.summertime;
}

98
Wecker/pinInput.ino Normal file
View File

@@ -0,0 +1,98 @@
void test_pin(){
handle_Time_switch();
handle_Set_switch();
handle_Alarm_switch();
handle_Input_switch();
}
void handle_Alarm_switch(){
int state = digitalRead(pinAlarm);
if(state == LOW && setItem == 0) {
int presstime = get_pin_delay(pinAlarm);
if (presstime > 10){
set_alarm_item();
delay(200);
wait_to_release(pinAlarm);
}
else{
switch_alarm_mode();
delay(200);
}
}
else if(state == LOW){
set_alarm_item();
delay(200);
}
}
void handle_Set_switch(){
int state = digitalRead(pinSet);
if (state == LOW && setItem > 0){
set_time();
delay(200);
}
else if (state == LOW && setItem == 0){
round_sleep();
delay(200);
}
}
void handle_Input_switch(){
int state = digitalRead(pinInput);
/*if (Set == LOW && setItem > 0){
set_time();
delay(200);
}
else */
if (state == LOW){
switch_input();
delay(200);
}
wait_to_release(pinInput);
}
void handle_Time_switch(){
int state = digitalRead(pinTime);
if(state == LOW && setItem == 0) {
int presstime = get_pin_delay(pinTime);
if (presstime > 10){
switch_set_item();
delay(200);
wait_to_release(pinTime);
}
else{
toggle_power();
}
}
else if(state == LOW){
switch_set_item();
delay(200);
}
}
int get_pin_delay(int pin){
int state = digitalRead(pin);
int delay_count = 0;
while(state == LOW && delay_count < 11)
{
state = digitalRead(pin);
delay(100);
delay_count = delay_count +1;
}
return delay_count;
}
void wait_to_release(int pin){
int state = digitalRead(pin);
while(state == LOW)
{
state = digitalRead(pin);
delay(100);
}
}

108
Wecker/radio.ino Normal file
View File

@@ -0,0 +1,108 @@
void round_sleep(){
if (minutes_sleep > 60){
minutes_sleep = 60;
}
else if (minutes_sleep > 45){
minutes_sleep = 45;
}
else if (minutes_sleep > 30){
minutes_sleep = 30;
}
else if (minutes_sleep > 15){
minutes_sleep = 15;
}
else if (minutes_sleep > 0){
minutes_sleep = 0;
}
else{
minutes_sleep = 90;
}
setSleepTimer(minutes_sleep);
}
void setSleepTimer(long Minutes){
if (Minutes == 0)
{
time_on = -1;
minutes_sleep = -1;
}
else{
unsigned long currentMillis = millis();
time_on = currentMillis + ((Minutes *60) *1000);
powerOn();
}
display_text("S " + String(Minutes));
delay_gui(1000);
}
void handleSleep(unsigned long intervall, unsigned long currentMillis){
if(time_on < 0){
return;
}
else if (currentMillis > time_on)
{
powerOff();
time_on = -1;
minutes_sleep = -1;
}
}
void switch_input(){
int currentIn = digitalRead(outInput);
if(currentIn == LOW){
switch_in(true);
}
else{
switch_radio(true);
}
}
void switch_radio(bool display_switch){
digitalWrite(outInput, LOW);
if(display_switch){
display_text("Radio");
delay_gui(500);
}
}
void switch_in(bool display_switch){
digitalWrite(outInput, HIGH);
if(display_switch){
display_text("In");
delay_gui(500);
}
}
void toggle_power(){
int currentState = digitalRead(outPower);
if (currentState == LOW){
powerOff();
}
else{
powerOn();
}
}
void powerOff(){
time_on = -1;
int currentState = digitalRead(outPower);
if (currentState != HIGH){
Serial.println("OFF");
digitalWrite(outPower, HIGH);
display_text("OFF");
delay_gui(1000);
}
switch_in(false);
}
void powerOn(){
int currentState = digitalRead(outPower);
if (currentState != LOW){
Serial.println("ON");
digitalWrite(outPower, LOW);
display_text("ON");
delay_gui(1000);
}
}