BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Can't SYNC PHYSICAL BUTTON WITH APP

i want to control led state and brightness via pwm using transistor by 3 PHYSICAL push buttons as well as app switches

cant understand why the physical push power button i.e function( void checkPhysicalPOWERButton(); ) cant push change in virtual pin V2 switch state
by Blynk.Write(V2,ledState);

im using arduino uno via serial USB
here is the code----->

DELETED unformatted code - Moderator

@farzan.wani 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.

You must have a macro for this repetitively needed statement :stuck_out_tongue:

Predictive text on my iPad/phone most of the time.
Can get messy when attempting to write messages to friends and family though :grinning:

I’ve also started setting topic timers whenever I write it, to remind me to go back and tidy-up if thevcode hasn’t been sorted. I’ve also adopted a rule of never looking at unformatted code (or at least never responding with helpful feedback until it’s sorted).

Pete.

Wife text - “Please pick up milk and butter”

Pete text - " Backticks or you are deleted!.. darn auto correct… Honey dearest??"

1 Like

Hard to read your unformatted code… but I can’t stick around to wait for you to fix it, so…

This might change the widget, but without a follow up sync command nothing will happen in code for the V2 function… is that what you are needing?

i want to control led state and brightness via pwm using transistor by 3 PHYSICAL push buttons as well as app switches. where V2 is in switch state, V3 and V4 in push State.
everything works fine but when i press physical power button it doesn’t update V2 status.

cant understand why the physical push power button described by function( void checkPhysicalPOWERButton(); ) cant change status in virtual pin V2 switch widget
by Blynk.Write(V2,ledState) written in checkPhysicalPOWERButton() function

im using arduino uno via serial USB

here is the code----->


#define BLYNK_PRINT SwSerial //arduino serial commn setup
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
char auth[] = “8-----3hqE_1J49a-z8odawPJDdPyZq58”;
const int LED_PIN = 3 ;
const int UP_BUTTON_PIN =8 ;
const int DOWN_BUTTON_PIN =7 ;
const int POWER_BUTTON_PIN =12 ;
const int FADE_DELAY =10 ;
const int FADE_PERCENTAGE =20 ;
int currentLevel = 0 ;
int fadeLevel = 0 ;
unsigned long upPreviousMillis = 0;
unsigned long downPreviousMillis = 0;
unsigned long buttonFadeDelay = 100;
int upValue =0 ;
int downValue =0 ;
int powerValue = LOW   ;
int btnState = HIGH ; //pull up mode
BlynkTimer timer ; //sync comm need timer
void checkPhysicalPOWERButton(); //function described later
void checkPhysicalupButton();
void checkPhysicaldownButton();
BLYNK_CONNECTED() {
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);}
BLYNK_WRITE(V2) {
powerValue = param.asInt(); //change the state according to the change in state of app
if (powerValue == HIGH){
if(fadeLevel == 0) {
fadeToLevel(50);
fadeLevel = 50;
}
else{
fadeToLevel(fadeLevel);
      }
}
if (powerValue == LOW){
fadeToLevel(0);
}
}
BLYNK_WRITE(V3){
upValue=param.asInt();
if(powerValue== HIGH){
if ((upValue==HIGH)&&(fadeLevel<100)){
fadeLevel += FADE_PERCENTAGE;
fadeLevel= fadeLevel >100 ? 100 : fadeLevel;
fadeToLevel(fadeLevel);
}
}}
BLYNK_WRITE(V4){
downValue=param.asInt();
if(powerValue== HIGH ){
if ((downValue==HIGH)&&(fadeLevel>0)){
fadeLevel -= FADE_PERCENTAGE;
fadeLevel= fadeLevel <5 ? 5: fadeLevel;
fadeToLevel(fadeLevel);
}
}
}
void checkPhysicalPOWERButton()
{
if (digitalRead(POWER_BUTTON_PIN) == LOW) {
if (btnState != LOW) {
// Toggle LED state
powerValue= !powerValue;
Blynk.virtualWrite(V2, powerValue);  //  WHY DOES’NT IT Update Button Widget state?
if (currentLevel > 0) {
fadeToLevel( 0 );
}
else if
(fadeLevel == 0) {
fadeToLevel(50);
fadeLevel = 50;
}
else{
fadeToLevel(fadeLevel);
}
}
btnState= LOW;
}else{
btnState=HIGH;
}
}
void checkPhysicalupButton(){
if(powerValue==HIGH){
if (digitalRead(UP_BUTTON_PIN ) == LOW) {
unsigned long upCurrentMillis = millis();
if(upCurrentMillis - upPreviousMillis > buttonFadeDelay)
{
if (fadeLevel<100){
fadeLevel += FADE_PERCENTAGE;
fadeLevel = fadeLevel > 100 ? 100 : fadeLevel;
fadeToLevel( fadeLevel );
  }
  upPreviousMillis = upCurrentMillis;
}
}
}
}
void checkPhysicaldownButton(){
if(powerValue==HIGH){
if (digitalRead(DOWN_BUTTON_PIN ) == LOW) {
unsigned long downCurrentMillis = millis();
if(downCurrentMillis - downPreviousMillis > buttonFadeDelay)
{
if (fadeLevel>0){
fadeLevel -= FADE_PERCENTAGE;
fadeLevel = fadeLevel <5 ? 5 : fadeLevel;
fadeToLevel( fadeLevel );
  }
  downPreviousMillis = downCurrentMillis;
}
}
}
}
void fadeToLevel( int toLevel ) {

Serial.print("toLevel Value: ");
Serial.println(toLevel);
Serial.print("currentLevel Value: ");
Serial.println(currentLevel);

int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;

while ( currentLevel != toLevel ) {
currentLevel += delta;
analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
delay( FADE_DELAY );
}
}
void setup()
{
// Debug console
SwSerial.begin(9600);
// Blynk will work through Serial
// Do not read or write this serial manually in your sketch
Serial.begin(9600);
Blynk.begin(Serial, auth);
pinMode(LED_PIN, OUTPUT);
pinMode(POWER_BUTTON_PIN, INPUT_PULLUP);
pinMode(UP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalPOWERButton);
timer.setInterval(1130L, checkPhysicalupButton);
timer.setInterval(129L, checkPhysicaldownButton);
}
void loop()
{
Blynk.run();
timer.run();
}

