1 (edited by DougLeary 2022-05-16 09:24:02)

Topic: Associate UI element with a function instead of a pin

Besides controlling a hardware pin a UI element could also trigger function calls.

For example, selecting a pin in the "Snap to Pin" dropdown for button_1 generates code:

    digitalWrite(PIN_BUTTON_1, (RemoteXY.button_1==0)?LOW:HIGH);

If "Snap to Pin" also had a Function Name option, you could enter "myFunction" and it would generate code:

    if (RemoteXY.button_1==1) { myFunction(); }   // call myFunction if button_1 is pressed

2

Re: Associate UI element with a function instead of a pin

This is a good idea. For beginners. We think about calling a function when the variable changes to any value. There will be something like events.

3

Re: Associate UI element with a function instead of a pin

DougLeary wrote:

Besides controlling a hardware pin a UI element could also trigger function calls.

For example, selecting a pin in the "Snap to Pin" dropdown for button_1 generates code:

    digitalWrite(PIN_BUTTON_1, (RemoteXY.button_1==0)?LOW:HIGH);

If "Snap to Pin" also had a Function Name option, you could enter "myFunction" and it would generate code:

    if (RemoteXY.button_1==1) { myFunction(); }   // call myFunction if button_1 is pressed

A potential problem here is that your function will be called every iteration of void loop() while the button is held down. This may not be desirable.

You can avoid this by generating a "one-shot" and only calling your function once, but while coding that it's no big sweat to read the RemoteXY button variable as is, and using the one-shot code, call the function once only.

Here I'm using a button on the GUI that has to be held down for 5 seconds in order to reset my "hours_run" timings ....

 // reset button code
  if(RemoteXY.hours_reset == 1)
  {
    if(reset_ons == false) 
    {
      reset_start_time = millis();          // log the start time
      reset_ons = true;                     // set the one-shot
    } // if(reset_ons = false)

    if(millis() > reset_start_time + 5000)  // wait elapsed time
    {
      hours_run = 0;
      EEData.hours_run = hours_run;
      minutes_run = 0;
      EEData.minutes_run = minutes_run;
      seconds_run = 0;
      EEDataSAVE();
    } // if(millis() > reset_start_time + 3000)
  } // if(RemoteXY.hours_reset == 1)

  if(RemoteXY.hours_reset == 0) reset_ons = false;            // button released
2B, or not 2B, that is the pencil ...

4

Re: Associate UI element with a function instead of a pin

Along a similar line, I think it would be useful to have a one-shot option in the UI, so that only changes of state are transmitted wirelessly.    In order to implement an adjustment feature using up and down arrows, for instance, I had to "debounce" the variable coming back from RemoteXY to get just one increment per button touch rather than a string of them (like an autorepeat).     It would simplify some user interfaces if a button would only generate one change of state indication until it is released again.

Bob

5

Re: Associate UI element with a function instead of a pin

w9ran wrote:

Along a similar line, I think it would be useful to have a one-shot option in the UI, so that only changes of state are transmitted wirelessly.    In order to implement an adjustment feature using up and down arrows, for instance, I had to "debounce" the variable coming back from RemoteXY to get just one increment per button touch rather than a string of them (like an autorepeat).    It would simplify some user interfaces if a button would only generate one change of state indication until it is released again.

Bob

I'm not sure if this would be the best way forward Bob.

It would "fix" a button to be either COS or Continuous, but not both. I reckon some buttons you might want to use both methods in your sketch.

I'm sure there's a comprehensive library for dealing with button handling, I'll see if I can find one.  Many devices use a single button for a single increment (or decrement) of a value, for instance, but if "held" for a time the value gets incremented continuously, at a certain rate, which often also accelerates the longer you hold it.

I know I've used a "Debounce" function in the past that tells you which button is just pressed, and has the ability to tell you if a button is "held".

IMHO there'll be too many variable "features" you may require of a button for these to be handled in the UI, this sort of jiggery-pokery best done in the sketch, and the UI just says "button down/button up" as it does now.

EDIT : here's what comes up when you google "arduino button library" ....

https://www.arduino.cc/reference/en/libraries/button/
https://www.arduino.cc/reference/en/lib … n-arduino/
https://www.arduino.cc/reference/en/libraries/ezbutton/

There's loads more, and I'm sure at least one of them will suit your needs ...

2B, or not 2B, that is the pencil ...