LasClock with DS3231 RTC modul
This commit is contained in:
parent
4a7324888b
commit
da862a99a4
|
@ -3,9 +3,14 @@
|
||||||
*
|
*
|
||||||
* Program for a binary laser clock
|
* 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
|
#define pressed digitalRead(12) == LOW
|
||||||
|
|
||||||
//pins for hour display
|
//pins for hour display
|
||||||
|
@ -13,21 +18,10 @@ const unsigned char H[] = {8,9,10,11};
|
||||||
//pins for min display
|
//pins for min display
|
||||||
const unsigned char M[] = {2,3,4,5,6,7};
|
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
|
//intensity of laser, when "off": 0 -> 0% on; 255 -> 100% on
|
||||||
const unsigned int offtime = 160;
|
const unsigned int offtime = 160;
|
||||||
|
|
||||||
//current time
|
bool houtremp;
|
||||||
unsigned char min = 0;
|
|
||||||
unsigned char hour = 0;
|
|
||||||
|
|
||||||
//last time the minute changed
|
|
||||||
unsigned long lastmin;
|
|
||||||
|
|
||||||
//remove this line to enable Serial
|
|
||||||
#define nolog
|
|
||||||
|
|
||||||
void set(const unsigned char pin, bool on) {
|
void set(const unsigned char pin, bool on) {
|
||||||
if(on)
|
if(on)
|
||||||
|
@ -48,58 +42,50 @@ void write(unsigned char m = 255, unsigned char h = 255) {
|
||||||
set(H[i], h & (1 << i));
|
set(H[i], h & (1 << i));
|
||||||
// analogWrite(H[i], (h & (1 << i)) ? 255 : offtime);
|
// 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
|
inline void writeTime() {
|
||||||
void countTime() {
|
write(clock.getMinute(), clock.getHour(houtremp, houtremp));
|
||||||
if(60 == ++min) {
|
|
||||||
min = 0;
|
|
||||||
if(13 == ++hour) {
|
|
||||||
hour = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(13, OUTPUT);
|
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
|
//setup clock modul
|
||||||
char mode = 0;//mode = 0-> set hour, mode = 1 -> set min, mode = 2 -> exit setup
|
Wire.begin();
|
||||||
bool pres = false;//was button pressed
|
clock.setClockMode(true); //set 12h format
|
||||||
unsigned long timeout = millis();
|
clock.turnOffAlarm(1);//disable Arlam
|
||||||
while(mode < 2) {
|
clock.turnOffAlarm(2);
|
||||||
if(pressed) {//button is pressed
|
|
||||||
|
//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
|
if(!pres) {//button was not pressed before
|
||||||
timeout = millis();//reset timeout
|
timeout = millis();//reset timeout
|
||||||
pres = true;
|
pres = true;
|
||||||
if(mode == 0) {//count hour
|
if(mode == 0) {//count hour
|
||||||
if(++hour == 13)
|
byte hour = clock.getHour(houtremp, houtremp)+1;
|
||||||
hour = 1;
|
clock.setHour(hour == 13 ? 1 : hour);
|
||||||
} else {//count min
|
} else {//count min
|
||||||
min ++;
|
byte minute = clock.getMinute()+1;
|
||||||
if(min == 60)
|
clock.setMinute(minute == 60 ? 0 : minute);
|
||||||
min = 0;
|
|
||||||
}
|
}
|
||||||
write(min, hour);
|
writeTime();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pres = false;
|
pres = false;
|
||||||
|
@ -112,33 +98,25 @@ void setup() {
|
||||||
write();
|
write();
|
||||||
delay(300);
|
delay(300);
|
||||||
}
|
}
|
||||||
write(min, hour);
|
writeTime();
|
||||||
++mode;//next mode
|
++mode;//next mode
|
||||||
//reset timeout
|
//reset timeout
|
||||||
timeout = millis();
|
timeout = millis();
|
||||||
}
|
}
|
||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
lastmin = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if((millis() - lastmin) >= mindelay) {
|
//update min
|
||||||
//update min
|
byte hour = clock.getHour(houtremp, houtremp);
|
||||||
countTime();
|
if(hour > 12)
|
||||||
write(min, hour);
|
hour -=12;
|
||||||
lastmin += mindelay;
|
clock.setHour(hour);
|
||||||
} else {
|
write(clock.getMinute(), hour);
|
||||||
#ifndef nolog
|
|
||||||
Serial.print(millis()-lastmin);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.println(millis()-lastmin >= mindelay);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
//let the led blink
|
//let the led blink
|
||||||
if(millis()/1000 % 2 == 0) {
|
digitalWrite(13, millis()/1000 % 2 == 0);
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
} else
|
delay(500);
|
||||||
digitalWrite(13, LOW);
|
|
||||||
delay(50);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue