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
}