Hello. First of all you have to understand that i am stupid. But i am not planning to stay that way…
I am new to Blynk. I am new to ESP8266 as well. I am trying to do a project and hopefully others after this one(if i am not killing myself in the process)
Description:
I have a “device” based on wemos D1 mini board. It reads 2 push buttons and execute a relay action based on 2 timers.
First timer is a total time. It starts with 20 when button V9 is pressed on the app. Also V9 sets the Countdown timer to 10.
The second one is physical button countdown time named buttonPin.
Both timers are countdown timers. So, when i press V9 both timers are set from 0(both) to 20 and 10 seconds respectively. The relay is also set from OFF to ON. For the 20 seconds or 10 seconds (whichever comes first to 0)
the relay is ON. The countdown time is counting down only while buttonPin is kept pressed. If you stop pressing it, the countdown time stops/freezes and awaits to press buttonPin again to countdown the remaining countdown time. If you do nothing after V9 is pressed, the Total time reaches 0 after 20 seconds and the relay is set to OFF. If you press the V9 again, the counters are incremented. So, Total time from 0 to 20 and Countdown time from 10 to 20 and the relay is back to ON. I know that in the code you will find the increments set to 5 and 10, but if i try to put 10 and 20 (as it should) the incremental values are doubled. No idea why…
All that i wrote previously it happens already. So the “device” works. With one difference. The V9, Total time, is not a physical button, but a blynk(virtual) button on my phone. When i press the V9 on the phone, everything works as expected. The buttonPin is a physical pushbutton and works perfect. No matter what i’ve tried i can’t make a second physical button (to replace V9) to work for the life of me. Keep in mind that i am newer than new on this
The builtIN LED on the board it’s used so i can see when the buttonPin (Countdown button) is pressed. Being set as a PUSH on the app, while i keep it pressed on the phone, the builtIN LED stays ON, so i know that the code is working. The builtIN LED will be replaced with a second relay. That i will do later. I have only one relay shield for now.
If someone can help me with the piece of code that will replace the blynk button(V9) with a physical one will be a big help for me. Not only that it will solve my problem but will help me understand how i should have done it.
Second question is: how can i make the timers to be displayed as minutes:seconds? So instead of 30(seconds) to actually display 0:30…0:29…0:28…etc.
Thank you for all your help. The entire sketch follows: ( P.S. Please forgive my english. It is not my native language.
Please also forgive my comments, it helps me to find my head around all that mess:frowning:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h> // library for SimpleTimer
SimpleTimer timer; // define a timer for use by SimpleTimer library
char auth[] = "token";
char ssid[] = "ssid";
char pass[] = "pass";
// constants
const int buttonPin = 15; // COUNTDOWN TIME physical pushbutton / pin D8 on D1 Mini
const int ledPin = 2; // Built_IN LED pin D4 on D1 Mini
// variables
int buttonState = 0; // variable for reading the buttonPin status
int Totaltime=0; // Global variable Total time
int Countdown=0; // Global variable Countdown time / run EverySecond
bool ONstatus = false; // variable to switch the relay ON and OFF
BLYNK_WRITE(V9){ // Blynk Button V9 for Cycle start and/or increment Totaltime and Countdown
Countdown = (Countdown+5);
Totaltime = (Totaltime+10);
digitalWrite(16, HIGH); // send ON signal to relay
}
void runEverySecond(){ // runs every second
if((Totaltime > 0)&& (ONstatus == true)){
Totaltime--; // reduce Totaltime by 1 second, every second
}
if((Countdown > 0)&& (ONstatus == true)&&(buttonState == HIGH)&&(Totaltime > 0)){
Countdown--; // reduce Countdown by 1 second every second
Serial.print(F("Device will work for "));
Serial.print(Countdown);
Serial.println(F(" second(s)"));
}
if((Countdown > 0) && (ONstatus == false)&&(buttonState == LOW)){
Serial.println(F("Device was switched ON"));
ONstatus = true; // to ensure device is only turned ON once
digitalWrite(16, HIGH); // send ON signal to relay
}
if((Countdown == 0) && (ONstatus == true)&&(buttonState == HIGH)){
Serial.println(F("Device is now OFF"));
ONstatus = false; // to ensure device is only turned OFF once
digitalWrite(16, LOW); // send OFF signal to relay
}
}
void setup(){
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
pinMode(16, OUTPUT); // relay pin
digitalWrite(16, LOW); // relay is OFF on reboot
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, runEverySecond); // start the 1s timer function
yield();
}
void loop()
{
Blynk.run();
timer.run();
buttonState = digitalRead(buttonPin);
Blynk.virtualWrite(4,Countdown); //blynk widget V4 for showing the remaining working time
Blynk.virtualWrite(10,Totaltime); //blynk widget V10 for showing the remaining total time
if((Totaltime == 0)&& (ONstatus == true)){ // disconecting on timeout
digitalWrite(16, LOW); // ensure relay is OFF
}
if((Totaltime == 0)&& (ONstatus == false)&&(Countdown>10)){ // keep conected
digitalWrite(16, HIGH); // ensure relay is ON
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//turn LED on:
digitalWrite(ledPin, LOW);
}
else {
//turn LED off:
digitalWrite(ledPin, HIGH);
}
}