implementare ClickButton.h

salve a tutti,
attualmente ho un bottone che se spingo rapidamente mi cambia lo stato di una boolean, e se tengo premuto a lungo mi fa partire una funzione.

sempre con lo stesso tasto vorrei anche poter controllare la luminosita del display (alta e bassa) da qui la ricerca di una funzione e/o libreria che mi dia la possibilita di click, doppiclick, tripliclick, click lunghi... etc senza bloccare il codice.

sto provando ora ClickButton.h modificata per il mio bottone che ho collegato così

/* ClickButton library demo

  Blinks a LED according to different clicks on one button.
  
  Short clicks:

    Single click - Toggle LED on/off
    Double click - Blink      (Toggles LED 2 times/second)
    Triple click - Fast blink (Toggles LED 5 times/second)
    
  Long clicks (hold button for one second or longer on last click):
    
    Single-click - Slow blink   (Toggles LED every second)
    Double-click - Sloow blink  (Toggles LED every other second)
    Triple-click - Slooow blink (Toggles LED every three seconds)


  The circuit:
  - LED attached from pin 10 to resistor (say 220-ish ohms), other side of resistor to GND (ground)
  - pushbutton attached from pin 4 to GND
  No pullup resistor needed, using the Arduino's (Atmega's) internal pullup resistor in this example.

  Based on the Arduino Debounce example.

  2010, 2013 raron
 
 GNU GPLv3 license
*/

#include "ClickButton.h"

// the Button
const int buttonPin1 = 2;
//ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
ClickButton button1(buttonPin1, HIGH, CLICKBTN_PULLUP);

// Arbitrary LED function 
int LEDfunction = 0;


void setup()
{
  Serial.begin(115200);
  delay(1000);

  // Setup button timers (all in milliseconds / ms)
  // (These are default if not set, but changeable for convenience)
  button1.debounceTime   = 20;   // Debounce timer in ms
  button1.multiclickTime = 250;  // Time limit for multi clicks
  button1.longClickTime  = 1000; // time until "held-down clicks" register
}


void loop()
{
      //Serial.println("suka");
  // Update button state
  button1.Update();

  // Save click codes in LEDfunction, as click codes are reset at next Update()
  if (button1.clicks != 0) LEDfunction = button1.clicks;
  

  // Simply toggle LED on single clicks
  // (Cant use LEDfunction like the others here,
  //  as it would toggle on and off all the time)
  if(button1.clicks == 1) {
    Serial.println("click");
    //LEDfunction = 0;
  }

  // blink faster if double clicked
  if(LEDfunction == 2) {
    Serial.println("double click");
    LEDfunction = 0;
  }

  // blink even faster if triple clicked
  if(LEDfunction == 3) {
    Serial.println("triple click");
    LEDfunction = 0;
  }

  // slow blink (must hold down button. 1 second long blinks)
  if(LEDfunction == -1) {
    Serial.println("long click");
    LEDfunction = 0;
  }

  // slower blink (must hold down button. 2 second loong blinks)
  if(LEDfunction == -2) {
    Serial.println("long long click");
    LEDfunction = 0;
  }

  // even slower blink (must hold down button. 3 second looong blinks)
  if(LEDfunction == -3) {
    Serial.println("long long long click");
    LEDfunction = 0;
  }


}

che mi scrive nel serial monitor click, double click, triple click, long click, long long click e long long long click a seconda di quello che faccio al pulsante. fin qui tutto bene.

il problema sussiste quando oltre a controllare lo stato del pulsante, nel loop faccio girare un comando qualsiasi ..anche un semplice serial.println ("qualche cosa"), non fa funzionare il tutto:

questo il codice che uso con il pulsante a doppia funzione (click rapido e click lungo):

  if (digitalRead(button) == HIGH) {

    if (buttonActive == false) {

      buttonActive = true;
      buttonTimer = millis();
    }

    if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {

      cal();
    }

  } else {

    if (buttonActive == true) {

      if (longPressActive == true) {

        longPressActive = false;

      } else {

        StateDue = !StateDue;
          unit ();
           delay(1000);
      }

      buttonActive = false;

    }

  }


  if (StateDue == 1) {
  
  cmprint ();
    
  } else {
  
  feetprint ();
  }


}//--(end main loop )---

