BLYNK_WRITE to a variable and a bug?

I guess I missed something, I want to save a value set by BLYNK to the EEPROM. I got the save to work fine but it does not seem like I got the BLYNK part right, since a new value is not captured in the dimValue.

The simple target with the code is to save last value of a dimmer before turning of power and start with that value when power on.

(I also discovered that I must have the Blynk.begin(auth); after the Particle.variables in setup(); otherwise I wont get any readings in the console, is it suppose to be like that a bug or?)


#include <blynk.h>

int dimmerPin = A4;   // PWM Dimmer 0-255 
int dirPinA = A5;     // Ain 1 sets dir.      
int standbyPin = A7;  // Standby, needs to be HIGH to activate motor controller 


int dimValue;       // light parameters
int last_dimValue;  // light parameters
// uint32_t dimValue;       // light parameters
// uint32_t last_dimValue;  // light parameters
int addr = 10;          // EEPROM set-up

char auth[]="4c80somethingelse9544e39de0c2d"; //BLYNK CODE

BLYNK_WRITE(V9)
{   
    analogWrite(dimValue, param.asInt());
    analogWrite(dimmerPin, param.asInt());  
    
}

void setup() {
    
      
    // Blynk.begin(auth); //PROBLEM/BUG?: if I put Blynk.begin here Particle Variable wont "work"
    Particle.variable("DimValue", dimValue); // For debugging
    Particle.variable("lastDim", last_dimValue);
   
    
    // Activate controller 
    pinMode(standbyPin, OUTPUT);
    digitalWrite(standbyPin, HIGH);
    
    // LED set-up
    pinMode(dimmerPin, OUTPUT);     
    pinMode(dirPinA, OUTPUT);     
    digitalWrite(dirPinA, HIGH);

    // Light goes to last state before power off when turned on again
    EEPROM.put(addr, 30);
    EEPROM.get(addr, dimValue);
    analogWrite(dimmerPin, dimValue);
    
    Blynk.begin(auth); //PROBLEM/BUG?: if I put Blynk.begin here Particle Variable "work" fine
    
 }


void loop() {
    
    
    if (dimValue != last_dimValue) 
        {
        EEPROM.put(addr, dimValue);
        last_dimValue=dimValue;

        }
        
    Blynk.run();
}


    EEPROM.put(addr, 30);
    EEPROM.get(addr, dimValue);

Why are you hard coding 30 as dimValue after a reset?
There is no surprise that you never see any other value on restart :wink:

About the variable registration: It’s not a bug. Currently there is a short window after getting connected to the cloud where Particle.variable(), Particle.function() and Particle.subscribe() can be registered with the cloud.
If you miss that window, you need to Particle.disconnect() and Particle.connect() to inform the cloud of the changed state.

ahhh I forgot to remove that, I put it in for debugging, but it still does not work without the line… I think it is something with the blynk write that does not work as I think.

thanks for the clarification regarding particle variables and blynk aut.

I guess you should also initialize last_dimValue in setup() (or even better in STARTUP()) after you read the value from EEPROM.

And I’d consider it good practice to mirror back the current value to Blynk once you read the value from EEPROM.

This also doesn’t seem to make any sense

analogWrite(dimValue, param.asInt());

the first parameter is the pin number

I guess you rather want to do that

BLYNK_WRITE(V9)
{   
    dimValue = param.asInt();
    analogWrite(dimmerPin, dimValue);  
}
1 Like

You are the best, thanks a lot ScruffR, the new blynk write made it!

1 Like