[SOLVED] Unable to read from slider

Hi!

I’m trying to control a pwm output to control a mosfet using a button and a slider (The button turns full on or full off, but the slider will dim the output) and I’m struggeling reading from the slider.

The slider is on V0 and what I want to do (right now) is read from the slider and if the value is different than 0 change the button state to On and if it’s 0 change it to Off. The button is on D0.

That part of the sketch is this:

BLYNK_CONNECTED(){
    Blynk.syncVirtual(V0);
}
BLYNK_WRITE(V0) {

    if (param.asInt()==0) {
        //LOW
        // change button to off
        Blynk.setProperty(D0,"onLabel","OFF");
    }else{
        // change button to on
        Blynk.setProperty(D0,"onLabel","ON");
    }
}

What can I do to read this?

Thanks

I never use physical pins in Blynk but I think setProperty is restricted to the mighty virtual pins.

1 Like

You will definitely need to switch to full virtual pins and pass that data onto your PWM for digital pin control.

NOTE: you need to insert the applicable virtual and digital pin designations in this code (and it is uncompiled, so watch for syntax errors)

BLYNK_WRITE(vPin) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin
  int value = param.asInt();
  analogWrite(pin, value);
}

If you really want the Button to match the ON/OFF state of the Slider, ON meaning anything other than 0, then you can do it this way…

Also, there is no need to call Blynk.syncVirtual(vPin); in this case as the slider is already controlling the digital pin state as you touch it.

BLYNK_WRITE(vPin) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin
  int value = param.asInt();
  analogWrite(pin, value);
  if (value == 0) { // If slider is OFF, make sure button is as well
    Blynk.virtualWrite(vPin, 0) // Set Button to OFF
  } else ( // If slider is anything equal or higher than 1, set button ON
      Blynk.virtualWrite(vPin, 1) // Set Button to ON
  }
}

You can also do the inverse and set the Slider to full ON when the Button is pressed ON, etc… but I will let you try that one out yourself :wink:

1 Like

Thanks! I’m at work right now. Will try in a few hours :slight_smile:

@Gunner I’ve tried what you told me and it’s not working. I’m leaving here the function:

BLYNK_WRITE(V0) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin
  int value = param.asInt();
  analogWrite(mosfetPin, value);
  if (value == 0) { // If slider is OFF, make sure button is as well
    Blynk.virtualWrite(V1,0);
    //Blynk.setProperty(V1,"onLabel","OFF");
  } else { // If slider is anything equal or higher than 1, set button ON
    Blynk.virtualWrite(V1,1);
    //Blynk.setProperty(V1,"onLabel","ON");
  }
}

Hi!

I just dig into the code with enough time and I just see that I’m changing the label that is shown in the button widget, but, no changing the state of the button. Now I’ve changed my code and looks like this:

// SLIDER
BLYNK_WRITE(V0) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin
  int value = param.asInt();
  analogWrite(mosfetPin, value);
  if (value == 0) { // If slider is OFF, make sure button is as well
    Blynk.virtualWrite(V1,0);
    Blynk.setProperty(V1,"offLabel","OFF");
  } else { // If slider is anything equal or higher than 1, set button ON
    Blynk.virtualWrite(V1,1);
    Blynk.setProperty(V1,"onLabel","ON");
  }
}

// BUTTON
BLYNK_WRITE(V1) {
    int value = param.asInt();
    digitalWrite(mosfetPin,value);
    if (value==0) {
        //LOW
        // change button to off
        Blynk.virtualWrite(V0,0);
    }else{        // change button to on
        
        Blynk.virtualWrite(V0,255);
    }
}

The problem is that I don’t know how to change the status of the button (V1) to “on”.

Greetings.

255 should be 1. The only time it is 255 is if you are working with Blynk’s LED’s.

V0 is the Slider, so, when I click On the Slider goes to 255 (full)

Ah, OK

Try adding:

Blynk.syncVirtual(V1);

after:

Blynk.virtualWrite(V1,1);
Blynk.setProperty(V1,"onLabel","ON");

Now it’s working but I need to change the logic to make it work properly.

Thanks!

This is the code than I’m usign right now. The logic is not perfect, but changing how the buttons displays turns out into a full on although the slider was in the middle, so, that functionality is off by now. Will try to change whenever I got some time, but it’s working right now.

// SLIDER
BLYNK_WRITE(V0) {  // reads value from slider, set range of 0-255, and passes it onto PWM pin

    //Blynk.syncVirtual(V1);
    int value = param.asInt();
    analogWrite(mosfetPin, value);
    if (value == 0) { // If slider is OFF, make sure button is as well
        Blynk.virtualWrite(V1,0);
        Blynk.setProperty(V1,"offLabel","OFF");
    }else { // If slider is anything equal or higher than 1, set button ON
        Blynk.virtualWrite(V1,1);
        Blynk.setProperty(V1,"onLabel","ON");
    }
}

// BUTTON
BLYNK_WRITE(V1) {
    int value = param.asInt();
    digitalWrite(mosfetPin,value);
    if (value==0) {
        //LOW
        // change button to off
        Blynk.virtualWrite(V0,0);
    }else{        // change button to on
        
        Blynk.virtualWrite(V0,255);
    }
}

Greetings!

These commands are unnecessary as they are redundant to what is already standard on the button itself… and since you can type in any message that will fit, in the button setup. The only reason to use this if if you are dynamically changing the messages, via code, that you want to see on the buttons. i.e. A counter for how many times the button has been pushed, etc.

Hi!

I know, I need to change it and some other small things that can improve the “app” but from now everything is working at 90% of how it should be (in my mind).