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 0000000..b539547 Binary files /dev/null and b/brightness.png differ 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(); }