I want to Push a Physical button and write on to a virtual pin with it

Greetings,

I want to tie a physical button to an vpin.

For example:
I push the Lightswitch so get a HIGH on pin 13 from my arduino mega 2560 and then it should write an HIGH signal onto the V10 Pin.

Unfortunate im a beginner and got stuck with the scetch.

Here It is :slight_smile:

> /*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.
    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app
  Blynk library is licensed under MIT license
  This example code is in public domain.
 *************************************************************
  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Mega 2560 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)
  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Look at your own ;) ";
void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(13, INPUT);
  int buttonState = 0;
  
  //Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  Blynk.begin(auth, IPAddress(192,168,1,11), 8442);
}
BLYNK_WRITE(V10)
{
 if digitalRead(13 == HIGH) 
   {
    Blynk.virtualWrite(10, 255);
   } 
 else
 {
  Blynk.virtualWrite(10, 0)
 }
}
void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

The Problem is that arduino IDE gives out this ERROR code:

sketch_jul08a:72: error: expected ‘;’ before ‘}’ token

}

^

exit status 1
expected ‘(’ before ‘digitalRead’

And i dont get it any more. i looked and tried it with the help of docs.blynk ut i think its to difficult :smiley:

Sorry for the bad english (Im from switzerland)

Thanks

Ok the issue with the IDE is fixed. i forgot some ()

Here is the new scetch:

> /*************************************************************
>   Download latest Blynk library here:
>     https://github.com/blynkkk/blynk-library/releases/latest

>   Blynk is a platform with iOS and Android apps to control
>   Arduino, Raspberry Pi and the likes over the Internet.
>   You can easily build graphic interfaces for all your
>   projects by simply dragging and dropping widgets.

>     Downloads, docs, tutorials: http://www.blynk.cc
>     Sketch generator:           http://examples.blynk.cc
>     Blynk community:            http://community.blynk.cc
>     Follow us:                  http://www.fb.com/blynkapp
>                                 http://twitter.com/blynk_app

>   Blynk library is licensed under MIT license
>   This example code is in public domain.

>  *************************************************************

>   You’ll need:
>    - Blynk App (download from AppStore or Google Play)
>    - Arduino Mega 2560 board
>    - Decide how to connect to Blynk
>      (USB, Ethernet, Wi-Fi, Bluetooth, ...)

>   There is a bunch of great example sketches included to show you how to get
>   started. Think of them as LEGO bricks  and combine them as you wish.
>   For example, take the Ethernet Shield sketch and combine it with the
>   Servo example, or choose a USB sketch and add a code from SendData
>   example.
>  *************************************************************/

> /* Comment this out to disable prints and save space */
> #define BLYNK_PRINT Serial


> #include <SPI.h>
> #include <Ethernet2.h>
> #include <BlynkSimpleEthernet2.h>

> // You should get Auth Token in the Blynk App.
> // Go to the Project Settings (nut icon).
> char auth[] = "look at you own";


> void setup()
> {
>   // Debug console
>   Serial.begin(9600);

>   pinMode(13, INPUT);
>   int buttonState = 0;
>   
>   //Blynk.begin(auth);
>   // You can also specify server:
>   //Blynk.begin(auth, "blynk-cloud.com", 8442);
>   Blynk.begin(auth, IPAddress(192,168,1,11), 8442);
> }


> BLYNK_WRITE(V10)
> {

>  if (digitalRead(13 == HIGH)) 
>    {
>     Blynk.virtualWrite(10, 255);
>    } 
>  else
>  {
>   Blynk.virtualWrite(10, 0);
>  }
> }
> void loop()

> {
>   Blynk.run();
>   // You can inject your own code or combine it with other sketches.
>   // Check other examples on how to communicate with Blynk. Remember
>   // to avoid delay() function!
> }

Problem is that i got nothing on the V10 pin

You should probably pull pin 13 low In void setup with
digitalWrite(13,LOW);
after the pinMode(13, INPUT); line

