alextaujenis / RBD_Button

Arduino Button Library - Read and debounce buttons and switches without delay or interrupts.
https://robotsbigdata.com/docs-arduino-button.html
MIT License
33 stars 9 forks source link

delayon #15

Closed aaronik19Europe closed 1 year ago

aaronik19Europe commented 1 year ago

Dear Alex, first I want to thank you for this great library...both RBD button and RBD Timer. I have a question and this might concern both libraries. I would like to create an Cyclic sequence timer (pulsing...200ms on 400ms off) with RBD timer but when the RBD button is pressed. How can i combine both libraries together? For the cyclic timer i have to use two separate timer, one for the on delay and one for off. can you give me an idea?

alextaujenis commented 1 year ago

Hello @aaronik19Europe,

Sorry it took so long to respond. I noticed the example sketches at https://robotsbigdata.com/docs-arduino-button.html were not showing and had to fix this first.

Just to clarify; you would like a cyclical sequence timer that can be turned on when a button is pressed, and turned off when the button is released. The timer should enable a pulse for 200ms, then disable the pulse for 400ms.

Here's an example sketch. You can add your PULSE ON and PULSE OFF code inside of the pulseOn() and pulseOff() functions at the bottom. Let me know if this is what you are looking for...

#include <RBD_Timer.h>
#include <RBD_Button.h>

RBD::Button button(2);
RBD::Timer timer200(200);
RBD::Timer timer400(400);

void setup() {
  Serial.begin(115200);
}

void loop() {
  if(button.onPressed()) {
    timer200.restart();
  }
  if(button.onReleased()) {
    timer200.stop();
    timer400.stop();
    pulseOff();
  }
  if(timer200.onActive()) {
    pulseOn();
  }
  if(timer200.onExpired()) {
    pulseOff();
    timer400.restart();
  }
  if(timer400.onExpired()) {
    timer200.restart();
  }
}

void pulseOn() {
  Serial.println("ON - waiting 200ms");
}

void pulseOff() {
  Serial.println("OFF - waiting 400ms");
}
aaronik19Europe commented 1 year ago

Thanks for your help. I tried the sketch and worked perfectly. Last issue which I think it might be useful to others.

Let assume we have a function

void longtimer1{ code... }

Now assume that we have multiple timers and we need to apply the same functions to different timers, how can I change the constructor in the program, from longtimer1 to longtimer2 for example?

On Sun, 12 Nov 2023, 20:59 Alex Taujenis, @.***> wrote:

Hello @aaronik19Europe https://github.com/aaronik19Europe,

Sorry it took so long to respond. I noticed the example sketches at https://robotsbigdata.com/docs-arduino-button.html were not showing and had to fix this first.

Just to clarify; you would like a cyclical sequence timer that can be turned on when a button is pressed, and turned off when the button is released. The timer should enable a pulse for 200ms, then disable the pulse for 400ms. Is this a battery charger?

Here's an example sketch. You can add your PULSE ON and PULSE OFF code inside of the pulseOn() and pulseOff() functions at the bottom. Let me know if this is what you are looking for...

include

include

RBD::Button button(2); RBD::Timer timer200(200); RBD::Timer timer400(400); void setup() { Serial.begin(115200); } void loop() { if(button.onPressed()) { timer200.restart(); } if(button.onReleased()) { timer200.stop(); timer400.stop(); } if(timer200.onActive()) { pulseOn(); } if(timer200.onExpired()) { pulseOff(); timer400.restart(); } if(timer400.onExpired()) { timer200.restart(); } } void pulseOn() { Serial.println("ON - waiting 200ms"); } void pulseOff() { Serial.println("OFF - waiting 400ms"); }

— Reply to this email directly, view it on GitHub https://github.com/alextaujenis/RBD_Button/issues/15#issuecomment-1807223886, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD6BLZDQLORB6ZCQXLBIMOTYEETCZAVCNFSM6AAAAAA7B3STN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMBXGIZDGOBYGY . You are receiving this because you were mentioned.Message ID: @.***>

aaronik19Europe commented 1 year ago

No it was not a battery charger, a signal blinker and change frequency accordingly. It worked perfectly.

alextaujenis commented 1 year ago

Now assume that we have multiple timers and we need to apply the same functions to different timers, how can I change the constructor in the program, from longtimer1 to longtimer2 for example?

I'm not sure what you are asking, maybe learning a little bit about how functions behave could help? https://cplusplus.com/doc/tutorial/functions/

aaronik19Europe commented 1 year ago

Maybe I did not explained well myself. Sorry for that. I have 3 relays on teh board, and every relay has 3 presets timers each such as (Off Delay, Cyclic timer). Now according to the choose of the user, when the button of the relay is pressed, the particular timer is executed. That why I need to change the number of the constructor. I will pass all the data through function, but seems that I can't change the constructor ID example from RBD::Timer timer1; to RBD::Timer timer2;

Hope that I explained myself better

alextaujenis commented 1 year ago

There was an issue with the original sketch and I updated the example above to include an extra pulseOff() when the button is released:


if(button.onReleased()) {
  timer200.stop();
  timer400.stop();
  pulseOff();
}
alextaujenis commented 1 year ago

I have 3 relays on teh board, and every relay has 3 presets timers each such as (Off Delay, Cyclic timer). Now according to the choose of the user...

There are many ways to structure your program and duplicate this logic across multiple relays/buttons. You could just copy/paste the code, or abstract part of it into a function with parameters, or you could create your own object like a CycleTimer(2,200,400) that would attach to your button and encapsulate the logic for the different cycles.

I've provided the logic on how to get a single cycle timer functioning... I'm going to leave it up to you to figure out how to finish your sketch.