what i am doing is i have 9 squares with 9 vibration sensors(set at 125 trigger) when the square(top left) gets hit that LED lights up (top left) but i am running into the problem sometimes where if i hit say in this case middle both middle and middle right square will light up at the exact same time as you can see from the serial monitor info below....i can not use delays because the object of the game is to light up the 9 squares the fastest .... any idea what could help
17:34:44.062 -> Middle 348
17:34:44.062 -> Middle right 393
What ever vibration trigger first ...so if it’s in between square 1 and square 2 ...square 1 should light first because it first in my program ...but I want only 1 to light up not both
some might think this is very easy to fix and would love to hear from someone that could help me out
i am building an electronic hockey net
my debounceDelay is set for 1000
what i am doing is i have 9 squares with 9 vibration sensors(set at 125 trigger) when the square(top left) gets hit that LED lights up (top left) but i am running into the problem sometimes where if i hit say in this case middle both middle and middle right square will light up at the exact same time as you can see from the serial monitor info below....i can not use delays because the object of the game is to light up the 9 squares the fastest .... any idea what could help
17:34:44.062 -> Middle 348 17:34:44.062 -> Middle right 393
I'm not entirely sure what you are asking but I think you are trying to do the debounce independently on each pin? Regardless, you can shrink you code way down by using arrays...
const byte buttonPin[] = { A4, A5, A6, A7, A8, A9, A10, A11, A12 };
const byte buttonCount = sizeof(buttonPin) / sizeof(buttonPin[0]);
const byte ledPin[buttonCount] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bool ledon[buttonCount];
const char * pinLabel[buttonCount] = {
"Top Left ", "Top Middle ", "Top right ",
"Middle Left ", "Middle ", "Middle right ",
"Bottom Left ", "Bottom ", "Bottom right ",
};
unsigned int buttonState[buttonCount];
unsigned long lastDebounceTime[buttonCount];
const unsigned long debounceDelay = 1000;
const unsigned int SEN = 125;
void loop() {
for ( int i = 0; i < buttonCount; ++i) {
buttonState[i] = analogRead(buttonPin[i]) ; //Reading statuses of the pins
if ( (millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonState[i] > SEN) {
digitalWrite(ledPin[i], HIGH);
ledon[i] = true;
Serial.print(pinLabel[i]);
Serial.println(buttonState[i]);
lastDebounceTime[i] = millis();
}
}
}
}
You can also eliminate the buttonState array and ledon array if you don't use it elsewhere in your code. They can just be a single variable inside the for() loop.