BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Im struggling with a simple countdown

iOS, ESP32 Dev Board, Blynk Server

Im struggling with this countdown, as it stands; the countdown will only count down for as many seconds as the button is toggled on or off.
For example: If I rapidly toggle the button 5 times; it will count down 5 seconds. I obviously need it to count down to 0 to trigger a relay.

I’m eventually going to change the time of ‘i’ to be whatever it’s set to by the slider module.
I’m desperate guys!

#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#define RELAY_ON 1      // Define relay on pin state
#define RELAY_OFF 0     // Define relay off pin state
 
#define Relay  18       // Arduino pin where the relay connects
#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "9j1zLb9y1P8dZJShin-qgpLQCXw9L9nS";

int i = 15;

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lTime = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

BLYNK_WRITE(V1)
{
int button = param.asInt();
Serial.print(button);

reading = digitalRead(button);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - lTime > debounce) {
    if (state == HIGH){
      state = LOW;
    }
    else{
      state = HIGH;

    lTime = millis();    
   }
  }
if (state == HIGH){
  Blynk.virtualWrite(3, i);
  if (i>0) {
    i = i-1;
    Blynk.virtualWrite(3, i);
    delay(1000);
  }
  if (i==0) {
  digitalWrite(Relay, RELAY_ON);  // turn the relay on
    Serial.end();
   }
  }
}
  

void setup() {
  // put your setup code here, to run once:
   // Debug console
  Serial.begin(115200);

  Blynk.setDeviceName("Anchor");

  Blynk.begin(auth);
  

}


void loop() {

    Serial.println(state);
  
  Blynk.run();

}

Pete.

Hey thank you Pete!

So basically once the Timer hits 0 I want it to trigger a relay. Im very new to programming so forgive me if this is stupidly rudimentary.
Am I able to display the time remaining, and then do the action once it does hit 0?

What am I doing wrong in my code?

Could you provide your new code with countdown ?

Ive provided it up the top :slight_smile:

Yes but I don’t see any countdown :thinking:
I got it, It’s a manual count !

if (i>0) {    <------ never = 0
    i = i-1;
    Blynk.virtualWrite(3, i);
    delay(1000);
  }
  if (i==0) {
    digitalWrite(Relay, RELAY_ON);  // turn the relay on
    Serial.end();
    }else {
    i = i-1;
    Blynk.virtualWrite(3, i);
    delay(1000);
      }
  }

Sorry dude do you mind explaining what you did there? Im so new to this and i struggle to break down all the syntax

in your code, your ‘i’ never becomes zero, so I change the ‘if’ statement :wink: