Go Down

Topic: Blink LED without using delay in the main loop (Read 5872 times) previous topic - next topic

yee0722

May 10, 2014, 08:52 am Last Edit: May 10, 2014, 10:56 am by yee0722 Reason: 1
Hi, I want to add blinking to my led, without using delay that will interrupt the overall system. The main loop has delay 3s, which will read temperature from sensor every 3s. I had try using example of "led blinking without delay", it was able to blink, but the blinking interval is not correct, it follows main loop delay, which not what I want.

I want the led to blink every 100ms in the 3s delay main loop.  Any ideas?


Code: [Select]
//declare variables
int buzzer = 5;
int yellow1 = 3;  //warning temp
int red1 = 4; // overtemp
int red = 2;  // red led indicate flame
float tempC;
float voltage;
int tempPin =0; // Temp sensor plugged analog pin 0
int flamePin= 1;

// write setup function

void setup()
{
 Serial.begin(9600);// open serial port to communicate with the temp sensor
 pinMode(red1, OUTPUT);
 pinMode(yellow1, OUTPUT);  
 pinMode (red, OUTPUT);
 pinMode (buzzer, OUTPUT);

}

//write loop that will control what we want the arduino to do with the sensor readout

void loop()
{
 tempC = analogRead(tempPin); // taking the temp pin reading and setting it equal to tempC variable
 tempC = (5.0*tempC*100.0)/1024.0; // will convert the analog input to a temperature in celcius
 Serial.print((byte)tempC); //will output the converted temperature to pc
 Serial.println("02");  //xbee 2
 
 voltage = analogRead(flamePin);
 voltage = (5.0*voltage)/1023.0;
 Serial.print("0");
 Serial.print((byte)voltage);
 Serial.println("04"); //flame sensor from xbee2
 
     
   

////////////////////////////////////////////////////////////    
 
 if (tempC>34)  //overtemp
 {
    if (voltage <= 3)  //there is flame and temp >40, give fire alert!
    {
   digitalWrite (red1, HIGH);  //overtemp
   digitalWrite (yellow1, LOW);  //overtemp    
   digitalWrite (red, HIGH);  //flame detected
   digitalWrite (buzzer, HIGH); //give alert
   }
   else  //temp>40, but no flame, possibility of false alarm
   {
   digitalWrite(red, LOW); //RED led, no flame alert
   digitalWrite (yellow1, LOW);  //overtemp
   digitalWrite (red1, HIGH);  //high temp alert
   digitalWrite (buzzer, LOW);   //no alert  
   }


 }    
//////////////////////////////////////////////////////////////
 
 
 else if ((tempC > 30) && (tempC <= 34))  //warning temp range
{
 if (voltage <= 3)  //there is flame and temp n warning range
    {
   digitalWrite (yellow1, HIGH);  //overtemp
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (red, HIGH);  //flame detected
   digitalWrite (buzzer, HIGH); //give alert
   }
   else  //warning temp, but no flame, possibility of false alarm
   {
   digitalWrite(red, LOW); //RED led, no flame alert
   digitalWrite (yellow1, HIGH);  //high temp alert
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (buzzer, LOW);   //no alert  
   }

}    
   else  //not in warning range and no flame
   {
   digitalWrite(red, LOW);  //RED led, no flame alert
   digitalWrite (yellow1, LOW);  //YELLOW led, no high temp alert    
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (buzzer, LOW);   //no alert
   }
 

 delay(3000);
}



PaulRB

Hi, have you read and understood the "Blink Without Delay" example sketch?

Paul

PaulRB

Ah, you edited your original post while I was replying...

The point of the BWD sketch is the principal of checking if a desired period has passed and taking an action when it has. In your case you have 2 desired periods: one of 3 secs and the other of 100ms. You have to keep track separately of the time each of those two periods began, and test separately for the end of either/both periods.

PaulRB

Post your sketch with the attempt to use the BWD principle.

yee0722

below is use the principle of blink without delay.. I know somewhere wrong but no ideas how to correct it.. because it involve two timing.. it can blink but blink with the delay in main loop. Any ideas? I just try it on yellow led only.

