Error with Temperature Sensor

Hello everyone,

I have been working on a code that utilizes a temperature sensor (lm35) and outputs a color based on the temperature via an RGB LED. I cannot see to get the color to change based on temperature. The code will work but the LED remains a constant color. Any ideas? heres the code:

float RGB[3];

float tempC;

int ldrPin = 0; // LDR in Analog Input 0 to read the ambient light

int analog_tempPin1 = 1;

int ambientLight; // variable to store the value of the ambient light

int ledPin1 = 11; // red LED in Digital Pin 11 (PWM)

int ledPin2 = 10; // green LED in Digital Pin 10 (PWM)

int ledPin3 = 9; // blue LED in Digital Pin 9 (PWM)

int ledPin4 = 7; // LED connected to digital pin 7

int ledPin5 = 6;

void setup(){

pinMode(ledPin1,OUTPUT); // tell arduino it's an output

pinMode(ledPin2,OUTPUT);// tell arduino it's an output

pinMode(ledPin3,OUTPUT); // tell arduino it's an output

// set all the outputs to low

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

pinMode(ledPin4, OUTPUT);

pinMode(ledPin5, OUTPUT);

Serial.begin(9600);

}

void loop(){

for (float x=0;x<PI;x=x+0.00001){

RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led

RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led

RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led

ambientLight=analogRead(ldrPin); // read an store the ambient light

if(ambientLight>600){ // start only if the ambient light is very low

// write the brightness on the leds

analogWrite(ledPin1,RGB[0]);

analogWrite(ledPin2,RGB[1]);

analogWrite(ledPin3,RGB[2]);

}

else{

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

}

for(int i=0;i<3;i++){

if(RGB[i]<1){

delay(100);

}

if(RGB[i]<5){

delay(50);

}

if(RGB[i]<10){

delay(10);

}

if(RGB[i]<100){

delay(5);

}

}

delay(1);

tempC = analogRead(analog_tempPin1); //read the value from the sensor

tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature

Serial.print("analog ");

Serial.print(analog_tempPin1);

Serial.print(" ");

Serial.println(tempC); //send the data to the computer

if (tempC < 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(1000); // wait for a second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(1000); // wait for a second

}

if (tempC > 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(100); // wait for 0.1second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(100); // wait for 0.1second

}

}

}

Thanks!

What is all that other LED diddling code for? Dump that crap. Write a sketch that does nothing but read the temperature sensor and print the value read. Dos the output make sense? If not, do not bother trying to use the value to set the color.

The only thing you do with the temperature is turn pins on or off. How is that supposed to change the color?

Alright, heres my new code:

/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;

void setup() {

 pinMode (13, OUTPUT); //Blue LED symbolising Cold
 pinMode (8, OUTPUT); //Red LED symbolising Hot
 pinMode (9, OUTPUT); //Green LED symbolising Lukewarmn
 pinMode (tempPin, INPUT);
}

 void loop() {
    temp = analogRead(tempPin);
    temp = temp * 0.48828125;

 delay(100);
 if (temp < 35) //when temp is below 35 degrees
 {
 digitalWrite(13, HIGH); //blue led is on
 digitalWrite(8, LOW); //red led is off
 digitalWrite(9, LOW); //green led is off
 }
else if (temp > 40) //when temp is above 40 degrees
{
 digitalWrite(13, HIGH); //blue led is off
 digitalWrite(8, LOW); //red led is on
 digitalWrite(9, LOW); //green led is off
}
  else //when between 35-40 degrees
{
 digitalWrite(13, LOW); //blue led is off
 digitalWrite(8, HIGH); //red led is off
 digitalWrite(9, LOW); //green led is on
    } 
   }

Does this appear better?

You mixing floating point numbers with integers messy thing happen

int tempPin = A0;
int temp;

//... other code ...

temp = analogRead(tempPin); // returns int 0 to 1024
float Tfloat = (float) temp * 0.48828125; // wants to return a float
temp = (int) Tfloat; // make it back into an int

I note that you have two cases where you configure the LED's the same. But other than that, the code looks good.

Alright I found a code from a forum on this site that I have modified to work at the desired temperatures. Everything works well. I have 3 temperatures corresponding to 3 different LED colors (Red, Green, Blue). I want to create a forth temperature range but I am unsure of how to get two LED colors to be illuminated simultaneously (i.e. Red and Blue to make Purple).

Here’ my new code:

/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;
byte redPin=8, greenPin=9, bluePin=13; // swap numbers for words

