diff --git a/README.md b/README.md index 17eb81f..260196e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ * usb phone charger as power supply * 2 push buttons * 1 on/off switch +* 1 photoresistor +* 1 resistor to make secund shoulder of photosensitive "bridge", 10 kOhms, 0.25 watts * Gainta G521G enclosure (92x66.5x28ΠΌΠΌ) * 5m power cord diff --git a/clock_schem.pdf b/clock_schem.pdf index ef901b3..cf05b18 100644 Binary files a/clock_schem.pdf and b/clock_schem.pdf differ diff --git a/simpleclock.ino b/simpleclock.ino index eb1ecbe..1f3f920 100644 --- a/simpleclock.ino +++ b/simpleclock.ino @@ -61,6 +61,10 @@ bool dot = true; /* time inaccuracy compensation flag */ uint8_t compflag = 0; +const uint8_t timeSkew = 6; // assume that clock runs faster than should + +/* ambient light compensation aka night mode */ +const int photoPin = A2; void setup() { @@ -70,7 +74,12 @@ void setup() byte digitPins[] = {9, 10, 11, 12}; byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 13}; bool resistorsOnSegments = true; // 8 resistors, 330 Ohms each - bool updateWithDelays = false; // Default. Recommended + /* updateWithDelays defaults to false and it is recommended value, + * but i was unable to make my display to change brightness with + * recommended value, so i have to use updateWithDelays=true, + * in this case brightness gets changed. + */ + bool updateWithDelays = true; // Default. Recommended /* without it there is visual bug at 0 o'clock, and in current version * of SevSeg' setChars() does not dots at all */ @@ -96,6 +105,9 @@ void setup() // setup control buttons pinMode(hrs_btn, INPUT_PULLUP); pinMode(min_btn, INPUT_PULLUP); + + // setup brigtness sensor + pinMode(photoPin, INPUT); } void loop() @@ -104,6 +116,7 @@ void loop() currentPushTimer = currentMillis; char str[4]; float d; + unsigned int ambLight = analogRead(photoPin); clock.getTime(); @@ -162,7 +175,7 @@ void loop() if ((clock.hour == 21) and (clock.minute == 21) and (clock.second == 21) and (compflag == 0)) { - clock.fillByHMS(clock.hour, clock.minute, 14); + clock.fillByHMS(clock.hour, clock.minute, (clock.second - timeSkew)); clock.setTime(); compflag = 1; } @@ -191,6 +204,31 @@ void loop() } } + /* ambient light compensation + * ambLight can be in range of 0..1023, if it 800, it has point to use fullbright + */ + if (ambLight >= 940) { + Display.setBrightness(100); + } else if ((ambLight < 900) and (ambLight >= 840)) { + Display.setBrightness(95); + } else if ((ambLight < 800) and (ambLight >= 735)) { + Display.setBrightness(70); + } else if ((ambLight < 700) and (ambLight >= 635)) { + Display.setBrightness(25); + } else if ((ambLight < 600) and (ambLight >= 530)) { + Display.setBrightness(15); + } else if ((ambLight < 500) and (ambLight >= 430)) { + Display.setBrightness(12); + } else if ((ambLight < 400) and (ambLight >= 325)) { + Display.setBrightness(10); + } else if ((ambLight < 300) and (ambLight >= 220)) { + Display.setBrightness(8); + } else if ((ambLight < 200) and (ambLight >= 110)) { + Display.setBrightness(7); + } else if (ambLight < 100) { + Display.setBrightness(6); + } + Display.refreshDisplay(); }