Code: [Select]
//declare variables
int buzzer = 5;
int yellow1 = 3;  //warning temp
int red1 = 4; // overtemp
int red = 2;  // red led indicate flame
float tempC;
float voltage;
int tempPin =0; // Temp sensor plugged analog pin 0
int flamePin= 1;

int yellow1state = LOW;
long previousMullis = 0;
long interval = 100;
// write setup function

void setup()
{
 Serial.begin(9600);// open serial port to communicate with the temp sensor
 pinMode(red1, OUTPUT);
 pinMode(yellow1, OUTPUT);  
 pinMode (red, OUTPUT);
 pinMode (buzzer, OUTPUT);

}

//write loop that will control what we want the arduino to do with the sensor readout

void loop()
{
 tempC = analogRead(tempPin); // taking the temp pin reading and setting it equal to tempC variable
 tempC = (5.0*tempC*100.0)/1024.0; // will convert the analog input to a temperature in celcius
 Serial.print((byte)tempC); //will output the converted temperature to pc
 Serial.println("02");  //xbee 2
 
 voltage = analogRead(flamePin);
 voltage = (5.0*voltage)/1023.0;
 Serial.print("0");
 Serial.print((byte)voltage);
 Serial.println("04"); //flame sensor from xbee2
 
     
   

////////////////////////////////////////////////////////////    
 
 if (tempC>34)  //overtemp
 {
    if (voltage <= 3)  //there is flame and temp >40, give fire alert!
    {
   digitalWrite (red1, HIGH);  //overtemp
   digitalWrite (yellow1, LOW);  //overtemp    
   digitalWrite (red, HIGH);  //flame detected
   digitalWrite (buzzer, HIGH); //give alert
   }
   else  //temp>40, but no flame, possibility of false alarm
   {
   digitalWrite(red, LOW); //RED led, no flame alert
   digitalWrite (yellow1, LOW);  //overtemp
   digitalWrite (red1, HIGH);  //high temp alert
   digitalWrite (buzzer, LOW);   //no alert  
   }


 }    
//////////////////////////////////////////////////////////////
 
 
 else if ((tempC > 30) && (tempC <= 34))  //warning temp range
{
 if (voltage <= 3)  //there is flame and temp n warning range
    {
   digitalWrite (yellow1, HIGH);  //overtemp
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (red, HIGH);  //flame detected
   digitalWrite (buzzer, HIGH); //give alert
   }
   else  //warning temp, but no flame, possibility of false alarm
   {
   digitalWrite(red, LOW); //RED led, no flame alert

   ///////////////////////////// yellow1 start blink

   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 (yellow1State == LOW)
     yellow1State = HIGH;
   else
     yellow1State = LOW;

   // set the LED with the ledState of the variable:
   digitalWrite(yellow1, yellow1State);
 }


   ////////////////////////////////
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (buzzer, LOW);   //no alert  
   }

}    
   else  //not in warning range and no flame
   {
   digitalWrite(red, LOW);  //RED led, no flame alert
   digitalWrite (yellow1, LOW);  //YELLOW led, no high temp alert    
   digitalWrite (red1, LOW);  //overtemp
   digitalWrite (buzzer, LOW);   //no alert
   }
 

 delay(3000);
}

PaulRB

#5
May 10, 2014, 10:04 am Last Edit: May 10, 2014, 10:08 am by PaulRB Reason: 1
Your post subject says "without using delay in main loop". So you already know what you need to do. There must be a reason why you have not done it, can you explain please?

Also I can see a spelling error in that sketch, it cannot have been tested.

I do not see any code there for testing if 3 seconds has passed and it is time to read your sensors again.

yee0722

okok.. I know what you means..  :smiley-roll-sweat: My title should "Blink LED without using delay in a delay main loop"....

I don't want to delete the delay in main loop..  :~

yee0722

Sorry my mistake on the typo in the sketch, here it is. No error detected.


Code: [Select]
//declare variables
int buzzer = 5;
int yellow1 = 3;  //warning temp
int red1 = 4; // overtemp
int red = 2;  // red led indicate flame
float tempC;
float voltage;
int tempPin =0; // Temp sensor plugged analog pin 0
int flamePin= 1;

int yellow1State = LOW;
long previousMillis = 0;
long interval = 100;
// write setup function

