I am making a temperature sensor that has an LED stimuli, which turns different colors based on the temperature. I set the temp as a variable and the temps it is compared to as variables also(int cold = 20)
However, once I turn on my arduino it responds to whatever the temp condition is at that moment and the LED stays the same color even if that condition is no longer fulfilled and another condition is. I am using an RGB LED with a built in resistor and a ds18b20 with an Arduino Uno. There are no wiring problems as all colors are able to be used and the temperature is correctly being read. Thanks for your help.
Here is my code:
/********************************************************************/
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
//Variables for LED Pins
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int tempF = sensors.getTempCByIndex(0);
int hot= 40;
int cold= 30;
void setup(void)
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop(){
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
/********************************************************************/
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
/********************************************************************/
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
if(tempF <= cold){
setColor(0, 0, 255); //blue
}
else if (tempF >= hot) { //hot
setColor(255, 0, 0); //red
}
else { //fine
setColor(0, 255, 0); //green
}
delay(250);
}