School project help (Arduino UNO)

Hey guys/gals,

First time posting in the fourms (just made my account).

I am having troubles with a project a teacher gave me. i am unable to get the code for the blue LED to kick in, it switches from green to red fine though. any help would be appreciated. also need a timer and alarm sound. if someone would be able to point me to an example or 2 of both that would be helpful.

Thank you

Below i will include all my code and a description of what the teacher wants.

Description:

Create a program that will monitor temperature, drive a cooling fan, and indicate low/med/high temperature and sound an alarm. You will be changing the temperature of the sensor by holding it in your fingers so make sure the sensor has enough space around it.

You will use the RGB tricolour LED in your kit (see lab 12 for basic operation). When the sensor detects room temperature (approx. 20 degrees C) or below, light the Blue element at about half intensity. As the temperature climbs the intensity increases. When the temperature reaches 26 degrees C the blue LED will be at its brightest. Above that, the blue LED turns off and the Green LED will turn on at about half intensity. Its intensity will increase until the sensor reads about 30 degrees C. Above that, it will be off and the Red LED will come on at full intensity. If the temperature exceeds 32 degrees C, the Red LED will blink. If the Red LED blinks for more than 10 seconds, sound an alarm with the piezo element. The alarm will stop when the temperature drops below the threshold that triggered it.

The DC motor will simulate a cooling fan with two speeds. It will not turn while the Blue LED is on, at low speed while the Green LED is on, and at maximum speed when the Red LED is on.

// constants won't change
const int bluePin = 10;
const int greenPin = 11;
const int redPin = 9;
const int speakerPin = 5;
const int motorPin = 6;
const int temperaturePin = 0;

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0; 
long interval = 100;

void setup ()
{
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(motorPin, OUTPUT); 
  pinMode(temperaturePin, INPUT);

  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
{
 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
                                                  //to degrees ((volatge - 500mV) times 100)
 Serial.println(temperature);                     //printing the result
 delay(500);                                      //waiting a second
 
//blue LED *NOT WORKING*
    if (temperature <= 20){                              
      analogWrite(bluePin, 127.5);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
     }
    else if (temperature > 20 && temperature <= 23){
      analogWrite(bluePin, 191);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
    }
    else {
      analogWrite(bluePin, 255);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
    }
//green LED    
    if (temperature <= 27){                              
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 127.5);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
     }
    else if (temperature > 27 && temperature <= 28.5){
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 191);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
    }
    else {
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 255);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
    }
//red LED    
    if (temperature > 30 && temperature <= 32){          
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 255);
      analogWrite(motorPin, 255);
     }
    else if (temperature > 32){
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 0);
      
        //led flash     
        unsigned long currentMillis = millis();
        if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        ledState = HIGH;
        else
        ledState = LOW;

        // set the LED with the ledState of the variable:
        digitalWrite(redPin, ledState);
        }
        
  analogWrite(motorPin, 255);
 }
}
/*
 * 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
}

ok couple of points

  1. wrap your code inside
    [kode]
    your code
    [/kode]
    tags (but spell it with a c!

  2. a picture/wiring diagram is almost essential to solve this

over to you!

hee hee and I found the bug already!

in your second test, if the temp is less than 27 you turn the blue LED off
remove that line and see what happens

(still post your code properly though)

ya sorry. its my 1st time on the forum will do that from now on as it is much neater. and i will try that. i am working on a wire schematic right now

go back and edit your original post to add the code tags

(not moaning just helping for next time!)

Here is a schematic. Sorry if its a little messy. and i tried removing

 if (temperature <= 27){                              
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 127.5);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
     }

and it still does the same thing. the blue and green LEDs have 3 steps of intensity each. Originally my teacher wanted it to increase respectively with temperature increase but i have no idea how to get it to do that. so i simplified it with 1/2 intensity 3/4, then full.

did you read my post #2
wher eI told you one of the bugs?!?

the code i just quoted was the one i removed. was that not the one you wanted me to remove? less than or equal to 27?

er no
the test is fine
but work through it with a temperature that is <20, say 19

your first test "if temperature <= 20"
turns on the blue LED

your second test "if temperature <=27"
turns the blue LED off again!

so don't turn it off!

so I've tried keeping the blue LED on by setting it at 127 when <=27 and i've tried changing the code to

//green LED    
    if (temperature > 26 && temperature <= 27.5){                              
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 127.5);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
     }
     else if (temperature > 27.5 && temperature <= 28.5){
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 191);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
    }
    else {
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 255);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 127);
    }

with the above code i noticed when its below 26 the green LED is at full intensity. Blue will still not turn on. and the colours can not mix at all. If you're willing to could you write what you want me to change the code to? I have a feeling i still have not put it the way you want it

suggest you just make them all
if ()
{
}
else if ()
{
}
... all the way through
you start off that way
then seem to think that because it's in the green range that you do it differently

everything was copy pasted from //blue LED only the numbers were changed to specify temperature range and LED status. the structure stayed the same

WOOO

i finally figured it out

//blue LED 
    if (temperature <= 20){                              
      analogWrite(bluePin, 127.5);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
     }
    else if (temperature > 20 && temperature <= 23){
      analogWrite(bluePin, 191);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
    }
    else {
      analogWrite(bluePin, 255);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 0);
      analogWrite(motorPin, 0);
    }
//green LED    
    if (temperature > 26 && temperature <= 30){  
      if (temperature > 26 && temperature <= 27.3){      
          analogWrite(bluePin, 0);
          analogWrite(greenPin, 127.5);
          analogWrite(redPin, 0);
          analogWrite(motorPin, 127);
           }
       else if (temperature > 27.5 && temperature <= 28.5){
          analogWrite(bluePin, 0);
          analogWrite(greenPin, 191);
          analogWrite(redPin, 0);
          analogWrite(motorPin, 127);
          }  
        else {
          analogWrite(bluePin, 0);
          analogWrite(greenPin, 255);
          analogWrite(redPin, 0);
          analogWrite(motorPin, 127);
        }
  }
//red LED    
    if (temperature > 30 && temperature <= 32){          
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 0);
      analogWrite(redPin, 255);
      analogWrite(motorPin, 255);
     }
    else if (temperature > 32){
      analogWrite(bluePin, 0);
      analogWrite(greenPin, 0);
      
        //led flash     
        unsigned long currentMillis = millis();
        if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        ledState = HIGH;
        else
        ledState = LOW;

        // set the LED with the ledState of the variable:
        digitalWrite(redPin, ledState);
        }
        
  analogWrite(motorPin, 255);
 }
}

now just to get the alarm to turn on after the red LED blinks for 10 seconds