Trying to read piezo, and record the top value from analogRead()

I have a piezo attached to my Uno and wired up such that when i tap it, analogRead() reads the values.

I have written this code (noob alert). I put some comments in.

When I run this the Serial Monitor runs 0 down the screen. When I tap the piezo a value or two appear (hard to tell)

My code seems logical to me… Just now sure why it keeps printing 0 to the monitor when the only Serial.println is in an IF statement that should only be hit when an index value is greater than 0 (ie: the array got values.

int aValues[20];
int i = 0;
int topValue;

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

void loop() {
  
  int val = analogRead(A0);

   // if a value, store it in the array
   if (val != 0){
      aValues[i] = val;
      i++;
   } 
   // if val is 0, there may or may not be values in the array
   // as i got incrmented when a value was added, if it it greater than 0, there is 
   // an array to process. 
   if (val == 0){
    if (i > 0){
        topValue = TopValue();
        Serial.println(topValue);
        // set the array to 0
        memset(aValues, 0, sizeof(aValues));
        i = 0;
    
      } 
    }
   
    delay(2);

      
  };



int TopValue(){
  int top = aValues[0];
  for (int z; z <= i; z++){
     if (top < aValues[z]){
      top = aValues[z];
     }
  }
  return top;
}

for (int z; z <= i; z++){oops

Nice spot. Thanks TheMemberFormally.

Should be int z = 0

Still, that does not explain why the only println in the code is getting hit every loop.

I’m not sure I can see what stops you dribbling off the end of aValues.

Taking the thread title literally, ie

Trying to read piezo, and record the top value from analogRead()

int topValue = 0;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
}

void loop()
{
  int currentValue = analogRead(A7);  //any analogue input will do
  if (currentValue > topValue)
  {
    topValue = currentValue;
    Serial.println(topValue);
  }
}

Thanks UkHelibob. Alas, that code did not work.

First, when I ran it the Serial Mon output two numbers

357
367

Next when I tap the piezo, I get no values.

By 'read piezo' I mean each time I tap it.

analogRead() might return

15
45
200
78
15

...when I tap the piezo. I want the 200.

Alas, that code did not work

But it pointed you in the right direction.
Over to you.

Your piezo creates A/C. Do you have a circuit that takes care of the negative values?

Paul

Alas, that code did not work.

On the contrary it looks like it worked perfectly

What I think you mean is that it did not do what you want, but that is no surprise as you had not explained exactly what you wanted and I did say that I was taking the thread title literally

If you want to record the max value each time you tap the piezo then something needs to happen to set the max value back to zero between taps. That could be done by reading another input, by resetting the value after a period of time or perhaps by resetting it when the input has been significantly low for a number of reads

Paul_KD7HB:
Your piezo creates A/C. Do you have a circuit that takes care of the negative values?

Currents are tiny, and the ESD pin protection should be able to take care of negative pulses.
Unless you ‘tap’ with a hammer.
Must have that 1Megohm resistor across, to bleed any DC on the piezo to ground.

Try this (untested).
Leo…

const byte piezoPin = A0;
int threshold = 10;
int rawValue, piezoValue;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // remove this line if too sensitive
}

void loop() {
  if (analogRead(piezoPin) > threshold) { // only process/print when tapped
    int piezoValue = 0; // reset
    for (int i = 0; i < 1000; i++) { // multiple A/D readings
      rawValue = analogRead(piezoPin);
      if (rawValue > piezoValue) piezoValue = rawValue; // store peaks
    }
    Serial.print("Peak value: ");
    Serial.println(piezoValue);
  }
}

Wawa:
Currents are tiny, and the ESD pin protection should be able to take care of negative pulses.
Unless you 'tap' with a hammer.
Must have that 1Megohm resistor across, to bleed any DC on the piezo to ground.

Try this (untested).
Leo..

...

Your code works a treat. Thanks so much Leo..

Apologies to all for not being more clear in my title. Will do better next time. :slight_smile:

Thanks all.