From a077f2e7e7da45764d636dbb97e166d3e0843fad Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 2 Mar 2024 19:08:16 +0100 Subject: [PATCH] feat: automatic brightness adjustment --- README.md | 13 +++++++++++++ brightness.png | Bin 0 -> 4272 bytes esp/src/main.cpp | 24 +++++++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 brightness.png diff --git a/README.md b/README.md index c292107..d9369ef 100644 --- a/README.md +++ b/README.md @@ -95,3 +95,16 @@ Leaving... Hard resetting via RTS pin... =================================================== [SUCCESS] Took 23.76 seconds ==================================================== ``` + +## Automatic Brightness Adjustment + +You can attach a voltage divider with a photo resistor as pull-down and a regular resistor as pull-up to the ESP's8266 ADC. +If you choose to do this, please check the ADC voltage level of your ESP8266 board: While the ADC itself works in the range 0.0V...1.0V, a lot of breakout boards scale that up to 0.0V...3.3V. + +Example with a NodeMCU board: + +![brightness.png][brightness.png] + +You can use the values `ADC_MAX`, `ADC_MIN`, `BRIGHT_MIN` and `BRIGHT_MAX` at the top of the file `esp/src/main.cpp` to fine-tune the sensitivity and thresholds of the auto adjustment. + +If you do not use this feature, and observe some flickering, please short the ADC to GND. diff --git a/brightness.png b/brightness.png new file mode 100644 index 0000000000000000000000000000000000000000..b53954746cc235ae97ea665e5eaa79407f2de4a1 GIT binary patch literal 4272 zcmb7Idpy(o|CiE5p%Th%N+l(7q&RaaDsyR43CT)oE|bYJGPYB>RB|m6Mx=65BZer~ zE=yz6a^1#ASIIM-F4i0#mkOIr8Tb%{44HdY?7;!T&~$-K5TRZ9cK_B1d^kB6+OP1 z&&sWY*L~bFE)Lf$B~Beopr7m#(Dh23z=tNVRxPG{jHN>(Q0Q2Drxgk{xSSJc_UMSxZ<`LsUq-&ZelCaxO^KCnj;nVbrN4 zxPKO%;v?8kv!vl8pn2=wKw#*)hfU!yct`}aKO?T_71Q*&Lng2VX%Cd65LZ$%(AFVM zXVTgYR(qJIp2RcR&UFwY7H!tqPSG;)^hP?l>C%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v9;&8f@(2e2}>2LJ#7 literal 0 HcmV?d00001 diff --git a/esp/src/main.cpp b/esp/src/main.cpp index f3ec916..589c7a4 100644 --- a/esp/src/main.cpp +++ b/esp/src/main.cpp @@ -6,6 +6,11 @@ Adafruit_NeoPixel pixels(60, 5, NEO_GRB + NEO_KHZ800); +uint16_t ADC_MAX = 900; +uint16_t ADC_MIN = 250; +uint8_t ADC_INVERT = 1; +uint8_t BRIGHT_MIN = 50; +uint8_t BRIGHT_MAX = 255; const char *ssid = put your ssid here; const char *password = put your psk here; @@ -34,6 +39,15 @@ uint8_t sixtyToPixel(int8_t sixty) { void loop() { delay(1000); + uint16_t brightness = analogRead(A0); + uint8_t bscale = (uint8_t) (255*(min(max(brightness, ADC_MIN), ADC_MAX) - ADC_MIN) / (ADC_MAX - ADC_MIN)); + if (ADC_INVERT) { + bscale = 255 - bscale; + } + bscale = min(max(bscale, BRIGHT_MIN), BRIGHT_MAX); + Serial.print(brightness); + Serial.print(" -> "); + Serial.println(bscale); ntp.update(); Serial.println(ntp.formattedTime("%T")); uint8_t h = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12)); @@ -42,10 +56,10 @@ void loop() { uint8_t m = sixtyToPixel(ntp.minutes()); uint8_t s = sixtyToPixel(ntp.seconds()); pixels.clear(); - pixels.setPixelColor(hl, pixels.getPixelColor(hl) | 0x00ff0000); - pixels.setPixelColor(h, pixels.getPixelColor(h) | 0x00ff0000); - pixels.setPixelColor(hr, pixels.getPixelColor(hr) | 0x00ff0000); - pixels.setPixelColor(m, pixels.getPixelColor(m) | 0x0000ff60); - pixels.setPixelColor(s, pixels.getPixelColor(s) | 0x000000ff); + pixels.setPixelColor(hl, pixels.getPixelColor(hl) | (bscale << 16)); + pixels.setPixelColor(h, pixels.getPixelColor(h) | (bscale << 16)); + pixels.setPixelColor(hr, pixels.getPixelColor(hr) | (bscale << 16)); + pixels.setPixelColor(m, pixels.getPixelColor(m) | (bscale << 8) | (bscale >> 2)); + pixels.setPixelColor(s, pixels.getPixelColor(s) | bscale); pixels.show(); }