Hello,
I am trying to make a Love-o-Meter with an RGB LED. I am able to get the RBG to turn Blue and then Green, but the other two colors, yellow and Red will not change. I am not sure if I am coding it correctly and need some help and is appreciated.
const int sensorPin = A0;
// room temperature in Celcius
const float baselineTemp = 24.0;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
void setup() {
// open a serial connection to display values
Serial.begin(9600);
// set the LED pins as outputs
// the for() loop saves some extra coding
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
// to set up RGB lights
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
}
void loop() {
int sensorVal = analogRead(sensorPin);
// send the 10-bit sensor value out the serial port
Serial.print("sensor Value: ");
Serial.print(sensorVal);
// convert the ADC reading to voltage
float voltage = (sensorVal / 1024.0) * 5.0;
// Send the voltage level out the Serial port
Serial.print(", Volts: ");
Serial.print(voltage);
// convert the voltage to temperature in degrees C
// the sensor changes 10 mV per degree
// the datasheet says there's a 500 mV offset
// ((volatge - 500mV) times 100)
Serial.print(", degrees C: ");
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
// -*
if (temperature < baselineTemp) {
setColor(0, 0, 255); // blue
delay(1000);
} // if the temperature equal to baseline +2, turn an LED on
else if (temperature = baselineTemp + 1 ) {
setColor(0, 255, 0); // green
delay(1000);
} // if the temperature is equal to baseling + 3, turn a second LED on
else if (temperature = baselineTemp +2 ) {
setColor(255, 255, 0); // yellow
delay(1000);
} // if the temperature rises more than 6 degrees, turn all LEDs on
else if (temperature = baselineTemp +3) {
setColor(255, 0, 0); // red
delay(1000);
}
}
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);
}