Button ,Timer

Hi
How to use a same button as on off and timer to control.

Could you please explain better and in more details?

What I mean is a button created for on and off can be used as timer to on and off( two way function)

There is a Timer Widget for this

sat,

Do you mean a button which overrides the timer. Like a timer and manual switch for a lawn sprinkler?

R

hi I want to have a timer and a button to control a switch, both way I want to control a switch.

@rapgar I think that’s it. I made my living room lights go on and off, but I’d like to time it as well with an option to override.

I think you gonna have to use virtual pins for this, but I haven’t been able to spend some time figuring it out for now.

This sample code might get you where you want to go. It is set up run a 4 zone sprinkler system and includes an enable button.

    #define BLYNK_PRINT Serial // Enables Serial Monitor
    #include <SPI.h>
    #include <Ethernet.h>
    #include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
    
    char auth[] = "XX"; // Put your Auth Token here. (see Step 3 above)
    
      int enableZone1 = LOW;
      int timerZone1 = LOW;
      int manualZone1 = LOW;
    
    
    void setup()
    {
      Serial.begin(9600); // See the connection status in Serial Monitor
      Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
      
      //  Set all Arduino outputs to HIGH in respect to Relay Board
      pinMode(2, OUTPUT); // Zone 1
      digitalWrite(2, HIGH);
    }
    
    void loop()   // run over and over again
    {
      Blynk.run(); // All the Blynk Magic happens here...
      zone1();
    }
    
    //  manage Sprinkler Zone1
    void zone1()  {
      if  (((enableZone1 == HIGH) && (timerZone1 == HIGH)) || (manualZone1 == HIGH)) {
        digitalWrite(2, LOW); }
      else  {
        digitalWrite(2, HIGH);  }  
    }

Here’s the logic Lichtsignaal. Be sure to declare all variables above ‘void setup’ so they are handled globally.

Your logic is flawless I think, but I’m running into another issue. I’ll be using the timer widget, so I need to enable or disable that and I’m not sure how to accomplish that yet.

In my example I got the following setup:

  • 2x lamps attached to 2 different relays
  • 1x timer to enable both lamps (from, let’s say, 8pm to 11pm)
  • 3x buttons
    • 1 button to enable the timer
    • 1 button for each lamp individually (the override, for when it’s really rainy and I need to turn on the lamp earlier)

I tried to code it, but am at work right now so I can’t test it, but in general, this is what I got( showing relevant parts only):

BLYNK_WRITE(3)  // Timer enable/disable
{
  int setting = param.asInt();
  enableTimer = setting;
}

BLYNK_WRITE(4)  // Actual timer to vPin
{
  int setting = param.asInt();
  timerLights = setting;
}

BLYNK_WRITE(5)  // Manual override for Tree
{
  int setting = param.asInt();
  manualTree = setting;
}

BLYNK_WRITE(6)  // Manual override for Lamp
{
  int setting = param.asInt();
  manualLamp = setting;
}

void loop()
{
  Blynk.run();
  checkTimer();
}

void checkTimer()
{
  if(enableTimer) // If this is true, then check if it is timer-time (timerLights param)
  {
    switch(timerLights)
    {
      case 0:
        // Timer is running, so lights on!
        digitalWrite(2, HIGH);
        digitalWrite(4, HIGH);
      break;
      case 1: // Timer is off, so lights off, unless the manual override is on for either lamp
        switch(manualTree)
        {
          case 0:
            // Tree overridden, turn on
            digitalWrite(2, HIGH);
          break;
          case 1:
            // Tree via Timer on off mode
            digitalWrite(2, LOW);
          break;
        }
        
        switch(manualLamp)
        {
          case 0:
            // Lamp overridden, turn on
            digitalWrite(4, HIGH);
          break;
          case 1:
            // Lamp via Timer
            digitalWrite(4, LOW);
          break;
        }
      break;  
    }
  }   
}

However, since I’m not really turning on or off the timer-widget, I’m afraid the relays will keep clicking on and off because of the loop, but then again, I haven’t tried it with my actual setup yet.

  • edit

I figured out you can add the timer widget for a virtual pin too, duh :smiley:

  • edit2

Alternate code added

Seems like you go it with the virtual pin on the timer. How’d it go?

R

I picked up a new MacBook yesterday so I haven’t put any time in it yet. Tonight will be difficult too (modeltrain hobbies, lol). I’ll try and look at it friday night. I may even throw in a nice tutorial if it all works :slight_smile:

Hi I have been using rf switch with blynk and it is working well, how do I use timer for the same,since I am using virtual
button to control. please find below my code .how do I modify to use timer along with manual.

