Last active
September 4, 2023 16:19
-
-
Save iGhost/7ff175326b13d1901401eea7b3ace51d to your computer and use it in GitHub Desktop.
Dim screen backlight depending on photosensor values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "microLiquidCrystal_I2C.h" | |
#include "GyverBME280.h" | |
#include <DHT.h> | |
#define ITERATION_MOD 60 // iterations to skip between measurements storing | |
#define HISTORY_SETS 2 | |
#define HISTORY_SIZE 20 | |
DHT dht22(2, DHT22); | |
DHT dht11(3, DHT11); | |
LiquidCrystal_I2C lcd(0x27, 20, 4); | |
GyverBME280 bme; | |
int history[HISTORY_SETS][HISTORY_SIZE]; | |
int plot_array[20]; | |
int plot_maxs[HISTORY_SETS] = {-32000, -32000}; | |
int plot_mins[HISTORY_SETS] = {32000, 32000}; | |
int averages[HISTORY_SETS][2] = {{0,0}, {0,0}}; | |
bool first_iteration = true; | |
unsigned int iteration = 0; | |
unsigned int brightness = 250; | |
unsigned int last_light = 0; | |
unsigned int light = 0; | |
bool bri = true; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(F("--===============**===============--")); | |
Serial.println(F("DHT temperature and humidity monitor")); | |
delay(50); | |
dht22.begin(); delay(30); | |
dht11.begin(); delay(30); | |
bme.begin(); | |
lcd.init(); | |
lcd.backlight(); | |
analogWrite(9, 100); | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(F("Initializing")); | |
delay(500); | |
for(byte i=0; i < 3; i++) { | |
lcd.print(F(".")); | |
delay(500); | |
} | |
initHistory(); | |
initPlot(); | |
} | |
// ===== Main Loop ===== | |
void loop() { | |
// Serial.print(F("Light: ")); Serial.print(analogRead(A0)); Serial.println(""); | |
// for (int i=0;i<26;i++) { | |
// if(bri) { | |
// brightness = i*10; | |
// } else { | |
// brightness = 250 - i*10; | |
// } | |
// analogWrite(9, brightness); | |
// delay(30); | |
// } | |
// if(bri == true) { bri = false; } else { bri = true; } | |
Serial.print(F("Light: ")); Serial.print(analogRead(A0)); Serial.println(""); | |
light = analogRead(A0); | |
if(light > 600) { | |
brightness = 255; | |
} else { | |
brightness = light < 20 ? 0: light/2; | |
} | |
analogWrite(9, brightness); | |
dhtValues(dht22, 0, 3, history[0]); | |
dhtValues(dht11, 1, 2, history[1]); | |
bmeValues(bme); | |
if((iteration % ITERATION_MOD == 0) || (iteration == 0)) { | |
drawPlot(history[1], 0, 1, 20, 1, plot_mins[1], plot_maxs[1], 0); | |
// drawPlot(history[1], 0, 3, 20, 1, plot_mins[1], plot_maxs[1], 0); | |
} | |
++iteration; | |
delay(3000); | |
} | |
void bmeValues(GyverBME280 bme) { | |
float temperature = bme.readTemperature(); | |
float humidity = bme.readHumidity(); | |
float pressurePa = bme.readPressure(); | |
float pressure = pressureToMmHg(pressurePa); | |
float altitude = pressureToAltitude(pressurePa); | |
Serial.print(F("BME")); | |
Serial.print(F(": Temperature: ")); Serial.print(temperature, 1); | |
Serial.print(F("°C Humidity: ")); Serial.print(humidity, 1); | |
Serial.print(F("% Pressure: ")); Serial.print(pressure, 1); | |
Serial.print(F("mm Hg; Altitude: ")); Serial.print(altitude, 1); | |
Serial.println(""); | |
lcd.setCursor(0, 0); | |
lcd.print(F("B ")); | |
lcd.print(temperature, 1); lcd.print(F(" C ")); | |
lcd.print(humidity, 1); lcd.print(F("%")); | |
} | |
void dhtValues(DHT &sensor, int index, int lcdLine, int* history_set) { | |
float h = sensor.readHumidity(); | |
float t = sensor.readTemperature(); | |
if (h > plot_maxs[index]) plot_maxs[index] = h + 1; | |
if (h < plot_mins[index]) plot_mins[index] = h - 1; | |
if (isnan(h) || isnan(t)) { | |
Serial.println(F("Failed to read from DHT sensor!")); | |
lcd.setCursor(6, lcdLine); lcd.print(F("Failed!")); delay(2000); | |
return; | |
} | |
if(first_iteration) { | |
initHistory(index, h); | |
first_iteration = false; | |
} | |
if((iteration % ITERATION_MOD == 0) || (iteration == 0)) { | |
countAverage(index, h); | |
pushToHistory(history_set, averages[index][0]); | |
resetAverage(index); | |
iteration = 0; | |
} else { | |
countAverage(index, h); | |
} | |
lcd.setCursor(0, lcdLine); | |
lcd.print(F("D")); lcd.print(index); lcd.print(F(" ")); | |
lcd.print(t, 1); lcd.print(F(" C ")); | |
lcd.print(h, 1); lcd.print(F("%")); | |
Serial.print(F("DHT")); Serial.print(index); | |
Serial.print(F(": Temperature: ")); Serial.print(t, 1); | |
Serial.print(F("°C Humidity: ")); Serial.print(h, 1); | |
Serial.print(F("% Heat index: ")); | |
Serial.print(sensor.computeHeatIndex(t, h, false)); | |
Serial.println(""); | |
} | |
void countAverage(int index, int h) { | |
if(averages[index][0] != 0) { | |
averages[index][0] = (averages[index][0] * averages[index][1] + h) / (averages[index][1] + 1); | |
averages[index][1] += 1; | |
} else { | |
averages[index][0] = h; | |
averages[index][1] = 1; | |
} | |
} | |
void resetAverage(int index) { | |
averages[index][0] = 0; | |
averages[index][1] = 0; | |
} | |
void initHistory() { | |
for(int j=0; j < HISTORY_SETS; j++) | |
for(int i=0; i < HISTORY_SIZE; i++) | |
history[j][i] = 0; | |
} | |
void initHistory(int index, int value) { | |
for(int i=0; i < HISTORY_SIZE; i++) | |
history[index][i] = value; | |
} | |
void printHistory(int* history_set) { | |
Serial.print(F("History: ")); | |
for(int i=0; i < HISTORY_SIZE; i++) { | |
Serial.print(history_set[i]); | |
Serial.print(F(" ")); | |
} | |
Serial.println(""); | |
} | |
void pushToHistory(int* &history_set, int value) { | |
for(int i=0; i < HISTORY_SIZE; i++) { | |
if(i < HISTORY_SIZE-1) { | |
history_set[i] = history_set[i+1]; | |
} | |
} | |
history_set[HISTORY_SIZE-1] = value; | |
} | |
void initPlot() { | |
byte row8[8] = {0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111}; | |
byte row7[8] = {0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111}; | |
byte row6[8] = {0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111}; | |
byte row5[8] = {0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111}; | |
byte row4[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111, 0b11111}; | |
byte row3[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111}; | |
byte row2[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111}; | |
byte row1[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111}; | |
lcd.createChar(0, row8); | |
lcd.createChar(1, row1); | |
lcd.createChar(2, row2); | |
lcd.createChar(3, row3); | |
lcd.createChar(4, row4); | |
lcd.createChar(5, row5); | |
lcd.createChar(6, row6); | |
lcd.createChar(7, row7); | |
} | |
void drawPlot(int* plot_array, byte pos, byte row, byte width, byte height, int min_val, int max_val, int fill_val) { | |
for (byte i = 0; i < width; i++) { | |
byte infill, fract; | |
infill = floor((float)(plot_array[i] - min_val) / (max_val - min_val) * height * 10); | |
fract = (infill % 10) * 8 / 10; | |
infill = infill / 10; | |
for (byte n = 0; n < height; n++) { | |
if (n < infill && infill > 0) { | |
lcd.setCursor(pos + i, (row - n)); | |
lcd.write(0); | |
} | |
if (n >= infill) { | |
lcd.setCursor(pos + i, (row - n)); | |
if (fract > 0) { | |
lcd.write(fract); | |
} else { | |
lcd.write(16); | |
} | |
for (byte k = n + 1; k < height; k++) { | |
lcd.setCursor(pos + i, (row - k)); | |
lcd.write(16); | |
} | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment