Esp 8266 blynk and blinking led

I know but if:

int current_state = digitalRead(16);

work with

digitalWrite(16,!current_state);

why

int current_state = digitalRead(5);

don’t work with

digitalWrite(5,!current_state);

they can’t be together this code:

    int current_state = digitalRead(16);
    int current_state = digitalRead(5);

if i delete:

int current_state = digitalRead(5);

i don’t have error

(I know, I am a serious clinical case… sorry)

(sometimes I wonder if I should take up something I don’t understand)

Why don’t you call them int current_state_pin_16 and int current_state_pin_5
That way, they will be variables with different names.

Pete.

if i copy and paste int current_state_pin_16 and int current_state_pin_5
and I’m verifying i have error : ‘current_state’ was not declared in this scope (text is grey):

 int current_state_pin_16
 int current_state_pin_5

(looks like it doesn’t recognize the code)
and if I will change code:

    int current_state_pin(16);
    int current_state_pin(5);
arduino ide turns the text brown

but if i verifying this code:

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state_pin(16);
    int current_state_pin(5);
    
    digitalWrite(16,!current_state);
    digitalWrite(5,!current_state);
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}

i have error
Compilation error: redeclaration of ‘int current_state_pin’

You need to change these variable names too!

Pete.

I don’t know why but now I don’t have any errors but if i upload code then no response to buttons
this is my code for now:

#define BLYNK_TEMPLATE_ID "TMPL4mVINjPLS"
#define BLYNK_TEMPLATE_NAME "lipa"
#define BLYNK_AUTH_TOKEN "JyB55r8vdWNlBZWmZNC3-l6oLLcT06V0"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Your WiFi credentials.
char ssid[] = "wifi";
char pass[] = "pass";

int blynk_led_switch = 0;

BlynkTimer timer;

void setup()
{
  // Debug console
  Serial.begin(74880);

  pinMode(16,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  pinMode(5,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500, toggle_led); // timer to toggle the LED every 500ms
}


void loop()
{
  Blynk.run();
  timer.run();
}

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(16);
    int current_state_pin_16;
    int current_state_pin_5;
  
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}


BLYNK_CONNECTED() // Triggered whne the device connects or re-connects to Blynk
{
  Blynk.syncVirtual(V0);  // Get the state of the V0 widget on startup
  Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup
}

BLYNK_WRITE(V0) // Triggered when the switch widget attache to V0 changes state
{
  // some code in here…
}


BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
{
  blynk_led_switch = param.asInt();
}

I don’t understand what you’re trying to achieve with this, so it’s difficult to understand what you’ve done wrong.

Pete.

Ok. Maybe this short video will explain everything :slight_smile:
https://www.youtube.com/watch?v=GU-iUqNFtVU

Not really. It doesn’t tell me which virtual datastreams the two button widgets are connected to.

Pete.

Hmm… if you give me first sketch you don’t need virtual datastreams because everything was in sketch
if:

