1

Added photoresistor and amb. light compensation

+ Added ambient light compensation logic
+ Added photocell on scheme
+ Added photoresitor to README.md

--HG--
branch : phtoresistor
This commit is contained in:
eleksir
2019-05-21 16:00:01 +03:00
parent c7ed04e987
commit 352dc5fa4c
3 changed files with 42 additions and 2 deletions

View File

@@ -12,6 +12,8 @@
* usb phone charger as power supply * usb phone charger as power supply
* 2 push buttons * 2 push buttons
* 1 on/off switch * 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мм) * Gainta G521G enclosure (92x66.5x28мм)
* 5m power cord * 5m power cord

Binary file not shown.

View File

@@ -61,6 +61,10 @@ bool dot = true;
/* time inaccuracy compensation flag */ /* time inaccuracy compensation flag */
uint8_t compflag = 0; 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() void setup()
{ {
@@ -70,7 +74,12 @@ void setup()
byte digitPins[] = {9, 10, 11, 12}; byte digitPins[] = {9, 10, 11, 12};
byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 13}; byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 13};
bool resistorsOnSegments = true; // 8 resistors, 330 Ohms each 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 /* without it there is visual bug at 0 o'clock, and in current version
* of SevSeg' setChars() does not dots at all */ * of SevSeg' setChars() does not dots at all */
@@ -96,6 +105,9 @@ void setup()
// setup control buttons // setup control buttons
pinMode(hrs_btn, INPUT_PULLUP); pinMode(hrs_btn, INPUT_PULLUP);
pinMode(min_btn, INPUT_PULLUP); pinMode(min_btn, INPUT_PULLUP);
// setup brigtness sensor
pinMode(photoPin, INPUT);
} }
void loop() void loop()
@@ -104,6 +116,7 @@ void loop()
currentPushTimer = currentMillis; currentPushTimer = currentMillis;
char str[4]; char str[4];
float d; float d;
unsigned int ambLight = analogRead(photoPin);
clock.getTime(); clock.getTime();
@@ -162,7 +175,7 @@ void loop()
if ((clock.hour == 21) and (clock.minute == 21) and if ((clock.hour == 21) and (clock.minute == 21) and
(clock.second == 21) and (compflag == 0)) { (clock.second == 21) and (compflag == 0)) {
clock.fillByHMS(clock.hour, clock.minute, 14); clock.fillByHMS(clock.hour, clock.minute, (clock.second - timeSkew));
clock.setTime(); clock.setTime();
compflag = 1; 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(); Display.refreshDisplay();
} }