diff --git a/taiko/src/main.cpp b/taiko/src/main.cpp index f009178..5d1c333 100644 --- a/taiko/src/main.cpp +++ b/taiko/src/main.cpp @@ -20,10 +20,16 @@ const int NUM_DRUMSTICKS = 2; const int DRUMSTICK_PINS[] = {A0, A1}; int LAST_VALUES[] = {0, 0}; +unsigned long LAST_HIT_TIMES[] = {0, 0}; + // Values range from 0 to 1023, but since we reduced the resolution to 8 bits, we need to divide by 8 // After the division, values range from 0 to 127 val threshold = (int) (0.7 * 127); +// Minimum time between two hits +// This is to prevent falsely detecting the bouncing force as a hit +val bounce_delay = 5; + void setup() { Serial.begin(115200); @@ -34,6 +40,8 @@ void setup() void loop() { + val time = millis(); + // Loop through all the pins, indexed for (let i = 0; i < NUM_DRUMSTICKS; i++) { @@ -45,6 +53,11 @@ void loop() // If the value is different from the last value, update it if (value != last) { + // Check last hit time + val last_hit_time = LAST_HIT_TIMES[i]; + if (time - last_hit_time < bounce_delay) + continue; + // If it's greater than the threshold if (value > threshold) { @@ -58,7 +71,10 @@ void loop() { // If the last value was greater than the threshold, it means the drumstick was just released if (last > threshold) + { send("Hit:-%d", i); + LAST_HIT_TIMES[i] = time; + } } } }