Arduino/LaserClock/LaserClock.ino

135 lines
2.7 KiB
C++

/*
* author: MrBesen
*
* 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!
*
*/
#define pressed digitalRead(12) == LOW
//pins for hour display
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 = 59500;
//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
//prints the data to the lasers, write(0,0)-> all off, write()-> all on
void write(unsigned char m = 255, unsigned char h = 255) {
//write minutes
for(unsigned char i = 0; i < 6; i++) {
analogWrite(M[i], (m & (1 << i)) ? 255 : 1);
}
//write hour
for(unsigned char i = 0; i < 4; i++) {
analogWrite(H[i], (h & (1 << i)) ? 255 : 4);
}
#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 = 0;
}
}
}
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
if(!pres) {//button was not pressed before
timeout = millis();//reset timeout
pres = true;
if(mode == 0) {//count hour
hour ++;
if(hour == 13)
hour = 1;
} else {//count min
min ++;
if(min == 60)
min = 0;
}
write(min, hour);
}
} else {
pres = false;
}
if(millis() - timeout > 10000) {//timed out
//blink 6 times
for(int i = 0; i < 6; i++) {
write(0,0);
delay(300);
write();
delay(300);
}
write(min, hour);
++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
}
//let the led blink
if(millis()/1000 % 2 == 0) {
digitalWrite(13, HIGH);
} else
digitalWrite(13, LOW);
delay(50);
}