Piezo sensor burst reading when hit, please help

hi, i have made a sketch that can read piezo sensors when hit… my problem is,when my sensor is hit, it burst read the values of it., that’s why it has a delay before executing the process.,

here is my code

void loop() {

snare = analogRead(snarepin);
delay(10);
snare = analogRead(snarepin);
delay(10);

if (snare>20){
 send(0x0001);//plays sound 1 in wave shield
 send(0xfffe); //play
 Serial.println(analogRead(snarepin));
}

kick = analogRead(kickpin);
delay(10);
kick = analogRead(kickpin);
delay(10);
if (kick>20){
 send(0x0000);//play sound 0 in wave shield
 send(0xfffe);//play
  Serial.println(analogRead(kickpin));
}
  
}

here is the value when i hit it in three consecutive times

312
54
15
278
48
7
252
45
9

so when i hit my sensor, it has a delay before playing the sound. please help me with this,i really need to have the proper timing for my sound to play. is there any solution to this?

and also it has a ticking sound when it reads the values that’s not in the range of the analogread value…

The code you posted does not compile so I cannot recreate the system, sorry

oh, it’s because i did not post the other codes,
here it is,

int snare;
int kick;
const int snarepin = A2;
const int kickpin = A5;
   
void setup() {   

    analogReference(DEFAULT);
    Serial.begin(9600);
      
}

void loop() {

snare = analogRead(snarepin);
delay(10);
snare = analogRead(snarepin);
delay(10);

if (snare>20){
  Serial.println(analogRead(snarepin));
}


kick = analogRead(kickpin);
delay(10);
kick = analogRead(kickpin);
delay(10);
if (kick>20){
  Serial.println(analogRead(kickpin));
}
  
}
int snare;
int kick;
const int snarepin = A2;
const int kickpin = A5;

unsigned long lastsnare = 0;  // will hold the millis() time of the last hit
unsigned long lastkick = 0;
const int snaredelay = 1000; // minimum allowed milliseconds between snare hits
const int kickdelay = 1000;

const int piezothreshold  = 20; // analogRead value that indicates a hit

void setup() {   
    analogReference(DEFAULT);
    Serial.begin(9600);
}

void loop() {
  if (analogRead(snarepin) > piezothreshold && millis() > lastsnare + snaredelay) { 
     // play the snare
    lastsnare = millis();
  }

  if (analogRead(kickpin) > piezothreshold && millis() > lastkick + kickdelay) { 
     // play the kick
    lastkick = millis();
  }
}
  
}

thanks so much! that solved the problem,, sorry for late reply, i got things to do on the past days.. but thanks a lot! :smiley:

hi, the codes you gave works, but when i change the minimum allowed milliseconds between hits to lower value,, it sometimes play the other sound.. when i hit the snare, sometimes it play the kick sound and vice versa,, what should i do?

Assuming there are no other errors in your code:

"The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins."

Sometimes the simplest solution is to just do a preliminary analogRead() on the pin to give it a chance to discharge.

  analogRead(snarepin);
  if (analogRead(snarepin) > piezothreshold && millis() > lastsnare + snaredelay) { 
     // play the snare
    lastsnare = millis();
  }

thanks! i will try that. :slight_smile: thank you so much :slight_smile: