LasClock with DS3231 RTC modul

This commit is contained in:
mrbesen 2019-01-09 14:51:24 +01:00
parent 4a7324888b
commit da862a99a4
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
1 changed files with 49 additions and 71 deletions

View File

@ -3,9 +3,14 @@
*
* Program for a binary laser clock
*
* Setup: a Button on pin 12, Led pin 13, Laser / LED on pin 2-11 needs to be pwm!
* Setup: a Button on pin 12, Led pin 13, Laser / LED on pin 2-11 needs to be pwm! RTC-Modul DS3231 required
*
*/
#include <Wire.h>
#include "DS3231.h"
DS3231 clock;
#define pressed digitalRead(12) == LOW
//pins for hour display
@ -13,21 +18,10 @@ const unsigned char H[] = {8,9,10,11};
//pins for min display
const unsigned char M[] = {2,3,4,5,6,7};
//how long takes a minute in ms?
//const unsigned int mindelay = 60000;
const unsigned int mindelay = 59000;
//intensity of laser, when "off": 0 -> 0% on; 255 -> 100% on
const unsigned int offtime = 160;
//current time
unsigned char min = 0;
unsigned char hour = 0;
//last time the minute changed
unsigned long lastmin;
//remove this line to enable Serial
#define nolog
bool houtremp;
void set(const unsigned char pin, bool on) {
if(on)
@ -48,58 +42,50 @@ void write(unsigned char m = 255, unsigned char h = 255) {
set(H[i], h & (1 << i));
// analogWrite(H[i], (h & (1 << i)) ? 255 : offtime);
}
#ifndef nolog
//send data to Serial
Serial.print(hour);
Serial.print(":");
Serial.println(min);
#endif
}
//add one minute to the time
void countTime() {
if(60 == ++min) {
min = 0;
if(13 == ++hour) {
hour = 1;
}
}
inline void writeTime() {
write(clock.getMinute(), clock.getHour(houtremp, houtremp));
}
void setup() {
pinMode(13, OUTPUT);
#ifndef nolog
Serial.begin(9600);
#endif
//Setup pins
for(unsigned char m = 0; m < 6; m++) {
pinMode(M[m], OUTPUT);
}
for(unsigned char h = 0; h < 4; h++) {
pinMode(H[h], OUTPUT);
}
pinMode(12, INPUT_PULLUP);
write();
while(!pressed) delay(5);
//setup time
char mode = 0;//mode = 0-> set hour, mode = 1 -> set min, mode = 2 -> exit setup
bool pres = false;//was button pressed
unsigned long timeout = millis();
while(mode < 2) {
if(pressed) {//button is pressed
//setup clock modul
Wire.begin();
clock.setClockMode(true); //set 12h format
clock.turnOffAlarm(1);//disable Arlam
clock.turnOffAlarm(2);
//Setup pins
for(unsigned char m = 0; m < 6; m++) {
pinMode(M[m], OUTPUT);
}
for(unsigned char h = 0; h < 4; h++) {
pinMode(H[h], OUTPUT);
}
pinMode(12, INPUT_PULLUP);
write();
while(!pressed) delay(5);//wait for press
//setup time
char mode = 0;//mode = 0-> set hour, mode = 1 -> set min, mode = 2 -> exit setup
bool pres = true;//was button pressed
unsigned long timeout = millis();
writeTime();
while(mode < 2) {
if(pressed) {//button is pressed
if(!pres) {//button was not pressed before
timeout = millis();//reset timeout
pres = true;
if(mode == 0) {//count hour
if(++hour == 13)
hour = 1;
byte hour = clock.getHour(houtremp, houtremp)+1;
clock.setHour(hour == 13 ? 1 : hour);
} else {//count min
min ++;
if(min == 60)
min = 0;
byte minute = clock.getMinute()+1;
clock.setMinute(minute == 60 ? 0 : minute);
}
write(min, hour);
writeTime();
}
} else {
pres = false;
@ -112,33 +98,25 @@ void setup() {
write();
delay(300);
}
write(min, hour);
writeTime();
++mode;//next mode
//reset timeout
timeout = millis();
}
delay(50);
}
lastmin = millis();
}
void loop() {
if((millis() - lastmin) >= mindelay) {
//update min
countTime();
write(min, hour);
lastmin += mindelay;
} else {
#ifndef nolog
Serial.print(millis()-lastmin);
Serial.print(" ");
Serial.println(millis()-lastmin >= mindelay);
#endif
}
//update min
byte hour = clock.getHour(houtremp, houtremp);
if(hour > 12)
hour -=12;
clock.setHour(hour);
write(clock.getMinute(), hour);
//let the led blink
if(millis()/1000 % 2 == 0) {
digitalWrite(13, HIGH);
} else
digitalWrite(13, LOW);
delay(50);
digitalWrite(13, millis()/1000 % 2 == 0);
delay(500);
}