suggerimenti potabili??

grazie in anticipo..

.. aspettando qualche consiglio continuo a fare prove ... con il seguente codice riesco a far cambiare lo stato di tre boolean con uno, due o tre click e tutto cio è geniale!!!

/* ClickButton library demo

  Blinks a LED according to different clicks on one button.

  Short clicks:

    Single click - Toggle LED on/off
    Double click - Blink      (Toggles LED 2 times/second)
    Triple click - Fast blink (Toggles LED 5 times/second)

  Long clicks (hold button for one second or longer on last click):

    Single-click - Slow blink   (Toggles LED every second)
    Double-click - Sloow blink  (Toggles LED every other second)
    Triple-click - Slooow blink (Toggles LED every three seconds)


  The circuit:
  - LED attached from pin 10 to resistor (say 220-ish ohms), other side of resistor to GND (ground)
  - pushbutton attached from pin 4 to GND
  No pullup resistor needed, using the Arduino's (Atmega's) internal pullup resistor in this example.

  Based on the Arduino Debounce example.

  2010, 2013 raron

  GNU GPLv3 license
*/

#include "ClickButton.h"

// the Button
const int buttonPin1 = 2;
boolean StateUno = false;
boolean StateDue = false;
boolean StateTre = false;
boolean StateQuattro = false;
ClickButton button1(buttonPin1, HIGH, CLICKBTN_PULLUP);


int BUTfunction = 0;

#include <NewPing.h>
#define trigger_Port 7
#define echo_Port 8
//long cm, INCHES, feet, inches;
unsigned int durata;
unsigned int durataCal;
NewPing sonar(trigger_Port, echo_Port);


void setup()
{
  Serial.begin(115200);
  delay(1000);

  // Setup button timers (all in milliseconds / ms)
  // (These are default if not set, but changeable for convenience)
  button1.debounceTime   = 20;   // Debounce timer in ms
  button1.multiclickTime = 250;  // Time limit for multi clicks
  button1.longClickTime  = 1000; // time until "held-down clicks" register
}


void loop()
{

  
  Serial.print("StateUno = ");
  Serial.println(StateUno);
  Serial.print("StateDue = ");
  Serial.println(StateDue);
  Serial.print("StateTre = ");
  Serial.println(StateTre);

  // Update button state
  button1.Update();

  // Save click codes in LEDfunction, as click codes are reset at next Update()
  if (button1.clicks != 0) BUTfunction = button1.clicks;

  //durata = sonar.ping_median(15);
  // Simply toggle LED on single clicks
  // (Cant use LEDfunction like the others here,
  //  as it would toggle on and off all the time)
  if (button1.clicks == 1) {
    //Serial.println("click");
    StateUno = !StateUno;
    //BUTfunction = 0;
  }

  // blink faster if double clicked
  if (BUTfunction == 2) {
    //Serial.println("double click");
    StateDue = !StateDue;
    BUTfunction = 0;
  }

  // blink even faster if triple clicked
  if (BUTfunction == 3) {
    //Serial.println("triple click");
    StateTre = !StateTre;
    BUTfunction = 0;
  }

  // slow blink (must hold down button. 1 second long blinks)
  if (BUTfunction == -1) {
    Serial.println("long click");
    BUTfunction = 0;
  }

  // slower blink (must hold down button. 2 second loong blinks)
  if (BUTfunction == -2) {
    Serial.println("long long click");
    //BUTfunction = 0;
  }

  // even slower blink (must hold down button. 3 second looong blinks)
  if (BUTfunction == -3) {
    Serial.println("long long long click");
    BUTfunction = 0;
  }


}

ma quando aggiungo codice al loop, per esempio

durata = sonar.ping_median(15);

gli stati delle booleane non si cambiano piu...