void setup()
{
  Serial.begin(9600);// open serial port to communicate with the temp sensor
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT); 
  pinMode (red, OUTPUT);
  pinMode (buzzer, OUTPUT);

}

//write loop that will control what we want the arduino to do with the sensor readout

void loop()
{
  tempC = analogRead(tempPin); // taking the temp pin reading and setting it equal to tempC variable
  tempC = (5.0*tempC*100.0)/1024.0; // will convert the analog input to a temperature in celcius
  Serial.print((byte)tempC); //will output the converted temperature to pc
  Serial.println("02");  //xbee 2
 
  voltage = analogRead(flamePin);
  voltage = (5.0*voltage)/1023.0;
  Serial.print("0");
  Serial.print((byte)voltage);
  Serial.println("04"); //flame sensor from xbee2
 
     
   

////////////////////////////////////////////////////////////   
 
  if (tempC>34)  //overtemp
  {
     if (voltage <= 3)  //there is flame and temp >40, give fire alert!
     {
    digitalWrite (red1, HIGH);  //overtemp
    digitalWrite (yellow1, LOW);  //overtemp     
    digitalWrite (red, HIGH);  //flame detected
    digitalWrite (buzzer, HIGH); //give alert
    }
    else  //temp>40, but no flame, possibility of false alarm
    {
    digitalWrite(red, LOW); //RED led, no flame alert
    digitalWrite (yellow1, LOW);  //overtemp
    digitalWrite (red1, HIGH);  //high temp alert
    digitalWrite (buzzer, LOW);   //no alert   
    }


  }   
//////////////////////////////////////////////////////////////
 
 
  else if ((tempC > 30) && (tempC <= 34))  //warning temp range
{
  if (voltage <= 3)  //there is flame and temp n warning range
     {
    digitalWrite (yellow1, HIGH);  //warning on
    digitalWrite (red1, LOW);  //overtemp off
    digitalWrite (red, HIGH);  //flame detected
    digitalWrite (buzzer, HIGH); //give alert
    }
    else  //warning temp, but no flame, possibility of false alarm
    {
    digitalWrite(red, LOW); //RED led, no flame alert

    ///////////////////////////// yellow1 start blink

    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 (yellow1State == LOW)
      yellow1State = HIGH;
    else
      yellow1State = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(yellow1, yellow1State);
  }


    ////////////////////////////////
    digitalWrite (red1, LOW);  //overtemp
    digitalWrite (buzzer, LOW);   //no alert   
    }

}   
    else  //not in warning range and no flame
    {
    digitalWrite(red, LOW);  //RED led, no flame alert
    digitalWrite (yellow1, LOW);  //YELLOW led, no high temp alert   
    digitalWrite (red1, LOW);  //overtemp
    digitalWrite (buzzer, LOW);   //no alert
    }
 

  delay(3000);
}

yee0722


I do not see any code there for testing if 3 seconds has passed and it is time to read your sensors again.


Sorry sir, what you means? I have delay at the last line "delay(3000)" in the main loop to read sensor every 3s.

Hackscribble

The reason PaulRB suggested using "blink without delay" is that the delay() function holds up the program for the length of the delay.  No other processing gets done!  So while your code is in the delay(3000), it cannot blink the LED. 

You need to remove the delay(3000) and add code to measure 3 seconds using the same technique as flashing the LED.  Then check your sensors each time the 3 seconds ends.
Hackscribble.  Writing about making things
hackscribble@outlook.com

yee0722

Sorry sir, what code should I add to replace the delay(3000)? 

PaulRB


I don't want to delete the delay in main loop..  :~


Why?

If you must keep that delay, you could replace your 3s delay with a loop that repeats 15 times. Inside the loop, swich your led on, delay for 100ms, switch your led off and delay another 100ms.

yee0722

ok sir, I know what you means.. i need do something on the main loop to read the sensor every 3s without using delay. Can you give me some hints on how to modify my sketch?

AWOL

Quote
Can you give me some hints on how to modify my sketch?

In the IDE there's an example called "blink without delay".
Read it, play with it, understand it.
And don't be blinkered by the word "blink" - the action could be anything.

PaulRB


Can you give me some hints on how to modify my sketch?


I already did in my second post above.

Please don't call me sir...

Go Up