Skip to content

Commit 87fdd59

Browse files
committed
Only check for button pressed/released events every 10 ms (debouncing).
1 parent e3a8069 commit 87fdd59

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

examples/Controlling_Manually_Braccio/Controlling_Manually_Braccio.ino

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,39 @@ void setup()
246246
}
247247

248248
void loop()
249+
{
250+
/* Execute every 10 ms. */
251+
{
252+
static auto prev = millis();
253+
auto const now = millis();
254+
if ((now - prev) > 10)
255+
{
256+
prev = now;
257+
handle_ButtonPressedReleased();
258+
}
259+
}
260+
261+
/* Execute every 100 ms. */
262+
{
263+
static auto prev = millis();
264+
auto const now = millis();
265+
if ((now - prev) > 100)
266+
{
267+
prev = now;
268+
269+
if (Braccio.isJoystickPressed_UP())
270+
app.update(Button::Up);
271+
if (Braccio.isJoystickPressed_DOWN())
272+
app.update(Button::Down);
273+
if (Braccio.isJoystickPressed_LEFT())
274+
app.update(Button::Left);
275+
if (Braccio.isJoystickPressed_RIGHT())
276+
app.update(Button::Right);
277+
}
278+
}
279+
}
280+
281+
void handle_ButtonPressedReleased()
249282
{
250283
/* ENTER */
251284

@@ -312,23 +345,6 @@ void loop()
312345
handle_OnButtonRightReleased();
313346
}
314347
prev_joystick_pressed_right = curr_joystick_pressed_right;
315-
316-
317-
static auto prev = millis();
318-
auto const now = millis();
319-
if ((now - prev) > 100)
320-
{
321-
prev = now;
322-
323-
if (Braccio.isJoystickPressed_UP())
324-
app.update(Button::Up);
325-
if (Braccio.isJoystickPressed_DOWN())
326-
app.update(Button::Down);
327-
if (Braccio.isJoystickPressed_LEFT())
328-
app.update(Button::Left);
329-
if (Braccio.isJoystickPressed_RIGHT())
330-
app.update(Button::Right);
331-
}
332348
}
333349

334350
void handle_OnButtonDownPressed()

0 commit comments

Comments
 (0)