Producing pulses

Hi,

I’m using the following working test code to activate various relays using digital pins (not virtual).

I need to pulse the relays 3 times instead of once. I can do that by pressing the button on the app 3 times but I want Blynk to do it for me.

Can it be done and, if so, how?
TIA

#include <Bridge.h>
#include <BlynkSimpleYun.h>

/* Blynk auth token */
char auth[] = "";

void setup()
{

  Bridge.begin();
  Console.begin();

  Blynk.begin(auth);
}

void loop() {
  Blynk.run(); // has to be the last line
  }

I am certain it can be done. I suspect it would be done through some coding.

Or are you asking someone to write the code for you? Cause If that’s the case… I got my own code to write. I am more than willing to look at yours though, and see if I can provide any input. I am sure other are willing as well. Maybe someone here has time to write the code for you, but this is more of a spot for help than a place to get your coding done for you. (I am still looking for that place, :grin:).

@Toro I probably I didn’t express myself well. No, I was not asking anyone to write a full code for me. I was just looking for a hint or guidance.

As I stated earlier, I’m using digital pins but if I was to use virtual pins I would do something like this…

/* == == = BLYNK CONTROLS == == = */
BLYNK_WRITE(V1)
{
blinds = param.asInt();
if ( blinds == 1)
{
  digitalWrite(blindsOpenRelayPin, LOW); // activate relay
  timerV1.setTimeout(500, deactivateRelayV1);
  digitalWrite(blindsOpenRelayPin, LOW); // activate relay
  timerV1.setTimeout(500, deactivateRelayV1);
  digitalWrite(blindsOpenRelayPin, LOW); // activate relay
  timerV1.setTimeout(500, deactivateRelayV1);
}
}

void deactivateRelayV1() {
  digitalWrite(blindsOpenRelayPin, HIGH); // deactivate relay
}

However, I want to use the digital pins. Normally I’d use delay between pulses but as I understand it, cannot do that when using Blynk.

@Emilio

That code looks good to me. As far as digital pins, isn’t that what is being switched on and off? That is, blindsOpenRelayPin is cycling from LOW to HIGH every .5 seconds. What is blindsOpenRelayPin defined as? I would think it would be the digital pin that the blinds relay is connected to. (e.g . #define blindsOpenRelayPin 4) //relay connected to digital pin 4

You might also be able to eliminate the blinds variable with:
if (param.asInt() == 1)
unless you use the blinds variable elsewhere, then I would leave it.

Does this code not give you the expected result? If not, what is it doing?

The question is that I don’t want to use virtual pins. Switching 10 relays using virtual pins use too much memory versus using digital pins as in my first post.

Your question then becomes confusing… if you DON’T want to use virtual pins, to activate the digital pins the relays are already hooked up to, then why use Blynk at all?

Using a Button Widget to directly control a single digital pin will work fine, but to control more or do so in pulses will require code and the use of Virtual pins.

SimpleTimer will work with or without Blynk, and will handle timing for anything, not just virtual pins.

So our question is… what exactly are you wanting to do?

The virtual pin is only being used for the button widget.

I guess you could do away with the BLYNK_WRITE (V1), and just have an IF statement that would look at a digital pin.

Something like below, with button widget controlling digital pin 4 (or whichever you want to use):

if (digitalRead (4) == HIGH)
{
digitalWrite(blindsOpenRelayPin, LOW); // activate relay
timerV1.setTimeout(500, deactivateRelayV1);
digitalWrite(blindsOpenRelayPin, LOW); // activate relay
timerV1.setTimeout(500, deactivateRelayV1);
digitalWrite(blindsOpenRelayPin, LOW); // activate relay
timerV1.setTimeout(500, deactivateRelayV1);
}

void deactivateRelayV1() {
digitalWrite(blindsOpenRelayPin, HIGH); // deactivate relay
}

but this would need to go in a simpleTimer function, and you would have to make sure it was quick enough to catch your button press, or hold the button until the process started.

not sure which would use more memory, another timer function or the BLYNK_WRITE.

Plus you would need a digital pin for each relay. In your case 10 digital pins to read, and 10 to control the relays. (if I am understanding your application correctly from the limited information).

For easy remote access.

I guess I should had said this from the beginning…
With the code shown in my first post and a button in the app, I’m pulsing a relay. My machine needs 3 pulses to get going so I click the button 3 times. Easy enough, but I thought to automate the 3 pulses and as I can see, it cannot be done with the code shown in my first post. I guess there’s no other alternative but to use the virtual pins.

If your question is, can you pulse a digital pin with the original code you posted? The answer is NO. With the original code you had posted, you can do very limited and basic tasks. Like toggle a digital pin from high to low, or read in a raw value from an analog pin.

To do more complex tasks, such as pulsing a digital pin, you will need to add more code. There are probably a few ways one could pulse a digital pin, and how it is accomplished will be determined by how you code.

Yes, clarity of the question and situation is generally helpful :wink:

This will wait for your Button Widget to be pressed. Once pressed, it will run the non-blocking pulse routine every 500ms for three times, leaving the relay activated for 100ms each time.

If you want it to activate the routine immediately at button push, then you will have to fiddle around with the placement of the digital writes and timers.

BLYNK_WRITE(V1)  // Virtual button on V1 to activate relay pulses
{
  int RLY = param.asInt();
  if (RLY == 1)
  {
    timerV1.setTimer(500, RelayON, 3);  // Pulse Relay ON routine three times in 500ms
  } 
}

void RelayON()
{
  digitalWrite(2, HIGH); // Activate relay
  timerV1.setTimer(100, RelayOFF, 1);  // RunRelay OFF routine once in 100ms
}

void RelayOFF()
{
  digitalWrite(2, LOW); // deactivate relay
}
1 Like

Got it. Thank you you all and apologize for the confusion.