/*
Name:Prerana Kolipaka
Design Challenge:
Design a device that has a red, green, and blue LED plus a push button.
The device should illuminate red if the temperature is above 40C, blue if the temperature is below 0C,
or green if the temperature is between 0 and 40C.
The LED should be on if the push button is pressed and off if not,
the LED lit should correspond to the appropriate temperature range.
The solution is below.
Components used: Arduino UNO, 3 LEDs, 3 Resistors (1000 ohms), push button and Analog temperature sensor: NTC
*/
const float BETA= 3950; // The beta coefficient of the thermistor
#define LED_RED 2 //Red LED is connected to pin 2 of Arduino UNO
#define LED_BLUE 3 // Blue LED is connected to pin 3 of Arduino UNO
#define LED_GREEN 4 // Green LED is connected to pin 4 of Arduino UNO
#define BUTTON 10
/*The 1.r terminal of the pushbutton is connected to the pin 10 of Arduino UNO
and 2.l is connected to ground.*/
/*Temperature sensor connections:
The Ground pin of the sensor is connected to the ground pin of the Arduino UNO.
The VCC pin of the sensor is connected to 5V on Arduino UNO.
The OUT pin is connected to A0 pin of the Arduino UNO */
void setup() {
// put your setup code here, to run once:
pinMode(LED_RED, OUTPUT);//setting the LED pin in OUTPUT mode
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);//setting the button pin in INPUT_PULLUP mode
Serial.begin(9600);
}
int lastValue=HIGH; //this variable is used to check change of state of the push button
void loop() {
// put your main code here, to run repeatedly:
int tempValue = analogRead(A0); //reading the value from temperature sensor and storing it in tempValue
float celsius = 1 / (log(1 / (1023. / tempValue - 1)) / BETA + 1.0 / 298.15) - 273.15; //calculating the temperature in celsius
int value=digitalRead(BUTTON); //reading the state of push button
if(lastValue!=value) //recognising change in state of push button
{
lastValue=value;
if(value==LOW) //checking if the button is pressed
{
if(celsius>40.0)
{
digitalWrite(LED_RED, HIGH);//if Temperature >40C, red LED is turned on.
}
else if(celsius>0.0)
{
digitalWrite(LED_GREEN, HIGH);//if temperature between 0C and 40C, green LED is turned on.
}
else
{
digitalWrite(LED_BLUE, HIGH); // if temperature <0C, blue LED is turned on.
}
}
else{
//turning of the LEDs in case the push button is released.
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
}
}