Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Replaced delay(500) with non-blocking timestamp-based debounce. The blocking delay prevented MIDI and serial processing during the 500ms wait period.

Changes

  • Added configurable constant: DEBOUNCE_TIME_MS (500ms) in user-editable section
  • Added per-button timestamp tracking: lastTriggerTime[] array initialized in setup()
  • Replaced blocking delay with time-based check: Uses millis() with overflow-safe arithmetic

Implementation

Before:

if (buttonState[i] == LOW) {
  sendKeySequence(i);
  delay(500); // Blocks entire loop
}

After:

if (buttonState[i] == LOW) {
  unsigned long currentTime = millis();
  if (currentTime - lastTriggerTime[i] >= DEBOUNCE_TIME_MS) {
    sendKeySequence(i);
    lastTriggerTime[i] = currentTime;
  }
}

The unsigned arithmetic correctly handles millis() overflow at ~50 days uptime.

Original prompt

This section details on the original issue you should resolve

<issue_title>Aggiungere debounce corretto al codice al posto di delay(500)</issue_title>
<issue_description>Attualmente il codice utilizza delay(500) per simulare il debounce, ma questa non è una soluzione ottimale.

Richiesta:

  • Implementare un debounce corretto al posto dell'attuale delay(500).
  • Il tempo di debounce deve essere definito in una costante facilmente modificabile (es: #define DEBOUNCE_TIME_MS 500), posizionata nella prima parte dello script insieme alle altre costanti per una facile configurazione, tra i commenti "START EDITING HERE " e "STOP EDITING HERE"
  • Non utilizzare una soluzione "hardcoded" per il valore.

Riferimenti nel codice:

  • arduino/push-push/push-push.ino:
    Nella funzione loop() viene utilizzato delay(500) dopo la chiamata a sendKeySequence(i) per evitare ripetizioni accidentali.
    Visualizza codice
  • All'inizio dello script, vicino alle costanti principali, puoi inserire la definizione della costante debounce:
    Visualizza punto consigliato

Benefici:

  • Migliora la leggibilità e l'adattabilità del codice
  • Consente di modificare agevolmente il comportamento della funzione debounce

Labels: enhancement, debounce (aggiungi "enhancement")</issue_description>

<agent_instructions>don't work in the main branch, create a new branch</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: garubi <10868669+garubi@users.noreply.github.com>
Copilot AI changed the title [WIP] Add proper debounce implementation instead of delay(500) Replace blocking delay() with proper debounce mechanism Jan 27, 2026
Copilot AI requested a review from garubi January 27, 2026 10:12
@garubi garubi marked this pull request as ready for review January 27, 2026 18:01
@garubi garubi merged commit c0d5006 into main Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aggiungere debounce corretto al codice al posto di delay(500)

2 participants