// Numbers for the display found at the following address:
// https://lb-community.s3.amazonaws.com/uploads/supporting_file/asset/457/Dice.ino
int numbers [] = {0,3,6,8,10,13,15,18,20,23,26,28,30,33,35,38,40,43,45,48,50,53,55,58,61,
64,66,69,71,74,76,78,81,84,86,89,91,94,96,99,102,104,106,108,111,114,116,
119,121,124,127,129,132,134,136,139,141,143,146,149,151,154,156,159,160,163,
166,169,171,173,176,179,181,184,186,188,190,193,196,199,202,205,208,210,212,
214,217,218,221,224,227,230,232,235,237,239,242,245,247,252,255};
// Time count - 1000milli = 1sec
const unsigned long MLEN = 1000;
const unsigned int MAX_HOUR = 24;
const unsigned int MAX_MINS = 60;
const unsigned int MAX_SECS = 60;
// output
int digo = 1;
int mo = 5;
int so = 9;
// input
int digi = 0;
int mi = A0;
int si = A1;
// Constants for measuring the button state
const int BDOWN = 1;
const int BUP = 0;
const int MODEINP = 1;
const int MODECLK = -1;
// The variables that handle the state of the LED
int btnState = 0;
int lstState = 0;
// The state of the clock
int CLOCK_STATE = -1;
// Clock times
unsigned long baset = 0;
unsigned long start = 0;
int the_hr = 0;
int the_mn = 0;
// functions
unsigned long setBaseT() {
return millis() + (MAX_SECS * MLEN);
}
// Start up and set the pin modes
void setup() {
// Input
pinMode(digi, INPUT);
pinMode(mi, INPUT);
pinMode(si, INPUT);
// 7-segment display
pinMode(digo, OUTPUT);
pinMode(mo, OUTPUT);
pinMode(so, OUTPUT);
// Set the default
baset = setBaseT();
}
// Main loop
void loop() {
if (CLOCK_STATE == -1) {
// Update time
if (millis() > baset) {
the_mn += 1;
if (the_mn == MAX_MINS) {
the_mn = 0;
the_hr += 1;
if (the_hr == MAX_HOUR) {
the_hr = 0;
}
}
// Update the display
analogWrite(mo, numbers[the_hr]);
analogWrite(so, numbers[the_mn]);
// Update baset
baset = setBaseT();
// Short delay
delay(50);
} else { // when millis() rolls over
if (millis() < start) {
baset = setBaseT();
}
}
} else {
// Get analog input
int ha = analogRead(mi);
int ma = analogRead(si);
// Map it to the range
the_hr = map(ha, 0, 1020, 0, 23);
the_mn = map(ma, 0, 1020, 0, 59);
// Write it out to the display
analogWrite(mo, numbers[the_hr]);
analogWrite(so, numbers[the_mn]);
delay(100);
}
// Read the input toward the end
btnState = digitalRead(digi);
// Change state last
if (btnState == BDOWN) {
if (lstState == BUP) {
CLOCK_STATE *= -1;
lstState = BDOWN;
// Short delay for the humans
delay(50);
if (CLOCK_STATE == 1) {
digitalWrite(digo, HIGH);
} else {
digitalWrite(digo, LOW);
baset = setBaseT();
start = millis();
}
}
}
else {
lstState = BUP;
}
}
Comments