How do you differentiate if the button is pressed once, light 1 is on and pressed twice, light 2 is on, then if you press it once, light 1 is off and pressed twice, light 2 is off, similar to a smartphone when you tap it twice, the screen is on?
Hello bimosora
You might use the millis() function for time measurement.
you have a finite state machine with 4 states:
- if the button is pressed once, light 1 is on --> 1 0
- and pressed twice, light 2 is on, --> 1 1
- then if you press it once, light 1 is off --> 0 1
- and pressed twice, light 2 is off, --> 0 0
so make a counter which counts from 0 to 3
every time you press the button, increase the counter and handle your outputs according to the state.
Is it like this?
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
if(digitalRead(10))
{
currentMillis = millis();
if(currentMillis - previousMillis >= 200 && !digitalRead(10))
{
//oneclick
previousMillis = currentMillis;
bool firstClick = true;
}else if(currentMillis - previousMillis >= 200 && digitalRead(10) && firstclick)
{
//DoubleClick
previousMillis = currentMillis;
firstClick = false;
}else if(currentMillis - previousMillis >= 2000 && digitalRead(10))
{
//long click (hold for 2000 ms or 2s)
previousMillis = currentMillis;
}
firstClick = false;
}
Iād suggest you check the code for one of the numerous button library handling double click such as OneButton
Matthias has taken the time to write some notes about his implementation and he explains the state machine he is using
I won't use the library because later I will try single tap and double tap code using RFID
the point was studying the code, not using the library if you don't want to. That will give you an understanding about Finite state machines (Here is a small introduction to the topic: Yet another Finite State Machine introduction)
thats something different.
It seems you want something like:
single click --> toggle Output 1
double click --> toogle Output 2
so the question is, what system behaviour you really want...
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.