void setup() {

  pinMode (bluePin, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
  pinMode (redPin, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
  pinMode (greenPin, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
  
}

  void loop() {
     temp = analogRead(tempPin);
     temp = temp * 0.48828125;

  delay(1000);
  if (temp < 20) lightLED(bluePin);
  if (temp >= 19 && temp <= 20) lightLED(greenPin);
  if (temp > 20) lightLED(redPin);
    }

void lightLED(byte whatLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off
// now turn on the requested LED
  digitalWrite(whatLED, HIGH); 
}

And I would like to make changes such as :

...

 void loop() {
     temp = analogRead(tempPin);
     temp = temp * 0.48828125;

  delay(1000);
  if (temp < 20) lightLED(bluePin);
  if (temp >= 19 && temp < 20) lightLED(greenPin);
  if (temp >=20 && temp <=25) lightLED(redPin) lightLED(bluePin);   //New line of code to make purple??
  if (temp > 20) lightLED(redPin);
    }

void lightLED(byte whatLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off
// now turn on the requested LED
  digitalWrite(whatLED, HIGH); 
}

I would like something similar to my above proposed code (i know it doesn’t work)

Any advise?

Thanks!

brandon15601 wrote:

i know it doesn't work

Actually, it DOES work. It just doesn't do what you want. You need to tell us:

  • What do you want, what do you expect?
  • What does the code do instead?

Hi,
Thanks for the reply. I uploaded the code with the changes to illuminate the red and blue pin simultaneously and the code will NOT work. An error appears because the program cannot execute the within one line of code. I was wondering if anyone could help me design a line of code that will illuminate 2 pins (red and blue) simultaneously when the desired temperature is detected. Heres my code, I placed a comment beside the temperatures that i want the red and blue pin to illuminate simultaneously creating a purple color in my RGB LED.

/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;
byte redPin=8, greenPin=9, bluePin=13; // swap numbers for words

void setup() {

  pinMode (bluePin, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
  pinMode (redPin, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
  pinMode (greenPin, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
  
}

  void loop() {
     temp = analogRead(tempPin);
     temp = temp * 0.48828125;

  delay(1000);
  if (temp < 26) lightLED(bluePin);
  if (temp >= 26 && temp <= 27) lightLED(greenPin);

if (temp <= 28) lightLED(greenPin);    // I know this code lights the green but i want to light the red and                                                                       //                                                         blue pin simultaneously to create a purple color


  if (temp >27) lightLED(redPin);
    }

void lightLED(byte whatLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off
// now turn on the requested LED
  digitalWrite(whatLED, HIGH); 
}

Heres my new proposed code:

/*
temp lights LM35 and leds
*/

int tempPin = A0;
int temp;
byte redPin=8, greenPin=9, bluePin=13, purplePin=13&8; // swap numbers for words

void setup() {

  pinMode (bluePin, OUTPUT); //Blue LED symbolising Cold and Not Fit for Consumption
  pinMode (redPin, OUTPUT); //Red LED symbolising Hot and Not Fit for Consumption
  pinMode (greenPin, OUTPUT); //Green LED symbolising Lukewarm and Read for Consumption
  pinMode (purplePin, OUTPUT);
  
}

  void loop() {
     temp = analogRead(tempPin);
     temp = temp * 0.48828125;

  delay(1000);
  if (temp < 26) lightLED(bluePin);
  if (temp >= 26 && temp <= 27) lightLED(greenPin);
  if (temp >= 27 && temp <= 28) lightLED(purplePin);
  if (temp > 28) lightLED(redPin);
    }

void lightLED(byte whatLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off
  digitalWrite(purplePin, LOW);
  
  
// now turn on the requested LED
  digitalWrite(whatLED, HIGH); 
}

As you can see, I inserted a method for the purplePin which should illuminate the red and blue pin at the same time. The code does not have any errors. When i cycle through the temperatures, the red, blue and green pins light up properly but the purple color will not appear in-between as it should.

13=1+4+8, so 13&8 is just 8.

...BUT...

that is not the way to do it anyway.

The function lightLED() turns off all LEDs and then turns on a single LED. You need a different function (or a modified lightLED function) that can turn on more than one LED. Also, forget about (as in, DELETE) all of that purplePin stuff.

digitalWrite(...) will never turn on or off more than a single pin at a time, no matter what you try to do. You will need a function that has more than one digitalWrite(...) for turning on LEDs.

You can write it yourself, or you can go to "Gigs and Collaborations" with some money and get someone to write it for you.

Or, you can use this:

void lightLED(byte whatLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off

// now turn on the requested LED
  digitalWrite(whatLED, HIGH);
}
void lightLED(byte whatLED, byte aSecondLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off

// now turn on the requested LEDs
  digitalWrite(whatLED, HIGH);
  digitalWrite(aSecondLED, HIGH);
}
void lightLED(byte whatLED, byte aSecondLED, byte aThirdLED){
  // first turn off all the LEDs
  digitalWrite(bluePin, LOW); //blue led is off
  digitalWrite(redPin, LOW); //red led is off
  digitalWrite(greenPin, LOW); //green led is off

// now turn on the requested LEDs
  digitalWrite(whatLED, HIGH);
  digitalWrite(aSecondLED, HIGH);
  digitalWrite(aThirdLED, HIGH);
}

There are fancier ways to do this, but this is enough for a novice.
A cool thing about C and C++ is that they allow the name of a function to be re-used as long as the argument list is sufficiently different.

When you want purple, use lightLED(bluePin, redPin);
When you want all LEDs on, use lightLED(bluePin, redPin, greenPin);
You could save a little space perhaps by just defining the third form of the function, and using (for example, for blue) lightLED(bluePin, bluePin, bluePin);

I've watched you struggle with this for days. OK, this isn't that hard.

First thing first, stop the coding. No code for this first part. Set down on paper a range of temperatures and what color the led should be. I assume we want red and blue. So for some temperature and below the blue will be fully on and the red will be fully off. Then as you go up in temperature decide what should happen. Should the red fade in until fully on and then the blue fade out as you go up in temp? Or are we just turning them on and off so that there are really only three colors, blue purple and red? Or what.

NO CODE!!! Let me repeat that NO CODE!!! Just write down how the led should react to temperature. Do that first and I'll show you how simple this is. Get us a chart going, or a graph if you will that will relate temperature to color. Once we have that we'll work on math, still no code. And once you have the math, then finally you can start thinking about the code.