Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- Combined LDR and Backlight Control with Smoothing and Presets
- - Automatically adjusts screen brightness based on ambient light
- - Adds smoothing to avoid flicker
- - Includes Day/Night preset brightness levels
- *******************************************************************/
- #include <TFT_eSPI.h>
- TFT_eSPI tft = TFT_eSPI();
- #define LCD_BACK_LIGHT_PIN 21 // PWM pin for screen backlight
- #define LDR_PIN 34 // Analog pin connected to LDR
- // PWM settings
- const int pwmChannel = 0;
- const int pwmFreq = 5000; // 5kHz PWM frequency
- const int pwmResolution = 12; // 12-bit resolution (0-4095)
- // Smoothing settings
- const int numReadings = 10;
- int readings[numReadings]; // Array to store LDR readings
- int readIndex = 0;
- long total = 0;
- int average = 0;
- string curr_bg_color = "";
- string curr_text_color = "";
- // Brightness presets
- const int DAY_BRIGHTNESS = 4095; // Full brightness
- const int NIGHT_BRIGHTNESS = 300; // Low brightness
- void setup() {
- Serial.begin(115200);
- tft.init();
- tft.setRotation(1);
- tft.fillScreen(TFT_BLACK);
- tft.setTextColor(TFT_WHITE, TFT_BLACK);
- tft.setTextSize(2);
- tft.setCursor(0, 0);
- tft.println("Auto Brightness Display");
- analogReadResolution(12); // Ensure analog input is 0–4095
- // Setup PWM for backlight
- ledcSetup(pwmChannel, pwmFreq, pwmResolution);
- ledcAttachPin(LCD_BACK_LIGHT_PIN, pwmChannel);
- // Initialize smoothing array
- for (int i = 0; i < numReadings; i++) {
- readings[i] = 0;
- }
- }
- void loop() {
- total = total - readings[readIndex];
- readings[readIndex] = analogRead(LDR_PIN);
- total = total + readings[readIndex];
- readIndex = (readIndex + 1) % numReadings;
- average = total / numReadings;
- Serial.print("Smoothed LDR Value: ");
- Serial.println(average);
- // Determine brightness preset based on ambient light
- int brightness;
- if (average < 1500) {
- brightness = DAY_BRIGHTNESS; // Bright outside
- lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(255, 255, 255), LV_PART_MAIN);
- lv_obj_set_style_text_color(table, lv_color_black(), LV_PART_ITEMS);
- lv_obj_set_style_bg_color(table, lv_color_white(), LV_PART_MAIN);
- curr_bg_color = "255, 255, 255";
- curr_text_color = "0, 0, 0";
- } else {
- brightness = NIGHT_BRIGHTNESS; // Dark environment
- lv_obj_set_style_bg_color(lv_scr_act(), lv_color_make(30, 30, 30), LV_PART_MAIN);
- lv_obj_set_style_text_color(table, lv_color_white(), LV_PART_ITEMS);
- //lv_obj_set_style_bg_color(table, lv_color_black(), LV_PART_MAIN);
- lv_obj_set_style_bg_color(table, lv_color_make(30, 30, 30), LV_PART_MAIN);
- curr_bg_color = "30, 30, 30";
- curr_text_color = "255, 255, 255";
- }
- ledcWrite(pwmChannel, brightness);
- delay(200);
- }
- void cell_color_mod(int row, int col, string col_color, string text_color) {
- lv_table_set_cell_text_color(table, row, col, lv_color_make(text_color));
- lv_table_set_cell_bg_color(table, row, col, lv_color_make(col_color));
- //lv_table_set_cell_bg_color(table, row, col, lv_palette_main(LV_PALETTE_RED));
- }
Advertisement
Comments
-
- https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/4-BacklightControlTest/4-BacklightControlTest.ino
- https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/blob/main/Examples/Basics/5-LDRTest/5-LDRTest.ino
Add Comment
Please, Sign In to add comment