pinMode(2,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2

connect to :

Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup

so why for example yyyyy…:

pinMode(16,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO16

(GPIO16 = D0 ) in wemos module
cannot be combined with (example)

Blynk.syncVirtual(V0);  // Get the state of the V0 widget on startup

or V2 ?

I don’t really understand your answer, but I suspect that’s because you don’t understand my question.

Your video shows two button widgets in switch mode on your mobile dashboard.
Every widget needs to be connected to a datastream, and in the basic example I posted we’re using Virtual datastreams. My instructions said…

So, presumably, one of your widgets in the video is attached to pin V1, but what about the other one?

Your latest code seems to imply that this will be datastream V0, but you have no code in the virtual pin handler that is triggered when the value of datystaream V0 changes…

So, forget videos, simply write an answer to these two questions…

  1. Which GPIO pin do you want to control with the widget attached to V1
  2. Which GPIO pin do you want to control with the widget attached to V0

Pete.

if your sketch tells me:

pinMode(2,OUTPUT) (GPIO2 =d4 pin in wemos) and connected to BLYNK_CONNECTED----> Blynk.syncVirtual(V1)----> BLYNK_WRITE(V1)
and if I create in blynk website switch button and I choose options V1 in widget
and this is work.
so why can’t I create another one led ? just like this:
second button for example
pinMode(16,OUTPUT) (GPIO16 =d0 pin in wemos) and connect to BLYNK_CONNECTED----> Blynk.syncVirtual(V0)----> BLYNK_WRITE(V0) and then I create button in blynk website and I will choose V0 (Datastream)

of course it doesn’t have to be GPIO16, it can be different, maybe GPIO14 or GPIO13.

that’s why I previously thought that if I copy and paste similar lines but change the pin numbers and V1 to V0, it will work, but it doesn’t work because I got an error.

With two LEDs you need to variables that indicate whether your LED should be flashing or not.

In the original code this was simply called blynk_led_switch because ther was only one switch widget.

The BLYNK_WRITE(V1) virtual pin handler set this variable to the value of the V1 datastream (0 for off or 1 for on).

With two virtual pins you need to use two DIFFERENT variables, and it would be good to give them sensible descriptive names.

One of the variables will be set in the BLYNK_WRITE(V1) virtual pin handler and the other in the BLYNK_WRITE(V0) virtual pin handler (which you aren’t currently doing).

Your toggle_led() function will then need two sets of if/else statements.
One which checks the state of the variable you’re using to track the status of the switch attached to V1, and one which checks the state of the variable used to track the switch attached to V0.

As previously discussed, when you read the GPIO16 and GPIO5 pins (or whichever pins you are now using) you need to read the state into two DIFFERENT variables, or declare a generic variable one and not re-declare it again later in the same function.

Pete.

I’m trying to understand what you wrote, although the language barrier complicates the situation a bit. I’m trying to change the settings myself and now I have no errors in this code when I verify it (I don’t even know if this code works on the device because I only check it on Arduino ide):

#define BLYNK_TEMPLATE_ID          "REDACTED"
#define BLYNK_TEMPLATE_NAME        "REDACTED"
#define BLYNK_AUTH_TOKEN           "REDACTED"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Your WiFi credentials.
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

int blynk_led_switch = 0;

BlynkTimer timer;

void setup()
{
  // Debug console
  Serial.begin(74880);

  pinMode(2,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  pinMode(16,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500, toggle_led); // timer to toggle the LED every 500ms
}


void loop()
{
  Blynk.run();
  timer.run();
}

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(2);
    digitalWrite(2,!current_state);
  }
  {
    int current_state = digitalRead(16);
    digitalWrite(16,!current_state);
  }
}

BLYNK_CONNECTED() // Triggered whne the device connects or re-connects to Blynk
{
  Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup
  Blynk.syncVirtual(V0);  // Get the state of the V0 widget on startup
}

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
{
  blynk_led_switch = param.asInt();
}

but when I try to add a line with

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
BLYNK_WRITE(V0) // Triggered when the switch widget attache to V0 changes state

i have error : Compilation error: exit status 1
or if I edit just like

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
}

BLYNK_WRITE(V0) // Triggered when the switch widget attache to V0 changes state
{

a have error: Compilation error: expected initializer before ‘}’ token

so this is like one step forward two steps back :slight_smile:

More like 10 steps back!

Where it says “some code in here” is where you need to insert some code, which does the same thing as the code in BLYNK_WRITE(V1) but with a different variable name than blynk_led_switch

Go back and re-read what I wrote in my last message, it should make more sense to you now.

Pete.

// some code in here…

I saw this text before but I didn’t realize that it could relate to what I want to achieve.

I thought it would all be easier. something like copy/paste and change the numbers and everything would work. and this time you need to have knowledge of C++ and the principles of its use…

Which takes us back to this discussion…

Had you said that you wanted more widgets, to control LEDs connected to different GPIOs then I would probably have written the demo sketch differently, but you didn’t vocalise that requirement.

Having said that, it is largely a copy/paste exercise, but yes, you do need to understand that C++ (or any programming language for that matter) has some basic rules.

I did add some in-code documentation which said that the BLYNK_WRITE(V1) function was triggered when the V1 datastream value changed.
I assumed that you’d realise that if you then add a widget attached to the V0 datastream you would need to copy/paste the BLYNK_WRITE(V1) code and make some minor changes to variables etc.

Pete.

yea… but I didn’t want to bother people too much with what I wanted to do because if I wrote “hey people, give me the code to flash 2 LEDs”
then people would tell me “find it on your own”
so I thought that all I needed was simple code and then I could do it myself. But unfortunately I had a collision with reality