Problem with Serial.println() and piezos

Hi,
I have a problem with my circuit.
There are 6 piezos connected to an arduino Uno.
I send a message with Serial.printl() every time a piezo is knocked.
After a minute or two, several messages are being sent for only one knock.
What is the problem ?
Thank you in advance for your answer.

sketch.ino (436 Bytes)

Code from Original Post so others don't have to download it.

int analogPins[] = {0, 1, 2, 3 , 4, 5};
unsigned int lastTime[] = {0, 0, 0, 0, 0, 0};
int size = 6;

int i;
int val;
int piezoThreshold = 750;
int timeThreshold = 100;
 
void setup()
{
  Serial.begin(250000);
}

void loop()
{
  for (i = 0; i < size; i++)
  {
    val = analogRead(i);
    
    if (val > piezoThreshold && (millis() - lastTime[i]) > timeThreshold)
    {
      lastTime[i] = millis();
      Serial.print(i);
    }
  }
}

...R

Try separating your IFs like this (for clarity)

void loop()
{
  for (i = 0; i < size; i++)
  {
    if (millis() - lastTime[i]) > timeThreshold) 
    {
      lastTime[i] += timeThreshold;   // avoids accumulating small errors
      val = analogRead(i);
    
      if (val > piezoThreshold )
      {
        Serial.print(i);
      }
    }
  }
}

...R

Something else I noticed - you should use 'unsigned long' for storing 'millis()' values, not 'unsigned int'.
Change this:-unsigned int lastTime[] = {0, 0, 0, 0, 0, 0};to this:-unsigned long lastTime[] = {0, 0, 0, 0, 0, 0};