[SOLVED] 1 button, 2 Tasks (relay and flashing indicator LED)

Hi Guys! I want to when I click on button widget (pin V19) (PUSH BUTTON) it turn ON per 1seg and OFF the D3 pin and blink LED on pin D9 for 20 seg
(1 button, 2 task)

how can I do that?

here is the code


#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>  
#include <SimpleTimer.h>

/***********************************************************************************************************************************************/

char auth[] = "545db7988exxxxxxxxxx6b99bc25";

/***********************************************************************************************************************************************/

#define W5100_CS  10
#define SDCARD_CS 4
#define DHTPIN 2  
#define DHTTYPE DHT11  

/***********************************************************************************************************************************************/

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

/***********************************************************************************************************************************************/

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

/***********************************************************************************************************************************************/

void setup()
{
  Serial.begin(9600);
  
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);
   
  Blynk.begin(auth);
  
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(5, HIGH);  
  digitalWrite(6, HIGH);  
  digitalWrite(7, HIGH); 
  digitalWrite(8, HIGH); 
  
  dht.begin();
  
  timer.setInterval(1000L, sendSensor);
}

BLYNK_WRITE(V9)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(5, HIGH);
    
  } 
else
  {
    digitalWrite(5, LOW);
  }
  
}

BLYNK_WRITE(V15)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(6, HIGH);
    
  } 
else
  {
    digitalWrite(6, LOW);
  }
  
}

BLYNK_WRITE(V7)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(7, HIGH);
    
  } 
else
  {
    digitalWrite(7, LOW);
  }
  
}

BLYNK_WRITE(V8)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(8, HIGH);
    
  } 
else
  {
    digitalWrite(8, LOW);
  }

}


BLYNK_WRITE(V19)
{
  if (param.asInt() == 0) 
  {
    digitalWrite(3, HIGH);
    
  } 


else
  {
    digitalWrite(3, LOW);
    delay(2000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(9, HIGH);
    delay(500);
       
  }
}
  

/***********************************************************************************************************************************************/
void loop()
{
  Blynk.run();
  timer.run();
}
/***********************************************************************************************************************************************/


Actually neither option is recommended for a Blynk sketch due to use of delay() commands. Best to use timer functions like this:

int RLY_LED;
int Flag;

//===== RELAY_LED PULSE - BLYNK Functions =====
BLYNK_WRITE(V19)  // Virtual button on V19 to activate Relay_LED pulses
{
  RLY_LED = param.asInt();
  if (RLY_LED == 1 && Flag == 0){  // If button is pressed (in momentary mode)
    Flag = 1;  // Keeps from allowing button press more then once while relay activated
    digitalWrite(3, HIGH); // Activate Relay (assuming High to activate)
    timer.setTimer(2000L, RelayOFF, 1);  // Deactivate Relay after 2 seconds
    timer.setTimer(400L, LedON, 40);  // Pulse LED ON/OFF routine every 500ms (400ms OFF, 100ms ON) for 40 times (20 seconds total)
  }  // END if
}  // END Blynk Function

void RelayOFF()  // Deactivate relay
{
  digitalWrite(3, LOW);  // Deactivate relay pin
  Flag = 0;  // reset flag after relay disables
}  // END RelayOFF Function

void LedON()  // Turn on LED for 100ms
{
  digitalWrite(9, HIGH);  // Turn on LED pin
  timer.setTimer(100L, LedOFF, 1);  // Run OFFLED function once in 100ms
}  // END LedON Function

void LedOFF()  // Turn off LED
{
  digitalWrite(9, LOW);  // Turn off LED pin
}  // END LedOFF Function

@Gunner Thank you very much! It really works!!!
But i think it is a bit faster, i want 1seg ON and 1seg OFF, then I change the
timer.setTimer(400L, LedON, 40); to -> timer.setTimer(1000L, LedON, 20);
and
timer.setTimer(100L, LedOFF, 1); to -> timer.setTimer(1000L, LedOFF, 1);
but it don’t work, this doesn’t turn ON D3 pin and being blinking fast and slow… What can I do?

My code example sets up two separate loops from one button press. One loop activates and deactivates the relay, while the second flashes the LED on and off

EDIT - OK, I see what is happening. I keep forgetting that each timer call is independent of the others. So when the 1st one-second-timer called the 2nd one-second-timer, it doesn’t wait for the 2nd one to finish before repeating itself, thus effectively having 2 one-second-timers trying to turn ON and OFF the same LED at the same time :stuck_out_tongue:

OK, so simply change the first LED timer to two-seconds as that will allow one-second for the ON loop and one-second for the LED to be OFF before repeating itself 10 times (total of 20 seconds of a slow flashing LED).

timer.setTimer(2000L, LedON, 10);

timer.setTimer(1000L, LedOFF, 1);

NOTE also that the way I set up the flag only prevents repeated button pushes from initiating until the relay timer expires (first 2 seconds)… but after that any additional button press with start the relay loop again and also add another concurrent flash loop (if the first one it is still running). So depending on when the button is re-pressed, you could effectively have multiple flashing loops running concurrently, making for strange flashing patterns.