hello guys/girls,
I'm having some difficulty understanding the Arduino Uno timer. i have a program right now where it takes a temperature reading and relates it to an LED. If the temperature exceeds 32 a red LED turns on and starts blinking. the part i am stuck on is after 10 seconds of the LED blinking an alarm is supposed to sound. I can get the alarm to sound i just do not know how to go about delaying it 10 seconds. whenever i have tried it it delays my LED flashing too.(under //red LED in code)
any help is much appreciated
Thanks
here is my program:
// 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
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);
if (temperature > 32){
beep (200);
}
}
}
void beep(unsigned char delayms){
analogWrite(speakerPin, 20); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(speakerPin, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}
/*
* 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
}