How to turn off led for 5 sec before start as usual

Hi my name is amirul, i want to ask about arduino coding, i want to make a pet food dispenser but i am still trying by replacing led as output, this project works in a way able to control via phone via blynk and ultrasonic sensor able to detect the presence and turn on led lights , the coding I'm trying to create I want it to work like:

  • when blynk is pressed, the led light will come on
  • when the ultrasonic detects the presence, it will turn on the led and the led will turn off for 5 min before functioning again as usual.

I'm stuck and don't know how to turn off the led for 5 minutes.

Ask the master for help to deal with my problem, sorry to bother

#define BLYNK_TEMPLATE_ID "TMPLlYOcF71F"
#define BLYNK_DEVICE_NAME "SHIBAL"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h

#define USE_ESP32_DEV_MODULE

#define TRIG_PIN  26 // ESP32 pin GIOP26 connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN  25 // ESP32 pin GIOP25 connected to Ultrasonic Sensor's ECHO pin
#define LED_PIN   17 // ESP32 pin GIOP17 connected to LED's pin
#define DISTANCE_THRESHOLD 50 // centimeters

#include "BlynkEdgent.h"



BLYNK_WRITE(V0)
{
  int pinValue = param.asInt();
 // digitalWrite(17,pinValue);
 digitalWrite(LED_PIN,OUTPUT);
 delay(1000);
}

// The below are variables, which can be changed
float duration_us, distance_cm;


void setup() {
  Serial.begin(115200);       // initialize serial port
  pinMode(TRIG_PIN, OUTPUT); // set ESP32 pin to output mode
  pinMode(ECHO_PIN, INPUT);  // set ESP32 pin to input mode
  pinMode(LED_PIN, OUTPUT);  // set ESP32 pin to output mode
  
  delay(1000);

  BlynkEdgent.begin();
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if (distance_cm < DISTANCE_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);

  BlynkEdgent.run();
}

There are 300000 milli seconds in 5 minutes.
The MCU has a millis() counter.
unsigned long store3000000 = 300000;
unsigned long storetimeof5minstart = millis()
bool AllowOn = false;
If millis() - storetimeof5minstart >= store3000000
AllowOn = true

If AllowOn then allow LED to turn on.

1 Like

See Arduino IDE: File|Examples|02.Digitial|BlinkWithoutDelay for starters

Off or on? 5 min or 5 sec? It is unclear from your description whether the light is supposed to be on 1 sec after detecting something.

Whenever you see a before/after in your description think about what you need to write that condition in code.

It is difficult to measure "before", but "after" something is possible:

if(five_minutes_after_ultrasonic_presense_detected_time) {
...
}

but to do that you need to remember when ultrasonic_presence_detected_time happened.

You could add a global variable for it and then in your if statement you could remember the last time of the event:

  if (distance_cm < DISTANCE_THRESHOLD){
    digitalWrite(LED_PIN, HIGH); // turn on LED
    ultrasonic_presence_detected_time = millis();
}

and then you could write your conditional as:

if (millis() - ultrasonic_presence_detected_time >= 5 *60 *1000UL){
    ...

sorry for my bad description.it will on for 5 send and will turn off for 5 min before the ultrasonic sense again.

So there's two different time intervals related to the last time it detected something. Still, store a timestamp from the time it detected something and then use that to write your if statements.

can you share the coding that you edited cause I'm not good in coding and still in understanding the milis concept.

It isn't edited from anything other than your code. My additions were new work written just for you, trying to translate your words into advice on how to change your code.

State machines might be a good pattern to follow.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.