Multiple piezo sensors

Hey!

I want to connect multiple piezo sensors to my Arduino Uno. 6 sensors in total.
The goal is to measure pressure from interaction with a foam ball with the piezo's.

I've done the practical part, and i need to code the piezo's, so i can monitor them (for now just in the serial monitor).

This is what i have atm.:

// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int knockSensor2 = A1;
const int knockSensor3 = A2;
//etc. etc.

const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int sensorReading2 = 0;
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  Serial.begin(9600);       // use the serial port
}

void loop() {

  int minThres = 50;
  int maxThres = 150;

  //perhaps an array? sensorReadingArray[6] = {sensorReading, sensorReading2, etc.)

  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
  sensorReading2 = analogRead(knockSensor2);
  


  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;
    // update the LED pin itself:
    digitalWrite(ledPin, ledState);
  }

  if (sensorReading2 >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;
    // update the LED pin itself:
    digitalWrite(ledPin, ledState);

  }


Serial.println(sensorReading);
Serial.println(sensorReading2);

  //Her måler vi om tingene er = 50 eller over, eller = 150 præcis.
  //Ellers kan vi måle hvad sensorReading fra piezo er: Serial.println(sensorReading);


if ((sensorReading > 50) && (sensorReading < 150)) {

    Serial.println("Peizo sensor 1 er mellem 50 og 150");

  } else {
    Serial.println("Out of range");
  }



  delay(300);

}

The code is clunky, and doesn't work properly.
Do you have any suggestions? Do i have to make an array of all the piezo sensorReadings?

I have found this example with MIDI's and a piano:

Click here and look in their code. Can't figure out if thats what i need

All help is appreciated!

P.S. Don't mind the writing, some of it is Danish.

rewrote your code to use an array.

It now only prints when value is above threshold, the delay is removed to make it more responsive.
superfluous comments are removed too :wink:

const int ledPin = 13;
const int knockSensor[6] = { A0, A1, A2, A3, A4, A5 };
const int threshold = 100;

int sensorReading[6] = {0, 0, 0, 0, 0, 0};
int ledState = LOW;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);       // use the serial port AT HIGHEST SPEED!
}

void loop()
{
  int minThres = 50;
  int maxThres = 150;

  for (int i = 0; i < 6; i++)
  {
    // read twice to minimize interference
    sensorReading[i] = analogRead(knockSensor[i]);
    sensorReading[i] = analogRead(knockSensor[i]);

    if (sensorReading[i] >= threshold)
    {
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
      Serial.println(sensorReading[i]);
    }
  }
  // do not use delay() as it is blocking...
}

give it a try

@robtillaart Looks smooth! Had a feeling i had to make the sensors an array.
Gonna give it a go soon.

I have a few questions:

  1. int sensorReading[6] = {0, 0, 0, 0, 0, 0};
    Why do you set the integer array to 0 at every placeholder? Is it because we define the values of the sensorReadings in the forloop:
 for (int i = 0; i < 6; i++)
  {
    // read twice to minimize interference
    sensorReading[i] = analogRead(knockSensor[i]);

?

  1. Serial.begin(115200);
    This means it will use the port at highest speed. Will that work in every arduino case?
    I thought the standard were 9600.

  2. The for-loop has // read twice to minimize interference.
    What does this mean basically? interference for what?

Again, thank you for helping me with this!

Edit: Oh, and please tell me to elaborate, if you're unsure of my question.
English isn't my mother tongue.

Byblix:
I have a few questions:

  1. int sensorReading[6] = {0, 0, 0, 0, 0, 0};
    Why do you set the integer array to 0 at every placeholder?

Just to be sure what its initial value is. I had my portion of uninitialized variables in the past.

  1. Serial.begin(115200);
    This means it will use the port at highest speed. Will that work in every arduino case?
    I thought the standard were 9600.

Yes at least for the 16Mhz ones on HW serials. 9600 is just one of the standard serial baud rates.

  1. The for-loop has // read twice to minimize interference.
    What does this mean basically? interference for what?

The Arduino has one ADC which is multiplexed, to a measurement from another pin might affect the current reading. Two readings minimizes this effect.

Again, thank you for helping me with this!
Welcome, that is where the forum is about, helping each other, learning, and making fun with Arduino.

@robtillaart
Thanks!

That explains it all, but i do want you to elaborate on the last part.

The Arduino has one ADC = The Arduino is meant to only have one analog reading input (in this case by a piezo)?

Im actually using the Arduino in combination with my semester final project.
If you want me to, i can upload a photo of what i have atm.?
Would love some feedback / ideas.

Wish me luck :slight_smile:

The 328 chip has internallyonly one ADC convertor. BY selecting the pin in AnalogRead() that one and only ADC is reading the voltage from another pin.

a bit like in this image

http://www.nutaq.com/sites/default/files/10_Louis%20Chouinard_image%204.jpg

You can always post your progress,

good luck :wink:

robtillaart:
...a measurement from another pin might affect the current reading. Two readings minimizes this effect.

A "ghost charge" of the previous read could still be present on the sample cap of the A/D converter.

A bit like eating different snacks on a party.
If you try another snack, there is still some left in your mouth from the previous one.
Untill you take a second bite byte.
Leo..

perfect analogy!

Hey you guys,

i'm back with some progress.

const int ledPin = 13;
const int knockSensor[6] = { A0, A1, A2, A3, A4, A5 };
const int threshold = 100;
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int sensorReading[6] = {0, 0, 0, 0, 0, 0};
int ledState = LOW;

void setup()
{
  Serial.begin(38400);       // use the serial port AT HIGHEST SPEED!
}

void loop()
{

  unsigned long currentMillis = millis(); //igangsætter sekundttæller metoden fra 0; tæller 1 sekund (se variabler)
  int minThres = 50;
  int maxThres = 150;

  for (int i = 0; i < 6; i++)
  {
    // read twice to minimize interference
    sensorReading[i] = analogRead(knockSensor[i]); //Måler 12 gange (2x6)
    sensorReading[i] = analogRead(knockSensor[i]);

    if (sensorReading[i] > minThres && sensorReading[i] < maxThres)
    {
      if (currentMillis - previousMillis >= 4000)
      { // save the last time you blinked the LED - resetter currentmillis til previousmillis ();
        previousMillis = currentMillis;

        // play track 1. name on sd-card: 001.mp3
        Serial.print("t");
        Serial.write(1);

        //delay(1000);
        //Serial.println(sensorReading[i]); //threshold-println

      }
    }
  }
}

Basically, i've connected 6 piezo sensors to the arduino.
I've ordered this device: Flexible speaker
And i've put it up to a mp3 trigger.

The idea is, that i need to randomize the different mp3 samples, when the if() method's are called upon the code - which is when the threshold from the piezo's are between 50 and 150.

I've found the
randNumber = random(300);
Serial.println(randNumber);
Function to be built in, but i can't seem to see how it's supposed to randomly trigger the mp3 files?

Thanks!