const float BETA = 3950; //from wokwi-ntc-temperature-sensor reference (BETA CONSTANT)
int pushButton = LOW;
void setup() {
pinMode(A0, INPUT); //temperature sensor
pinMode(7, INPUT); //push button
pinMode(2, OUTPUT); //blue LED
pinMode(3, OUTPUT); //green LED
pinMode(4, OUTPUT); //red LED
Serial.begin(9600);
}
void loop() {
pushButton = digitalRead(7);
int temperature = readTemperature();
if (pushButton == HIGH) { //Start checking temperature when push button is HIGH
delay(55); //an attempt to debounce push button
/*
- If Temperature is less than 0 degrees C, Blue LED is on
- If Temperature is greater than 0 degrees & less than 40 degrees C, Green LED is on
- If Temperature is greater than 40 degrees C, Red LED is on
*/
if (temperature < 0) {
digitalWrite(2, HIGH); //blue LED is on
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (temperature > 40) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH); //red LED is on
} else if (temperature < 40 && temperature > 0) {
digitalWrite(2, LOW);
digitalWrite(3, HIGH); //green LED is on
digitalWrite(4, LOW);
}
Serial.println(String(temperature)); //print temp in C degrees when any LED is on
} else if (pushButton == LOW) { //all LEDs stay LOW when push button isn't pressed
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
}
//Utilized wokwi-ntc-temperature-sensor reference for the NTC temperature sensor
int readTemperature() {
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1 )) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}