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..