.#include Ethernet.h>
.#include RCSwitch.h>
.#include stdio.h>
.#include <BlynkSimpleSerial.h>
RCSwitch mySwitch = RCSwitch();
char auth[] = “SOME CHARACTERS HERE”;

void setup() {
mySwitch.enableTransmit(7);
mySwitch.setPulseLength(321);
Serial.begin(9600);
Blynk.begin(auth);
}

void loop() {
Blynk.run();
}

BLYNK_WRITE(0) {
mySwitch.send(“100000101100100011001000”);
}

BLYNK_WRITE(1) {
mySwitch.send(“100000101100100011000010”);
}

Take a look at my code above. Basically, the principle is sound. I’ve tried it for a bit and it didn’t really went as expected, so it needs a little tweaking.

In essence, you tie all your buttons and timers to virtual pins and check those in the main loop if the settings change. That way you can make a program flow and logic to do stuff. E.g. check if timer enable button is set to on AND if the timer is running (also 0 or 1) and act accordingly.

-edit- Nevermind the above code, I forgot I already changed some stuff, take a look at this:

bool enableTimer = true;  // Defaults to enable timer (button on vPin 3)
bool timerLights = true;  // Defaults to timer is running (timer widget on vPin 4)

bool manualTree = false;  // Tree lamp thingie override (right of couch), default via timer (button on vPin 5)
bool manualLamp = false;  // Normal lamp override (left of couch), default via timer (button on vPin 6)

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, server_ip, 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  
  while (!Blynk.connect()) { // Wait until connected
  }
}

BLYNK_WRITE(3)  // Timer enable/disable
{
  int setting = param.asInt();
  enableTimer = setting;
  Serial.println(enableTimer);
}

BLYNK_READ(3) // Try and set the button according to actual on/off status (defaults to on)
{
  Blynk.virtualWrite(3, enableTimer);
}

BLYNK_WRITE(4)  // Actual timer to vPin
{
  int setting = param.asInt();
  timerLights = setting;
  Serial.println(timerLights);
}

BLYNK_WRITE(5)  // Manual override for Tree
{
  int setting = param.asInt();
  manualTree = setting;
  Serial.println(manualTree);
}

BLYNK_WRITE(6)  // Manual override for Lamp
{
  int setting = param.asInt();
  manualLamp = setting;
  Serial.println(manualLamp);
}

void loop()
{
  Blynk.run();
  checkTimer();
}

void checkTimer()
{
  // If timer is on and timer is running, turn lights on
  if(((enableTimer == 1) && (timerLights == 1)) || (manualTree == 1))
  {
    // Timer is running, so lights on!
    digitalWrite(A1, HIGH);
  }
  else
  {
    digitalWrite(A1, LOW);
  }

  if(((enableTimer == 1) && (timerLights == 1)) || (manualLamp == 1))
  {
    // Timer is running, so lights on!
    digitalWrite(A0, HIGH);
  }
  else
  {
    digitalWrite(A0, LOW);
  }
}

Hi thank you for your advice.Let me try it.

I’ve written a little blog for this thing over here:

http://lichtsignaal.nl/weblog/?p=102

It has updated code and works like charm over here on my Nano :slight_smile:

Unfortunately it stopped working like a charm, it stopped working at all.

The code from my blog has two little changes, I start with enableTimer and timerLights set to 1. In case of a reset it automatically starts the timer again, however, it doesn’t appear to be working. It’s almost as if the timer doesn’t start running. We got home today a little after the lights should have been on, but they weren’t on. After I stopped/started the V3 button (timer enable) the lights went on, which is good.

However, after I changed the timer to a time in the past to see if they would go out, they didn’t. I’m not sure how to debug this further. Anyone have any ideas about that? When coding the thing I had some serial output which seemed OK, but I think I’m making some sort of logic error somewhere…

-editting-

I’m running from the cloud now instead of local server, but no difference. After some fiddling I see that my timer doesn’t get activated. It is bound to vPin4, but it doesn’t go HIGH when the time arrives (I’ve set it from 00.00 to 23.59.59, so it should be high all the time).

Do timers work with virtual pins? It has worked before for me with local server 0.7.4 and library 0.2.5 beta. I’m on 0.3.0 library now (Arduino Nano) using ENCJ ethernet and connecting to cloud.

Were you ever able to get timers and buttons working to control the same device? I have a similar project right now and what I thought would be simple is turning out to be a mind boggler.

This is a very old topic. Please create your own current topic and supply full details so you can be better assisted. Thank you.