/* ClickButton library demo

  Blinks a LED according to different clicks on one button.

  Short clicks:

    Single click - Toggle LED on/off
    Double click - Blink      (Toggles LED 2 times/second)
    Triple click - Fast blink (Toggles LED 5 times/second)

  Long clicks (hold button for one second or longer on last click):

    Single-click - Slow blink   (Toggles LED every second)
    Double-click - Sloow blink  (Toggles LED every other second)
    Triple-click - Slooow blink (Toggles LED every three seconds)


  The circuit:
  - LED attached from pin 10 to resistor (say 220-ish ohms), other side of resistor to GND (ground)
  - pushbutton attached from pin 4 to GND
  No pullup resistor needed, using the Arduino's (Atmega's) internal pullup resistor in this example.

  Based on the Arduino Debounce example.

  2010, 2013 raron

  GNU GPLv3 license
*/

#include "ClickButton.h"

// the Button
const int buttonPin1 = 2;
boolean StateUno = false;
boolean StateDue = false;
boolean StateTre = false;
boolean StateQuattro = false;
ClickButton button1(buttonPin1, HIGH, CLICKBTN_PULLUP);


int BUTfunction = 0;

#include <NewPing.h>
#define trigger_Port 7
#define echo_Port 8
//long cm, INCHES, feet, inches;
unsigned int durata;
unsigned int durataCal;
NewPing sonar(trigger_Port, echo_Port);


void setup()
{
  Serial.begin(115200);
  delay(1000);

  // Setup button timers (all in milliseconds / ms)
  // (These are default if not set, but changeable for convenience)
  button1.debounceTime   = 20;   // Debounce timer in ms
  button1.multiclickTime = 250;  // Time limit for multi clicks
  button1.longClickTime  = 1000; // time until "held-down clicks" register
}


void loop()
{

  
  Serial.print("StateUno = ");
  Serial.println(StateUno);
  Serial.print("StateDue = ");
  Serial.println(StateDue);
  Serial.print("StateTre = ");
  Serial.println(StateTre);

  // Update button state
  button1.Update();

  // Save click codes in LEDfunction, as click codes are reset at next Update()
  if (button1.clicks != 0) BUTfunction = button1.clicks;

  durata = sonar.ping_median(15);
  // Simply toggle LED on single clicks
  // (Cant use LEDfunction like the others here,
  //  as it would toggle on and off all the time)
  if (button1.clicks == 1) {
    //Serial.println("click");
    StateUno = !StateUno;
    //BUTfunction = 0;
  }

  // blink faster if double clicked
  if (BUTfunction == 2) {
    //Serial.println("double click");
    StateDue = !StateDue;
    BUTfunction = 0;
  }

  // blink even faster if triple clicked
  if (BUTfunction == 3) {
    //Serial.println("triple click");
    StateTre = !StateTre;
    BUTfunction = 0;
  }

  // slow blink (must hold down button. 1 second long blinks)
  if (BUTfunction == -1) {
    Serial.println("long click");
    BUTfunction = 0;
  }

  // slower blink (must hold down button. 2 second loong blinks)
  if (BUTfunction == -2) {
    Serial.println("long long click");
    //BUTfunction = 0;
  }

  // even slower blink (must hold down button. 3 second looong blinks)
  if (BUTfunction == -3) {
    Serial.println("long long long click");
    BUTfunction = 0;
  }


}

possono coesistere le due cose?

devo spargere degli button1.Update(); in giro per il codice??

grazie

Non penso sia un problema di dove metti i tuoi update() ma dal codice che aggiungi in loop() ovvero se è codice "bloccante", che magari internamente la libreria usa delle delay() o comunque comandi che aspettano qualcosa per un certo tempo

nid69ita:
Non penso sia un problema di dove metti i tuoi update() ma dal codice che aggiungi in loop() ovvero se è codice "bloccante", che magari internamente la libreria usa delle delay() o comunque comandi che aspettano qualcosa per un certo tempo

era quello che sospettavo!!!

esiste la possibilita di avere piu opzioni per un tasto solo usando nello sketch NewPing?

grazie