/*
Wokwi | questions
can someone help me with this project please?
Lach$ff — 6/20/24 at 11:44 AM
https://wokwi.com/projects/401226418170277889
*/
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define NUM_LEDS 10
#define EEPROM_SIZE 900
#define TEMP_SENSOR_PIN A0
#define LDR_SENSOR_PIN A1
#define LED_RED_PIN 3
#define BEEP_PIN 2
const int BAR_LED_PINS[NUM_LEDS] = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4};
// NTC characteristic
const float BETA = 3950; // match the Beta spec of your thermistor
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
float temperatureSum = 0.0f;
float lightSum = 0.0f;
int readCount = 0;
int eepromIndex = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
float readLight() {
int analogValue = analogRead(LDR_SENSOR_PIN);
float voltage = analogValue / 1023.0 * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}
float readTemperature() {
int analogValue = analogRead(TEMP_SENSOR_PIN);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void storeData(float temp, float light) {
if (eepromIndex + 4 > EEPROM_SIZE) {
eepromIndex = 0;
}
EEPROM.put(eepromIndex, temp);
EEPROM.put(eepromIndex + 2, light);
eepromIndex += 4;
}
void updateDisplay(float temp, float light) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp, 1);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Luz : ");
lcd.print(light);
lcd.print(" lx");
}
void updateLedBar(float light) {
// 100 = 1, 100,000 = 10
int numLedsOn = (light / 100.0) * NUM_LEDS;
for (int i = 0; i < NUM_LEDS; i++) {
if (i < numLedsOn) {
digitalWrite(BAR_LED_PINS[i], HIGH);
} else {
digitalWrite(BAR_LED_PINS[i], LOW);
}
}
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(LED_RED_PIN, OUTPUT);
pinMode(BEEP_PIN, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(BAR_LED_PINS[i], OUTPUT);
}
// splash screen
lcd.setCursor(0, 0);
lcd.print("BlueShield");
lcd.setCursor(0, 1);
lcd.print("Inicializando...");
delay(2000);
lcd.clear();
}
void loop() {
float temperature = readTemperature();
float light = readLight();
Serial.print("Temp : ");
Serial.print(temperature, 1);
Serial.println("°C");
Serial.print("Light: ");
Serial.print(light, 0);
Serial.println(" lx\n");
temperatureSum += temperature;
lightSum += light;
readCount++;
if (readCount == 30) {
float avgTemp = temperatureSum / 30.0;
float avgLight = (lightSum / 30.0); // / 10.23;
if (avgTemp > 23.0) {
digitalWrite(LED_RED_PIN, HIGH);
tone(BEEP_PIN, 1000, 500);
storeData(avgTemp, avgLight);
} else {
digitalWrite(LED_RED_PIN, LOW);
noTone(BEEP_PIN);
}
updateDisplay(avgTemp, avgLight);
updateLedBar(avgLight);
temperatureSum = 0;
lightSum = 0;
readCount = 0;
}
delay(2000);
}