Start a new topic

Button Double click

hi everyone I am trying to make a button that does something with a fast double click and if there is only one click it does another thing. Any help? Thanks

Using a numeric variable called click_counter

Using a timer component called tm0


In your timer event

  tm0.en=0

  if(click_counter.val==1)

  {

     click_counter.val=0

     // code below for single


  }

  if(click_counter.val==2)

  {

      click_counter.val=0


      // code below for double

  }


In Button Press

  if(tm0.en=0)

  {

    tm0.en=1

  }

  click_counter.val++


Start with a timer

  tm0.tim=400

  tm0.en=0


a fast double click is subjective

If 400ms is your definition of a fast double click

 - otherwise adjust tm0.tim as needed

Hi Patrick Thank you for the suggestion it works pretty well but I forgot to mention that I wanted to control some relays with my arduino. So for example, I have 3 buttons and 3 relays. When button 1 is pressed once, relay 1 is activated, when button 2 is pressed once, relay 2 is also activated (relay 1 will stay activated) and for exemple when I double click on button 3 relay 3 will be activated and both relay 1 and 2 will be desactivated. I have it working with each button pressed once but I cannot get it to work withnthe double click. Any idea from your side? Thank you so much for your help.

So Either 400ms is way too fast or your not laying your logic out correctly


Trial 1:  Set tm0.tim=2000

  - check if it is now working


Trial 2: Layout your logic clearly

  - Button 1

      - press single

      - press double

  - Button 2

      - press single

      - press double

  - Button 3

      - press single

      - press double

GM Patrick


what I meant was, how to control the double click form the arduino? because the code you gave me earlier works on the nextion display but how does the arduino know when I double clicked on the touch?


Filipe



here is part of the code from my arduino for a button called m_40m_3elemUSA    


pressLength_3el_40m is the time between the first time I touched the screen and the second and timing_40m is a fixed variable with 1000 sec, for example.  

    if (button_40m_4elem == 1 && button_40m_2elem == 1 && button_40m_3elem == 0 && choose_3el==1) //--------> A1+A2 long press A3
       {
       digitalWrite(_40m_A1, RELAY_OFF); 
       digitalWrite(_40m_A2, RELAY_OFF); 
       digitalWrite(_40m_A3, RELAY_ON); 
       digitalWrite(_40m_stack, RELAY_ON); 
       myNextion.sendCommand("_40.bt10.val=0");
       myNextion.sendCommand("_40.bt11.val=0");
       myNextion.sendCommand("_40.bt12.val=1");
       myNext.sendCommand("_40.bt10.val=0");
       myNext.sendCommand("_40.bt11.val=0");
       myNext.sendCommand("_40.bt12.val=1");
       button_40m_3elem = 1;
       button_40m_4elem--;
       button_40m_2elem--;
       choose_3el=0;
       pressLength_4el_40m=0;
       pressLength_2el_40m=0;
       pressLength_3el_40m=0;
       }
           

 then I have some IFs to make exactly what I want to my relays, for exemple, bellow when button A1 == 1, button 2 == 1 and if I press twice on button 3 it should activate relay 3 and desactive the other 2.

      if (strcmp_P(rx_message_A,m_40m_3elemUSA)==0 || strcmp_P(rx_message_B,m_40m_3elemUSA)==0) //---------> 40M 3 ELEMENT USA
      {
          if ((button_40m_3elem % 2) == 0)
           {
           button_40m_3elem ++;
           on_3ele=millis();
           pressLength_3el_40m = off_3ele - on_3ele;
           }
                else if ((button_40m_3elem % 2) != 0)
                {
                button_40m_3elem --;
                off_3ele=millis();
                pressLength_3el_40m = off_3ele - on_3ele;
                }
      }
      if((button_40m_3elem % 2) == 0 && (pressLength_3el_40m > 0 && pressLength_3el_40m < timing_40m) || 
        (pressLength_3el_40m < 0 && pressLength_3el_40m > -timing_40m))
      {
        choose_3el=1;
      }
          else if((button_40m_3elem % 2) != 0 && (pressLength_3el_40m > timing_40m || pressLength_3el_40m < -timing_40m))
          {
            choose_3el=0;
          }    

    

Sorry to bother you with this arduino code, I just wanted to know if there is an easier way to tell the arduino I clicked 2 times on the touch so do this now...


So that the Arduino knows the (which) button is pressed (double pressed)

you would need to add a printh command to your timing event

Your code would catch this through a NexListen - that triggers your routine to handle


If I were to add in the timer event assuming there is no page 128

printh 65 80 01 00 FF FF FF   - this could be caught by Arduino


call it a custom way of catching custom prints so that nexListen can react




Hi,


Thanks for the reply.


I have been using this function to listen when something is pressed on the touch: 

  myNextion.listen().toCharArray(rx_message_A, 25);

 Are we talking about the same NexListen? 


So you are saying that I can do this: printh 65 80 01 00 FF FF FF 

If yes, I had no idea. I will test it right away.

Which library Filipe?

Sorry, I am using this one:


https://github.com/bborncr/nextion


works pretty well for me, I have done several projects with it.



In the IteadLib Arduino Nextion Library

 

 

#include "Nextion.h"

NexHotspot myCustom1  = NexHotspot(128,1,"doubleclick");
NexTouch *nex_listen_list[] = 
{
    &myCustom1,
    NULL
};

void doubleclickCallback(void *ptr) {
  // code when custom is received
}

void setup(void) {
  nexInit();
  myCustom1.attachPop(doubleclickCallback);
}

void loop(void) {
  nexLoop(nex_listen_list);
}

 

 

The printh 65 80 01 00 FF FF FF simulates a page 128 item 1 release event

  (which there is no such page-component, we are merely borrowing it for the catch)

  (the myCustom1 Hotspot also doesn't exist and I am not issuing anything hotspot related)

But this allows out doubeclickCallback routine to be triggered when the timer goes off


By having a custom code per logic result, custom routines trigger based on result

Does this mean that the printh 65 80 01 00 FF FF FF will not work with the library I am using?


I am away in Paris until Friday so will only be able to test it by then with my arduino. But on the debbuging window of the NEXTION editor, the printh is working ok, I see it on the simulator return data, I hope the arduino gets this HEX code with the double click.


image


Looks good ... if it is sending (it is)

then Arduino will get it, it must only now react upon it.


1 person likes this

I can't guess on how your library handles it

but if it is the one I think then something like

  if (message == '65 80 1 0 ffff ffff ffff') {

  }


What is the link to your lib

The reaction upon getting the code from the Nextion is already coded, hope it will work.


Thank you soo much for the help :)



Login or Signup to post a comment