How to use two widgets on same digital pin

Hi. This is my first time using Blynk. I am interested how can I set two widgets on one digital pin.
e.g. slider and timer for light luminance
separately is no problem but I can’t use them both.

The best solution is to use Virtual pins, with the slider connected to one V pin and the timer connected to another.
You then write some code that takes the values from these V pins and applies your own set of logic rules and controls the digital pin of your MCU using a digitalWrite command.

Welcome to the world of programming!

Pete.

Switch to virtual pins and then use code on the device site to transfer those virtual commands to the digital pins.

E.g. For simplicity, I am just using a using a Button Widget (set to Switch mode) on V0 and the Timer Widget on V1 to turn ON or OFF the same LED on a digital pin (including feedback to the Button when the timer kicks in :wink: ) Assuming built in LED on Arduino’s pin 13

BLYNK_WRITE(V0) { // Button Widget function
digitalWrite(13, param.asInt()); // take the state of the Button and send it to the pin
}

BLYNK_WRITE(V1) { // Timer Widget function
digitalWrite(13, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V0, param.asInt()); // Send timer state back to button to update it.
}

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynk_writevpin

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

1 Like

I will try it.
Code goes in void setup() or void loop() ?

Neither… a function is a “free standing loop” of it’s own.

void setup() {
  // put your setup code here, to run once:
}

BLYNK_WRITE(vPin) {
  //  Blynk function stuff here
  //  called whenever the associated widget or vPin changes state
}

void MyFunction() {
  // Generic code functions & stuff here
  // typically called from other functions as needed.
}

void loop() {
  // continuously looping stuff here... like Blynk.run()
}
1 Like

Thanks. I got it. It was relatively easy.

I have few questions.
Why can’t we use same pin on two widgets?

How to sync two sliders connected to the same digital pin?
e.g. slider V1 (digital pin 2) and slider V2 (digital pin 2).
If I move slider V1, I want for slider V2 to move.

I don’t know… probably some technical reason that the developers had.

EDIT - and has since been made available, at least for virtual pins.

Assuming Arduino built in LED on pin 13…

BLYNK_WRITE(V0) { // Slider 0
analogWrite(13, param.asInt()); // take the state of the slider and send it to the pin
Blynk.virtualWrite(V1, param.asInt());  // Send slider 0 state back to slider 1
}

BLYNK_WRITE(V1) { // Slider 1
analogWrite(13, param.asInt()); // take the state of the slider and send it to the pin
Blynk.virtualWrite(V0, param.asInt()); // Send slider 1 state back to slider 0
}
2 Likes

Thank you.

I learned everything that I wanted. Your comments are really good.

I have everything in working condition.

Can I make one function from more than one function? This is only so my code looks cleaner. If it isn’t possible doesn’t matter. You already helped me

e.g. BLYNK_WRITE(V1) and BLYNK_WRITE(V2) and BLYNK_WRITE(V3)
to BLYNK_WRITE(V1, V2,V3) //doesn’t work for me

Thanks a lot. I was looking for the answer to a similar question for a long time. Your comment is simple and uncomplicated and easy to do or understand.

1 Like