Multiple HIGH/LOW pin selections for a button

I am using 2 relays from a relay shield for the arduino Uno for controlling the forward and reverse direction of an actuator motor. When one relay is turned on the other must be turned off and visa-versa. Does the Blynk app allow me to do this with a button. So as to set HIGH for pin 6 and LOW for pin 7 for a button and visa-versa for another button? Or should I try an H-Bridge in the electronics instead of the relays?.Thanks.

You can write any logic you want, and trigger it by Button Widget attached to the Virtual Pin. Check this example on how to use Virtual Pins:

1 Like

I think you would need to hard program that logic into your sketch and decide which direction the motor should turn based on the state of the button, something simple like using the BLYNK_WRITE to change state of an indicator boolean.

I tested and the concept seemed to work.

You could do something like

BLYNK_WRITE(5)
{
buttonState=!buttonState;
}

and depending on if the buttonState were HIGH or LOW decide which relay combination to turn high and which to turn low.

You could also implement similar logic if you really wanted to use two buttons, but it would be a little more redundant.

Thank you for your input. You are right about the redundancy with 2 buttons. Can you please help me out a more on the logic for the one button since I am so weak in programming?

Robertben,

Please see below, I have not tested this, I don’t currently have a free Arduino to test on, but the basic logic stands behind what I was previously elaborating on. You want to initialize a Blynk button, check its state on the Arduino, and run your relays in a certain combination (HIGH/LOW or LOW/HIGH) depending on the state of the button. I’ve used a boolean (buttonState), which is modified by the Blynk button. This should do the trick.

Let me know if you run into issues, I can help troubleshoot!

Best,
Justin



#include <BlynkSimpleEthernet.h>
#include "Ethernet.h"
#include <SimpleTimer.h>

char auth[] = "12343333xxsdfs"; //blynk auth token

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(12, 123, 123, 12);

int relay1 = 1;
int relay2 = 2;

SimpleTimer timer;

boolean buttonState = HIGH; //set the initial direction of the relays, see below for if(HIGH) logic

void setup() {

  Blynk.begin(auth);
  Ethernet.begin(mac_addr);

  timer.setInterval(250, relayControl); //every .25 seconds relayControl will run, and react to buttonState

}

void loop() {

  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer

}


BLYNK_WRITE(5) //virtual pin 5 = button widget (Switch)
{
  buttonState = !buttonState;
}

void relayControl() {

  if (buttonState == HIGH) {

    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);

  }
  else {

    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);

  }

}


Ahh, finally I found a clue on what Vpins are :smiley: This should be linked in “Getting started” for people like me, who don’t exactly understand or know the terms/function and how to use it, but would like to, to find it with ease.

Thank you for heading me in the right direction, but had some issues. Had to mod and move some of the code around a bit. see below:

> int relay1 = 6;
> int relay2 = 7;
> SimpleTimer timer;
> boolean buttonState = 1; //changed this from High to 1

> void setup()
> {
>   Serial.begin(9600);
>   Blynk.begin(auth);
>   timer.setInterval(250, relayControl); //every .25 seconds relayControl will run, and react to buttonState

> }
> BLYNK_WRITE(5) //virtual pin 5 = button widget (Switch)
> {
>   buttonState = !buttonState;
> }
> void relayControl() {
>   if (buttonState == HIGH) {
>     digitalWrite(relay1, HIGH);
> digitalWrite(relay2, LOW);
>   }
>   else {
>    buttonState == LOW;
>     digitalWrite(relay1, LOW);
> digitalWrite(relay2, HIGH);
>   }}
> void loop()
> {
>  Blynk.run(); // Initiates Blynk
>   timer.run(); // Initiates SimpleTimer
> }

The odd thing is that the V5 button did not work until I turned on the relays with a button attached to the D6 and another to the D7 pins. Once I hit the D6 and D7 button in the Blynk android interface and turned the relays on I could use the the button with the virtual pin 5 attached to it. Not sure what is going on…

Try defining the pinMode’s and writing them to their initial state in void setup() like this:

pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
digitalWrite(relay1,LOW);
digitalWrite(relay1,HIGH);

also, set the original arrangement of the boolean to HIGH or LOW rather than 1.

Testing it out, this works well for me. The only issue I’m now running into is the fact that the button seems to push itself when you hit the play button, so each time the play button is pressed its actually toggling the switch.

I think this is a side effect of the way the program is written, but need to brainstorm on that.