I use 3 different codes, and the same circuit, but is not work.
Why?
Code 1
int sensorPin = 0;
int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float cold = 0;
float hot = 0;
void setup()
{
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
}
void loop()
{
digitalWrite(red, LOW); //switch off the LEDs
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
// read the temperature and convert the result to degrees Celsius
sensor = analogRead(sensorPin); // TMP36 sensor output pin is connected to Arduino analogue pin 0
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-400; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
// now lets turn the LEDs if it is hot or cold
if (celsius>=hot)
{
digitalWrite(red, HIGH); // the LED red will light and is time to move
}
else if (celsius<=cold)
{
digitalWrite(yellow, HIGH); // the LED red will light and is time to move
}
else
{
digitalWrite(green, HIGH); // everything normal
}
}
Code 2
int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
int sensorPin = 0;
int reading = 0;
void setup(){
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
Serial.begin(9600); // activate the serual output connection
}
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float toocold = 10;
float toohot = 20;
void loop()
{
int reading = analogRead(sensorPin);
float voltage = reading * 5.0; // convert raw sensor value to millivolts
voltage /= 1024.0;
float celsius = (voltage - 0.5) * 100; // convert millivolts to Celsius
if (celsius <= toocold) {
digitalWrite (red, LOW);
digitalWrite (green, LOW);
digitalWrite (yellow, HIGH);
}
else if (celsius >= toohot){ // if statment to turn the LED Red if Over Max Temperature
digitalWrite (red, HIGH);
digitalWrite (green, LOW);
digitalWrite (yellow, LOW);
}
else { // in all other cases temperature is ok
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, HIGH);
}
}
Code 3
int red = 2; // the pins for the LED
int green = 3;
int yellow = 5;
void setup(){
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
Serial.begin(9600); // activate the serual output connection
}
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float toocold = 15;
float toohot = 20;
void loop()
{
sensor = analogRead(0);
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-500; // remove voltage offset
celsius = voltage/10; // convert millivolts to Celsius
if (celsius <= toocold) {
digitalWrite (red, LOW);
digitalWrite (yellow, HIGH);
digitalWrite (green, LOW);
}
else if (celsius >= toohot){ // if statment to turn the LED Red if Over Max Temperature
digitalWrite (red, HIGH);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
}
else { // in all other cases temperature is ok
digitalWrite (red, LOW);
digitalWrite (yellow, LOW);
digitalWrite (green, LOW);
}
}
I look forward to hearing from Forum!
Att.

