Hi, i'm completely new to arduino and today i tried to build a lamp that changes color on how hot the temperature sensor is. My code: http://pastecloud.net/deuh0yV7xI
This is just an test document, but the rest works fine. This are the results i get:
const int sensorPin = A0; // pin for the temp sensor
const int baselineTemp = 21.0;
const int greenLEDPin = 9;
const int redLEDPin = 11;
const int blueLEDPin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(sensorPin); // reads the value of the temp sensor
float voltage = (sensorValue/1024.0) * 5.0; // changes the value of the temp sensor to a voltage between 0 and 5
Serial.print(voltage); Serial.println(" Volts");
float temperature = (voltage - .5) * 100; // changes the voltage to the real temperature
Serial.println(temperature); Serial.println(" Degrees C");
float temperatureDifference = temperature - baselineTemp; // checks how much degrees celcius the temperature is above the baseLineTemp so everything above the baselinetemp is making the RGB led more red
if(temperatureDifference <= 0){
temperatureDifference = 0;
}else if(temperatureDifference > 10){
temperatureDifference = 10;
}
float scale = temperatureDifference * 2;
analogWrite(redLEDPin, scale * 25); // changes the scale from the temperature to a value that the RGB led can use
analogWrite(blueLEDPin, 250 - (scale * 25));
analogWrite(greenLEDPin, 0);
}
Display to your serial monitor the raw reading from analog read (before any calculations).
Does it change as the the temp. sensor is heated and cooled?
What sensor are you using? Is it a thermistor? What is its resistance? How do you have it connected to the arduino?
You may want to use a good thermometer and celebrate that input using the map() function.