Button acts as push instead of switch

I created a simple test project with a switch button that switches V1 from 0 → 1
All i want is that an external led on D4 and an internal led turns on.

The leds turn on and stay on but the button is turning to off again. I want that button on and when i press it the leds turn of and the button also.
What is wrong with my code?

Thx in advance

// vult een textveld in de app op de foon

#define BLYNK_PRINT Serial

#define SERIALDEBUG
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "sid";
char pass[] = "pass";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

void sendSeconds() {
  Blynk.virtualWrite(V0, millis() / 1000);
}

void sendMillis() {
  Blynk.virtualWrite(V1, millis());
}


BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.println("test");
  digitalWrite(13,1);
  digitalWrite(4,1);

  // process received value
}

void setup()
{
  // Debug console
  

   Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  // Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSeconds);
  // Setup a function to be called every second
  timer.setInterval(1000L, sendMillis);
}

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

@mikje please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

done and learned something :slight_smile:

Two things:

  1. your button widget in the app needs to be set to Switch not Push
  2. your BLYNK_WRITE(V1) command has no if statement to make it perform different actions when the button is turned on and off. Look at the “Bring it all together” section of this post to see how to do that:

Pete.

regarding 1) the button is set to switch, but when pressed it goes off within a second
regarding to 2) you are right and I changed my code

// vult een textveld in de app op de foon

#define BLYNK_PRINT Serial

#define SERIALDEBUG
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SID";
char pass[] = "PASS";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

void sendSeconds() {
  Blynk.virtualWrite(V0, millis() / 1000);
}

void sendMillis() {
  Blynk.virtualWrite(V1, millis());
}


BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  

 if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    
    Serial.println("test 1");
  digitalWrite(13,1);
  digitalWrite(4,1);
  {
  else
  {
    // execute this code if the switch widget is now OFF
        Serial.println("test 0");
    digitalWrite(13,0);
  digitalWrite(4,0);

  }
  // process received value
}

void setup()
{
  // Debug console
  

   Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  // Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSeconds);
  // Setup a function to be called every second
  timer.setInterval(1000L, sendMillis);
}

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

Copying my example exactly makes this line redundant:

This function, which is being called once per second via a timer, is your culprit:

V1 is the virtual pin that your switch widget is attached to.

Pete.

Thanx very much now it is working as expected. Stupid from me to oversee the Vnumber from the timer

1 Like