Virtual pin code

im trying out blynk with the particle photon, when im using virtual pins it doesn’t work like i expect, if i run my code and use virtual pin 1 no leds burn, until i use a button to activate D0 or D1 , 2, 3. then the according pin will join in the sequence. i tried putting high on all leds before my program starts, that didn’t work. i hope you can understand something of this story.

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>


#define BLYNK_PRINT Serial  // Set serial output for debug prints
//#define BLYNK_DEBUG       // Uncomment this to see detailed prints

#include <blynk.h>

// You should get Auth Token in the Blynk App.
char auth[] = "auth";



int ledRed = D0;
int ledWhite = D1;
int ledYellow = D2;
int ledGreen = D3;

int state = 0;
int prevState = 0;

void setup() {
    
    Serial.begin(9600);
    delay(5000);
    
    Blynk.begin(auth);
}

void loop() {
    Blynk.run();
    digitalWrite(D7, HIGH);
    
    if(state == 0){
        off();
    }
    if(state == 1){
        Blink();
    }
    if(state == 2){
        Breath();
    }
}

BLYNK_WRITE(V0){
    
    if(param.asInt() == 1){
        state = 0;
    }

    
}

BLYNK_WRITE(V1){
    
    if(param.asInt() == 1){
        prevState = state;
        state = 1;
    }
    if(param.asInt() == 0){
        state = prevState;
    }
}

BLYNK_WRITE(V2){
    
    if(param.asInt() == 1){
        prevState = state;
        state = 2;
    }
    if(param.asInt() == 0){
        state = prevState;
    }
    
}

void off(){
    
    digitalWrite(ledRed, LOW);
    digitalWrite(ledWhite, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, LOW);
}

void Blink(){
    
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledWhite, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, LOW);
    
    delay(1000);
    
    digitalWrite(ledRed, LOW);
    digitalWrite(ledWhite, HIGH);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, LOW);
    
    delay(1000);
    
    digitalWrite(ledRed, LOW);
    digitalWrite(ledWhite, LOW);
    digitalWrite(ledYellow, HIGH);
    digitalWrite(ledGreen, LOW);
    
    delay(1000);
    
    digitalWrite(ledRed, LOW);
    digitalWrite(ledWhite, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, HIGH);
    
    delay(1000);
}

void Breath(){
    
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledWhite, HIGH);
    digitalWrite(ledYellow, HIGH);
    digitalWrite(ledGreen, HIGH);
    
    delay(1000);
    
    digitalWrite(ledRed, LOW);
    digitalWrite(ledWhite, LOW);
    digitalWrite(ledYellow, LOW);
    digitalWrite(ledGreen, LOW);
    
    delay(1000);
}

Are you not able to paste formatted code rather than those horrible images?

1 Like

hope this update helps.

Your code is… well… bad… :confounded:

In your void loop() you are constantly (and unnecessarily) turning on D7 (whatever that is) and running your state checks, thousands of times a second.

Then when it runs a function based on a “state”, it hits massive delays that will cause Blynk Server disconnections.

And in the end, I still have no idea what you are trying to accomplish :stuck_out_tongue:

This is an easy one… With all those delays our colleague is trying to break the connection between the device and Blynk Server… :stuck_out_tongue_winking_eye:

@SanderPeters, avoid all delays!

1 Like