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();
}