int RLed = 13;
int BLed = 12;
int GLed = 11;
int YLed = 10;
int WLed = 9;
int LDR_Pin = A0;
int NTC_Pin = A1;
int potentiometerPin = A2;
int val = 0;
// Thermistor characteristics:
const float BETA = 3950;
// LDR Characteristics:
const float GAMMA = 0.8;
// https://lednique.com/tag/ldr-gamma/
const float RL10 = 20;
// from the datasheet https://files.coventry.aula.education/6aee55e75412820efe43a09ee4298211ldr_datasheet_rs.pdf
// RL10 is the resistance of the LDR at 10 lux.
void setup() {
Serial.begin(9600);
pinMode(RLed, OUTPUT);
pinMode(BLed, OUTPUT);
pinMode(GLed, OUTPUT);
pinMode(YLed, OUTPUT);
pinMode(WLed, OUTPUT);
}
void temperature(){
// https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
// below converts from analogue to celsius.
int analogTemperature = analogRead(NTC_Pin);
float temperatureInCelsius = 1 / (log(1 / (1023. / analogTemperature - 1)) / BETA + 1.0 / 298.15) - 273.15;
if (temperatureInCelsius<5){
digitalWrite(BLed, HIGH);
}else{
digitalWrite(BLed, LOW);
}
Serial.print("Temperature: ");
Serial.print(temperatureInCelsius);
Serial.println(" ℃");
delay(1000);
}
void timeOfDay(){
int AnalogLDRVal = analogRead(LDR_Pin);
float voltage = AnalogLDRVal / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Light Level: ");
Serial.print(lux);
Serial.println(" lux");
delay(1000);
// https://wokwi.com/projects/347552240227058259
if (lux < 200) { // night
Serial.println("It is night time. Red LED is on.");
digitalWrite(RLed, HIGH);
digitalWrite(GLed, LOW);
digitalWrite(YLed, LOW);
delay(500);
} else if (lux>=200 && lux<800){ // day
Serial.println("It is day time. Green LED is on.");
digitalWrite(GLed, HIGH);
digitalWrite(RLed, LOW);
digitalWrite(YLed, LOW);
delay(500);
} else if (lux>=800){ // day and too bright
Serial.println("It is day time but it is too bright. Green and Yellow LED are on.");
digitalWrite(YLed, HIGH);
digitalWrite(GLed, HIGH);
delay(500);
}
}
void loop() {
val = analogRead(potentiometerPin);
int AnalogLDRVal = analogRead(LDR_Pin);
float voltage = AnalogLDRVal / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
int analogTemperature = analogRead(NTC_Pin);
float temperatureInCelsius = 1 / (log(1 / (1023. / analogTemperature - 1)) / BETA + 1.0 / 298.15) - 273.15;
timeOfDay();
temperature();
if (lux<200 && temperatureInCelsius<5){ // night and temp below 5℃
digitalWrite(BLed, HIGH);
digitalWrite(RLed, HIGH);
} else if ((lux>=200 && lux<800) && temperatureInCelsius<5){
digitalWrite(BLed, HIGH);
digitalWrite(GLed, HIGH);
}
}
// References:
// https://wokwi.com/projects/305193592908939842
// https://wokwi.com/projects/305193627138654786