-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbutton_sketch.h
51 lines (44 loc) · 1.21 KB
/
button_sketch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Push Button Sketch
//
#define PUSH_BUTTION_PIN 1
unsigned long lastDebounceTime;
int lastState = -1;
int state = -1;
extern int currentGifPlayed;
extern volatile bool gifChangeRequested;
void buttonSketch(void *parameter);
bool ButtonPressed();
void buttonSketch(void *parameter)
{
pinMode(PUSH_BUTTION_PIN, INPUT_PULLUP);
lastDebounceTime = millis();
for (;;)
{
if (ButtonPressed())
{
Serial.println("Button Pressed, switching to the next GIF");
currentGifPlayed = (currentGifPlayed + 1) % GIF_COUNT; // Update the GIF index
gifChangeRequested = true; // Signal screenTask
}
delay(5); // Small delay for debouncing
}
}
bool ButtonPressed()
{
int currentState = digitalRead(PUSH_BUTTION_PIN);
if (currentState != lastState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > 1)
{
if (currentState != state)
{
state = currentState;
lastState = currentState;
return state == LOW; // Returns true if the button is pressed
}
}
lastState = currentState;
return false; // No change in state
}