2 way lighting circuit control with blynk and feedback updates

HI @wanek,

Forgot to ask you - you mentioned when pressing a button and holding it a little longer it did another function - can you detail this for us ?



the method i used it is not applicable in your case, because it depends on other functions, which are not relevant in you case. this is exactly how i done:

else if ((ambient || panic || ups) && (1000 * overrideSec < millis() - inputs[button][0] || millis() - inputs[button][0] < 0)) {
        digitalWrite(button + x, LOW);   // if (ambient or panic or ups) and (override or override roll over), turn on light
        inputs[button][0] = millis();
        mainLights[button + x] = true;   // save light state (false == off, true == on)
        delay(DEB + 15);

but this will not work for you. instead, i try to explain the basic idea how i would do:

because in the multi dimensional array “inputs” it is stored the last button press timestamp @ “inputs[button][0]” and also stored that if the button was pressed or not in the last loop cycle @ “inputs[button][1]”, one can easily monitor if a button is continuously kept pressed and for how long.

so, you should create a condition, that if a button was pressed continuously ("inputs[button][1]" is always true) for a certain amount of time (millis() - "inputs[button][0]" is > than X), do something else instead the default function in “checkbutton”.
(this would also involve some minor modification of the “checkButton()” function.

hope this makes sense :wink:

@wanek: thanks for your advice that I am sure you have experienced much on those. I highly appreciate for those. Actually from my end, it happened sometime so let me revise my code to test on 12F that I may be coming back for further. Yes even very less cases it is still the issue for the customer side or it should be the thing to be improved.

In fact if you use a latching switch(with 5v) rather than a momentary button you get zero bounce :slight_smile:
you just toggle it from one way to the other so it either pulle the pin high to 5v or switches to low ground



i don’t think so… bouncing happens with all mechanical switches, it doesn’t matter if it is a push button or switch, etc.

“Bouncing is the tendency of any two metal contacts in an electronic device to generate multiple signals as the contacts close or open; debouncing is any kind of hardware device or software that ensures that only a single signal will be acted upon for a single opening or closing of a contact.”

if you really want to understand what is happening, read this.

1 Like

Hi @newdos, check the amazing code from @Jamin, you will find there the “hold” funtion you are looking for.


1 Like

I’ll summerize the code as its quite handy.

int longHoldTimer;

    int pressTime = millis();
    longHoldTimer = timer.setTimeout(1000,longHoldCallback); // start a timer as long as the button is HIGH
  } else {
    if((millis() - pressTime) < 999){
       // do short press task
    timer.disable(longHoldTimer); // if the button is let go, then stop the 1 second timer.

void longHoldCallback(){
  // do an action here after holding the button for 1 second
1 Like

but this will execute BOTH tasks when button is long pressed?
i mean first will execute the // do a task when the button is pressed like normal line, and after the 1 sec timeout will run longHoldCallback()?

Yup the idea would be put a trivial task on the short press of the button so that it can be actioned too.

Any ideas around this and I’d love to hear them! :smiley:

My use case was, short press stop/start a timer, and long press was reset the timer to 0. So it wasn’t an issue.

well, intuitively it should take one action:
if button is short pressed do this
if button is long pressed do that
this is how it works on every phone or device.

i would measure the time between if(param.asInt(), and if(!param.asInt()

if the measured time is lower than xxxx millis, it is a short click:
invoke void shortClick()

if millis longer than xxxx millis, invoke void longClick()


Updated the code… great idea! I think it will work! :smiley:

The action for the short press happens on the FALLING side which is the only issue really.

i see 2 problems:

  • int pressTime should be unsigned long
  • the else case will continuously be executed after releasing the button, while millis() - pressTime) < 999
1 Like

yes, but this is how things work. otherwise it can not decide if it is a long press or short, it has to wait to see :slight_smile: check on your phone keyboard. until you not release a button, it will not write a letter.

I have to head to work but please update the code with changes to the issues you listed. I think snippets of code like this will be very benefitial to new Blynkers trying to work out SimpleTimer :slight_smile:

If you want to help keep this thread clean, PM me the code and ill update my post :slight_smile:

yes but when it is latching in that state surely it is irrelevant ???



as i said in my earlier post, i do not think so. but you can try and see, that is the best you can do.

I have tried it @Wanek and it works brilliantly!!! I guess its because as the switch makes it remains in that state until you toggle it the other way

Cheers Kev

probably. i never used these kind of switches so far, so i do not have any experience on these. but glad it works :wink:

Yeah sure does very well and probs because of this explanation in your attachment. If the switch is moving from one state to another and staying there it becomes irrelevant as he describes here …

because of this if I can manage to use my existing two way wiring at the wall switch, back to the arduino, I will re-wire it all to use 5v logic the switch then just hard toggles the data pin to either logic 1 or 0 and the blynk app does the same.

So I guess a latching switch is the solution unless you need to use a momentary one for other functions ie long hold etc



the inconvenience i see with this, is that unlike buttons, switches can stay in 2 positions, and you never know when it is “on” or “off” the light. this can be surprise for a person who is not get used with the system :slight_smile: