[+] Bounce delay
This commit is contained in:
@@ -20,10 +20,16 @@ const int NUM_DRUMSTICKS = 2;
|
|||||||
const int DRUMSTICK_PINS[] = {A0, A1};
|
const int DRUMSTICK_PINS[] = {A0, A1};
|
||||||
int LAST_VALUES[] = {0, 0};
|
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
|
// 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
|
// After the division, values range from 0 to 127
|
||||||
val threshold = (int) (0.7 * 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()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -34,6 +40,8 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
val time = millis();
|
||||||
|
|
||||||
// Loop through all the pins, indexed
|
// Loop through all the pins, indexed
|
||||||
for (let i = 0; i < NUM_DRUMSTICKS; i++)
|
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 the value is different from the last value, update it
|
||||||
if (value != last)
|
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 it's greater than the threshold
|
||||||
if (value > 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 the last value was greater than the threshold, it means the drumstick was just released
|
||||||
if (last > threshold)
|
if (last > threshold)
|
||||||
|
{
|
||||||
send("Hit:-%d", i);
|
send("Hit:-%d", i);
|
||||||
|
LAST_HIT_TIMES[i] = time;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user