I’m not going to write your code for you - partly because it’s better if you learn by doing it yourself and partly because I’d have to dig out a device and set it up to mirror your setup, which I don’t have the time or inclination to do.
I’d also suggest using sensible variable names, so that you (and others) can see what you’re trying to achieve and add plenty of comments to explain the logic.
If you find that you’re still stuck then post your latest code, plus serial debug (with comments so it makes sense) and details of what problems you’re experiencing.
Everyone has their own way of tackling programming tasks and it depends on how your brain works.
When I first started programming, computer time was very limited and you had to take turns at the one terminal that was available. As a result, we were taught how to plan the structure of the code on paper and dry run it, keeping track of variable changes as they occur. This was quite a handy approach and although I don’t really do this anymore, it’s still a handy technique if you find you’re getting bogged-down in code that isn’t working. I find that sketching out a flowchart with a series of yes/no decision boxes and a few action boxed allows me to get my hear around the way that the software should be working, but you need to find an approach that works for you.
Pete.
This is the final code for my project.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "XXXX";
char ssid[] = "XXXX";
char pass[] = "XXXX";
const int relayPin = 13;
const int btnPin = 14;
BlynkTimer timer;
int btnstate = LOW; // physical switch's latest state
int relaystate = LOW; //relays state
int widstate = LOW; // widget button's latest state
int btnflag = LOW; // physical switch's privious state
int widflag = LOW; // widget button's perivious state
BLYNK_WRITE(V3)
{
widstate = param.asInt(); //this gives widstate the latest state of the button
if (widstate == widflag) // all this if section sets the widgets button previous state
{
widflag = !(widstate);
digitalWrite(relayPin, widstate); //if the widget buton is pressed then turn relay on
}
}
void checkPhyButton()
{
btnstate = digitalRead(btnPin); //this gives btnstate the latest state of the physical switch
if (btnstate == btnflag) // this if section sets to btnflag the previus physical switch state,
{
btnflag = !(btnstate);
Blynk.virtualWrite(V3, btnstate); //change the status of the widget button changes and the widstate and widflag(just like you pressed the widget button)
if(btnstate == HIGH)
{
widstate = HIGH;
widflag = LOW;
}else {
widstate = LOW;
widflag = HIGH;
}
digitalWrite(relayPin, btnstate); //opens the relay or closes the relay from the physical switch
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(relayPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(relayPin, relaystate);
timer.setInterval(100L, checkPhyButton); //timer for the checkPhybutton
}
void loop()
{
Blynk.run();
timer.run();
}
It works perfect with no problems.
For some other Blynkers that maybe need this what i was trying to do is to control a relay from my phone and from a physical switch. With this, the Physical switch controls the relay, also it sets the status of the relay to a Widget button in the Blynk App.
So im happy to say Project Finished! Thank you a lot @PeteKnight and @Gunner your help was great from the beginning till the end!
1 Like