This code has no errors but the color will not change with temperature. I connected the sensor but I'm not exactly sure which port it should be inserted into (the lead the sends info to the computer) can you happen to tell via this 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
}
}
}
And this code has a ton of errors which are listed below the code, but the concept is exactly what I want to happen with the LED colors:
/* "The Magic Crystal Mood Ball"
Stupid Pet Trick - ITP 2011
*/
//TMP36 Pin Variables
int temperaturePin = 0; //input: the analog pin the TMP36 is connected
//RGB LED pins
int ledDigitalOne[] = {9, 10, 11}; //output: the three digital pins of the RGB LED
//9 = redPin, 10 = greenPin, 11 = bluePin
const boolean ON = LOW;
const boolean OFF = HIGH;
//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};
void setup()
{
for(int i = 0; i < 3; i++)
{
pinMode(ledDigitalOne[i], OUTPUT); //Set the RGB LED pins as outputs
}
Serial.begin(9600); //Start the serial connection with the computer
}
void loop()
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
temperature = (((temperature - .5) * 100)*1.8)+32; //converting from 10 mv per degree wit 500 mV offset
int newTemperature = temperature; //to degrees ((volatge - 500mV) times 100)
Serial.println(newTemperature); //printing the result
delay(7000); //waiting 7 seconds to get a new result
// Set the three LEDs to any predefined color depending of the temperature in ºF
if ((newTemperature>40) && (newTemperaturesetColor(ledDigitalOne, BLACK)
Serial.println("Black");
}
else if ((newTemperature>=72) && (newTemperaturesetColor(ledDigitalOne, WHITE)
{
Serial.println("White");
}
else if ((newTemperature>=74) && (newTemperaturesetColor(ledDigitalOne, GREEN)
{
Serial.println("Green");
}
else if ((newTemperature>=76) && (newTemperaturesetColor(ledDigitalOne, CYAN)
{
Serial.println("Cyan");
}
else if ((newTemperature>=78) && (newTemperaturesetColor(ledDigitalOne, BLUE)
{
Serial.println("Blue");
}
else if ((newTemperature>=80) && (newTemperaturesetColor(ledDigitalOne, YELLOW)
{
Serial.println("Yellow");
}
else if ((newTemperature>=82) && (newTemperaturesetColor(ledDigitalOne, RED)
{
Serial.println("Red");
}
else
{
setColor(ledDigitalOne, MAGENTA);
Serial.println("Magenta");
}
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
}
// Function to set the color
}
void setColor(int* led, boolean* color)
{
for(int i = 0; i < 3; i++)
{
digitalWrite(led[i], color[i]);
}
}
// A version of setColor that allows for using const boolean colors
void setColor(int* led, const boolean* color)
{
boolean tempColor[] = {color[0], color[1], color[2]};
setColor(led, tempColor);
}
ERRORS:
sketch_apr28a.ino: In function 'void loop()':
sketch_apr28a.ino:43:72: error: 'newTemperaturesetColor' was not declared in this scope
sketch_apr28a.ino:44:1: error: expected ')' before 'Serial'
sketch_apr28a.ino:44:24: error: expected ')' before ';' token
sketch_apr28a.ino: At global scope:
sketch_apr28a.ino:46:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:50:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:54:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:58:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:62:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:66:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:70:1: error: expected unqualified-id before 'else'
sketch_apr28a.ino:82:1: error: expected declaration before '}' token
Error compiling.