Please don’t create duplicate topics for same issue… I merged them both here.

i was not able to edit the previous one since no pencil option was showing at the bottom of the post. so i tried to delete the same and create the new one.

PLZ take a look at the code . I need it immidiately
thanks in advance.

We (other Blynk users and forum members, like you) are not here to troubleshoot or fix code on demand… rather to help guide you to learn about Blynk. And on our own time, not on yours :stuck_out_tongue:

When syncing Virtual and Physical buttons, I find it best to…

  • Do all the work via BLYNK_WRITE(vPIN) function, via Virtual Buttons
  • Use polling or interrupts on the physical buttons (with some form of debouncing) that gets the new state…
  • … and call a simple void function that changes the vPin state with a Blynk.virtualWrite(vPin, value) followed by a Blynk.syncVirtual(vPin) to run the needed Blynk Function with the new state.

can you edit the same and post please…
.im a bit confused ,can we call BLYNK.syncVirtual(vPin) outside BLYNK_Connected() function

I find your code confusing… I suggest you start over with a single virtual and physical button and learn how it works.

Of course, it is just a command like Blynk.virtualWrite(vPin, value) or digitalWrite(pin, value), etc.)

Perhaps check out the docs?

When i try on single Virtual and physical button …it works fine . but when i call some other function in checkPhysicalButton() it does’nt work. Can’t understand Why?
in the mean time , i will try calling a separate function for change in status

Start by simply duplicating your Blynk and Physical functions for each needed combo… using different vPin and interrupts/polling for each (or combining the multiple polling checks in a single timer).

Actually only single virtual pin V2 is needed to update its status . Rest are the Switch widgets in push states.

Well, since I am not about to dig through your code on my time… sleep beckons…

Normally I would recommend one drop in a bunch of Serial.println("this thing happened"); type commands and follow the flow of your code in your serial monitor so you can see what is really happening…

But… as you are using USB link, I guess you can only do that if you use softserial, a TTL adapter and another terminal program (I personally use Termite).

BLYNK.syncVirtual(VPin)Can we write it inside if function, as i see its color does’nt change inside if statement

using a separate void function with Blynk.virtualWrite(vPin, value) followed by a Blynk.syncVirtual(vPin) to change vPin state
did not solve the issue

Well, what can I say… My virtual & physical button method described above works for me.

@farzan.wani your code is a mess of nested if statements and far too complex.

If you break the problem down into its components you have the following…
If the physical or virtual “up” button is pressed you want to trigger an “up” action.
If the physical or virtual “down” button is pressed you want to trigger an “down” action.

The way to handle this in C++ is to have your “up” action as a function (maybe called void do_UP_action) and the same for the down action.

You then call these function when either the up or down physical or virtual buttons are activated.

As far as synchronisation of the app with the physical switches is concerned, it really depends on what type of physical switches and widget switch settings you are using.
If you have momentary pushbutton physical switches then synchronisation is really a waste of time, as you’ll only press the physical button for a short period then release it. Echoing this to the app momentary widget button achieves very little and although it can be done it isn’t really worth the effort.

If you are using physical changeover switches, and have your button widgets set to ‘Switch’ then echoing your physical switch state to the widget button is very simple.

Pete.