I am trying to create a program that will monitor temperature, drive a cooling fan, and indicate low/med/high temperature and sound an alarm. 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 degreesC. 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.
with my code right know I can't even get a led to turn on I think it is how I have my if statement set up but not sure I have worked many hours on this trying different things but since I am relatively never at this I'm at a loss could really use some help or a point in the right direction
int tempPin = A1; // the output pin of LM35
int fan = 9; // the pin where fan is
int led1 = 3; // led pin green
int led2 = 4; // led pin blue
int led3 = 5; // led pin red
int temperature;
const int templow = 24;
const int tempmed = 26; // the temperature to start the fan
const int temphigh = 28; // the maximum temperature when fan is at 100%
int fanSpeed;
int speakerPin = 13;
void setup() {
pinMode(fan, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(tempPin, INPUT);
pinMode(speakerPin, OUTPUT);
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() {
float temperature = getVoltage(tempPin); //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(1000); //waiting a second
}
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
if(temperature < 28) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
digitalWrite(led1, HIGH); // else turn on led
digitalWrite(led2, LOW); // turn off led
digitalWrite(led3, LOW); // turn off led
delay(100);
}
else if((temperature >= templow) && (temperature <= tempmed)) { // if temperature is more than low temp but less than med temp
fanSpeed = map(temperature, tempmed, temperature, 22, 100); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
digitalWrite(fan, LOW);
digitalWrite(led2, HIGH); // else turn on led
digitalWrite(led1, LOW); // turn off led
digitalWrite(led3, LOW); // turn off led
delay(100);
}
else if((temperature >= tempmed) && (temperature <= temphigh)) { // if temperature is higher than minimum temp
fanSpeed = map(temperature, temphigh, temperature, 24, 255); // the actual speed of fan
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
digitalWrite(fan, fanSpeed);
digitalWrite(led3, HIGH); // else turn on led
digitalWrite(led1, LOW); // turn off led
digitalWrite(led2, LOW); // turn off led
delay(100);
}
else(temperature < temphigh); { // if temp is lower than minimum temp
// fanSpeed = 0; // fan is not spinning
//digitalWrite(fan, LOW);
digitalWrite(led3, HIGH); // else turn on led
digitalWrite(led1, LOW); // turn off led
digitalWrite(led2, LOW); // turn off led
tone(speakerPin,600,200);// sounds buzzer
delay(100);
}
}