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!