Pages: [1]   Go Down
Author Topic: Can't debug Code, to show change in temperature as colour with a RGB  (Read 7334 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I am trying to show the change in temperature(degrees centigrade), with different colours with a RGB Led.  It worked with the first few colours, but started to mess up when I added more detailed colour instructions.  I am trying to figure out the bugs, but it would be great if I would get some help.  Thank you for the help.

Code:
int temperaturePin = 0;
 int LED[] = {9, 10, 11}; //sets the pins
                         //9 = RED, 10=GREEN, 11= BLUE
         
const boolean ON = LOW;  //defines ON as LOW
const boolean OFF = HIGH;  //defines OFF as 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(LED[i], OUTPUT);   //Set the three LED pins as outputs
  }
  Serial.begin(115200);  //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()                     
{
 
/*
 * getVoltage() - returns the voltage on the analog input defined by
 * pin
 */
{float getVoltage(int pin);
{
 return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
} //converting from a 0 to 1023 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
 float temperature = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
 temperature = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset
                                                 
Serial.print(temperature); Serial.println(" degrees centigrade");           
 delay(1000); 
if(temperature < 430){
  setColor(LED, BLUE);
}
else{
  if(temperature >430 && temperature <440){
    setColor(LED, GREEN);}
      else {
        if(temperature >440 && temperature <450){
    setColor(LED, GREEN);}
      else{
        setColor(LED, BLACK);
      }



 
}



}

void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led[i], color[i]);
 }
}

void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8842
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should help some. 
Code:
int temperaturePin = 0;
int LED[] = {
  9, 10, 11}; //sets the pins
//9 = RED, 10=GREEN, 11= BLUE

const boolean ON = LOW;  //defines ON as LOW
const boolean OFF = HIGH;  //defines OFF as 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(LED[i], OUTPUT);   //Set the three LED pins as outputs
  }
  Serial.begin(115200);  //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)
}

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

 

  float temperature = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
  temperature = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset

  Serial.print(temperature);
  Serial.println(" degrees centigrade");           
  delay(1000);
 
  if(temperature < 430){
    setColor(LED, BLUE);
  }
  else{
    if(temperature >430 && temperature <440){
      setColor(LED, GREEN);
    }
    else {
      if(temperature >440 && temperature <450)
      {
        setColor(LED, GREEN);
      }
      else{
        setColor(LED, BLACK);
      }

    }
  }
}


void setColor(int* led, boolean* color){
  for (int i = 0; i < 3; i++) {
    digitalWrite(led[i], color[i]);
  }
}

void setColor(int* led, const boolean* color){
  boolean tempColor[] = { color[0], color[1], color[2]  };
  setColor(led, tempColor);
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26274
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code doesn't compile, so debugging is some way off.

One useful technique is to first make your code more legible, but you'll soon find that the auto format tool doesn't work on the code you posted, giving you the useful clue "Too many left curly braces".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: