// C++ code
//
// coded using tinkercad. CODE does not reflect the circuit
//////////////// read ^^^^^^^^^^^^^^^^^^^^^^^^^^/////////////////
int RLed = 13;
int BLed = 12;
int GLed = 11;
int YLed = 10;
int WLed = 9;
int LDR_Pin = A0;
int Moisture_Pin = A1;
int NTC_Pin = A2;
void setup(){
Serial.begin(9600);
pinMode(RLed, OUTPUT);
pinMode(BLed, OUTPUT);
pinMode(GLed, OUTPUT);
pinMode(YLed, OUTPUT);
pinMode(WLed, OUTPUT);
pinMode(LDR_Pin, INPUT);
pinMode(Moisture_Pin, INPUT);
pinMode(NTC_Pin, INPUT);
}
void temperature(){
// https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
// below converts from analogue to celsius.
int analogTemp = analogRead(NTC_Pin);
int tempInC = map((analogTemp-20)*3.04, 0, 1023, -40, 125);
//maps analog temperature reading from its scale
//of 0 to 1023 bits to the temperature scale of -40 to 125 celsius.
if (tempInC<5){
digitalWrite(BLed, HIGH);
Serial.println("It is Cold. Blue LED is on");
}else{
digitalWrite(BLed, LOW);
}
Serial.print("Temperature: ");
Serial.print(tempInC);
Serial.println(" C");
delay(1000);
}
void timeOfDay(){
// https://wokwi.com/projects/347552240227058259
int analogLux = analogRead(LDR_Pin);
float lux = map(analogLux, 0, 1023, 0, 10000);
Serial.print("Light Level: ");
Serial.print(lux);
Serial.println(" lux");
delay(1000);
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<3000){ // 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>=3000){ // 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 nightDayTemperature(){
int analogLux = analogRead(LDR_Pin);
float lux = map(analogLux, 0, 1023, 0, 10000);
int analogTemp = analogRead(NTC_Pin);
int tempInC = map((analogTemp-20)*3.04, 0, 1023, -40, 125);
if (lux<200 && tempInC<5){ // night and temp below 5℃
digitalWrite(BLed, HIGH);
digitalWrite(RLed, HIGH);
Serial.println("It is Cold. Blue LED is on");
} else if ((lux>=200) && tempInC<5){ // day and temp below 5℃
digitalWrite(BLed, HIGH);
digitalWrite(GLed, HIGH);
Serial.println("It is Cold. Blue LED is on");
}
}
void waterSensor(){
int analogMoist = analogRead(Moisture_Pin);
int moistLevel = map(analogMoist + 147 , 147, 1023, 0, 100);
Serial.print("moisture level: ");
Serial.print(moistLevel);
Serial.println("%");
if (moistLevel<80){
digitalWrite(WLed, LOW);
delay(200);
digitalWrite(WLed,HIGH);
delay(200);
}else{
digitalWrite(WLed, HIGH);
}
}
void loop(){
waterSensor();
temperature();
timeOfDay();
nightDayTemperature();
}