I assume your physical switch is connected between 5v positive and pin 13?

Pete.

Thanks Pete for your fast reply.
I added the line but no effects on the result :frowning:

Im also not sure if i forgot something or using the rong commands in the blynk write section . Or is something missing?

Gretings

I am guessing you are using a Display widget on V10 in the App, as a Button widget would only need a 1 or 0 to indicate feedback of ON or OFF… so not sure why you are pushing a 255 to the widget. EDIT - unless you are using a LED widget, in which case you are not setting it up correctly.

But then this statement is a bit confusing… do you mean you have NO widget, or not seeing any result?

Also, since Pin 13 is also wired to the internal LED of the MEGA, it’s own internal resistor may be adding to some confusion? I generaly avoid it for input purposes, so can’t say for sure.

What you wanted was if (digitalRead(13) == HIGH)

Yep, your sketch doesn’t make much sense right now.

If you go to https://examples.blynk.cc there is an examples called “Sync Physical Button”. You can borrow much of the code from there.

2 Likes

indeet i wantet to use the led widget to show me that the button is pushed.

I have the led widget in the blynk app but its not lighting up :slight_smile:

To eliminate this issue i^ll switched to pin 12

Thanks i didnt see that the ) was missing:)

Over all i look at the examples and test some things out. (its confusing to take bits of a code i dont understant to 100% and put it in my sketch :smiley: )
Thanks for the moment.

I’m glad you seem willing to understand what’s happening in code :slight_smile:

Firstly, you misunderstood BLYNK_WRITE() block. It is called when a state of a virtual pin is changed from elsewhere (e.g. in Blynk app).

So, move your syncing routine out from there and just call it periodically (say 50 ms) with timer.

Also, I believe you want to move int buttonState = 0; variable outside of the setup() (so that it is visible globally) and use it in your syncing routine so you don’t have to call Blynk.virtualWrite() on each timer call.

FINALLY :smiley:

Now i got some results :smiley:

Here is the new code (sorry for taking so much of the example code ;))

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.
    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app
  Blynk library is licensed under MIT license
  This example code is in public domain.
 *************************************************************
  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Mega 2560 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)
  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "look  at your own";
// Set your LED and physical button pins here
const int ledPin = 50;
const int btnPin = 12;
BlynkTimer timer;
void checkPhysicalButton();
int ledState = LOW;
int btnState = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V10);
  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V10) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}
void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState != LOW)
    {
      // Toggle LED state
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
      // Update Button Widget
      Blynk.virtualWrite(V10, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}
void setup()
{
  // Debug console
  Serial.begin(9600);
  //Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  Blynk.begin(auth, IPAddress(192,168,1,11), 8442);
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);
  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
   timer.run();
}

Now i get some signals onto the v10 pin.
only problem left is that i have a switch and the signal on v10 is line a self holding electrical cycle.

Info: Now i got the pullup on pin12 and “short” it on GND

Switch on = Pin 12 Low

Switch on → V10 Low
Switch off → V10 Low
Switch on → V10 High
Switch off → V10 High
Switch on → V10 Low
Switch off → V10 Low
Switch on → V10 High
Switch off → V10 High

In stead i need :

Switch on = Pin 12 Low

Switch on → V10 Low
Switch off → V10 High
Switch on → V10 Low
Switch off → V10 High
Switch on → V10 Low
Switch off → V10 High
Switch on → V10 Low
Switch off → V10 High

Thanks for the tipp with the examples.

Hi, been following your project with great interest because that’s what I want to do too. I want to use an output from my burglar alarm to notify me through the app on my phone.
Can you tell me if you have got your switch to work as you need it?
Thanks

Please note timestamps before posting… this is an older topic.

The question you ask is answered (in one of many ways) in a Sketch Builder…

Push Notification Button

Button Poll

Button Interrupt

And probably all over this forum :stuck_out_tongue_winking_eye:

Just Google how to use a transistor or optocoupler to transfer your alarm signal to a TTL safe output for use on your MCU (all that is NOT Blynk specific)