Temp sensor-controlled LEDs also dimmed by photoresistor, but the red won't....

Hi guys,

I’m very, very new to this as you can imagine. I took delivery of my Arduino kit today and have spent the entire afternoon and evening playing with it.

Now electronics I’m not too bad at, but programming is nothing I’ve done before. Still, it’s a fairly simple place to start, right…?

Anyway, I digress. My first “project” has been the following:

Four LEDs, red, yellow, green and blue (green and blue are actually the same RGB LED but hey ho)
Temperature sensor
Each LED lights up based on the room temperature and the ‘comfort’ of it, i.e. blue is less than 18c (cold), green is 19-22 (comfortable), yellow is more, i.e. warm, red is more still, i.e. hot. You get the idea.
I’ve also got a photoresistor that sorts the brightness of the LEDs, namely they dim when the room goes dark and brighten when you turn the lights on.

Now 95% of this works. The LEDs all change based on temp, and they dim based on the light levels. Well, nearly all of them dim. The red one is stubborn. It’s either on, or off. Darken the room and it won’t light up at all.

I wish I could for the life of me work out why.

Here’s my code:

int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade 
                        //(500 mV offset) to make negative temperatures an option
int lightPin = 1;       //the analog pin the Photoresistor is connected to
int lightLevel;
/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
int redLED=8;          //mapping the coloured LEDs to the digital pins
int yellowLED=9;
int greenLED=10;
int blueLED=11;
 
void setup()
{
  pinMode(redLED, OUTPUT);      //setting the LED pins to output
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  
  Serial.begin(9600);  //Start the serial connection with the copmuter
                       //to view the result open the serial monitor 
                       //last button beneath the file bar (looks like a box with an antenae)
}
 
void loop()                     // run over and over again
{
 lightLevel = analogRead(lightPin);
 lightLevel = map (lightLevel, 0, 800, 0, 255);
 lightLevel = constrain (lightLevel, 6, 255);
  float temperature = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
 temperature = (temperature - 0.5) * 100;          //converting from 10 mv per degree wit 500 mV offset
                                                  //to degrees ((volatge - 500mV) times 100)
 Serial.println(temperature);
 Serial.println(lightLevel);                     //printing the result
 
 if (temperature>= 25)
 {
   digitalWrite(yellowLED, LOW);
   digitalWrite(greenLED, LOW);
   digitalWrite(blueLED, LOW);
   analogWrite (redLED, lightLevel);
 }
   else if (temperature>= 23)
   {
   digitalWrite(redLED, LOW);
   digitalWrite(greenLED, LOW);
   digitalWrite(blueLED, LOW);
   analogWrite (yellowLED, lightLevel);
   }
   else if (temperature>= 19)
   {
   digitalWrite(redLED, LOW);
   digitalWrite(yellowLED, LOW);
   digitalWrite(blueLED, LOW);
   analogWrite (greenLED, lightLevel);
   }
else if (temperature< 18)
{
   digitalWrite(redLED, LOW);
   digitalWrite(yellowLED, LOW);
   digitalWrite(greenLED, LOW);
   analogWrite (blueLED, lightLevel);
}
 delay(1000);                                     //waiting a second
}

/*
 * getVoltage() - returns the voltage on the analog input defined by
 * pin
 */
float getVoltage(int pin){
 return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

Nicely pieced together from other sketches and bodged by me. Don’t hurt me, I’ve only been at it for about 8 hours :stuck_out_tongue:

So if someone could offer some insight as to why my red LED won’t dim, that would be splendid. The same happens when I swap it with a different one by the way, so it’s not the LED itself, merely my messy programming attempts. :smiley:

Thanks! :slight_smile:

Aaaaaand I've just figured it out.

The red LED wasn't connected to a PWM output.

Silly me.

As you were.

(Any hints on how to optimise my code however are gratefully received!)