Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Duplicating Arduino example code to work for 5 piezo mics

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1

    Duplicating Arduino example code to work for 5 piezo mics

    Hi,

    I'm currently working on a uni project in which I'm making a glove with piezo sensors on the fingertips that output a MIDI note into Ableton. I've found some example code in Arduino which works perfectly for one piezo, but I'm not sure how to change the code in a way that will allow five different piezos to output five different MIDI notes. I'll post the example code below for reference -

    const int channel = 10; // General MIDI: channel 10 = percussion sounds
    const int note = 36; // General MIDI: note 38 = acoustic snare

    const int analogPin = A0;
    const int thresholdMin = 60; // minimum reading, avoid noise and false starts
    const int peakTrackMillis = 12;
    const int aftershockMillis = 25; // aftershocks & vibration reject


    void setup() {
    Serial.begin(115200);
    while (!Serial && millis() < 2500) /* wait for serial monitor */ ;
    Serial.println("Piezo Peak Capture");
    }


    void loop() {
    int piezo = analogRead(analogPin);
    peakDetect(piezo);
    // Add other tasks to loop, but avoid using delay() or waiting.
    // You need loop() to keep running rapidly to detect Piezo peaks!

    // MIDI Controllers should discard incoming MIDI messages.
    // http://forum.pjrc.com/threads/24179-...ses-midi-crash
    while (usbMIDI.read()) {
    // ignore incoming messages
    }
    }


    void peakDetect(int voltage) {
    // "static" variables keep their numbers between each run of this function
    static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks
    static int peak; // remember the highest reading
    static elapsedMillis msec; // timer to end states 1 and 2

    switch (state) {
    // IDLE state: wait for any reading is above threshold. Do not set
    // the threshold too low. You don't want to be too sensitive to slight
    // vibration.
    case 0:
    if (voltage > thresholdMin) {
    //Serial.print("begin peak track ");
    //Serial.println(voltage);
    peak = voltage;
    msec = 0;
    state = 1;
    }
    return;

    // Peak Tracking state: capture largest reading
    case 1:
    if (voltage > peak) {
    peak = voltage;
    }
    if (msec >= peakTrackMillis) {
    //Serial.print("peak = ");
    //Serial.println(peak);
    int velocity = map(peak, thresholdMin, 1023, 1, 127);
    usbMIDI.sendNoteOn(note, velocity, channel);
    msec = 0;
    state = 2;
    }
    return;

    // Ignore Aftershock state: wait for things to be quiet again.
    default:
    if (voltage > thresholdMin) {
    msec = 0; // keep resetting timer if above threshold
    } else if (msec > aftershockMillis) {
    usbMIDI.sendNoteOff(note, 0, channel);
    state = 0; // go back to idle when
    }
    }
    }

    Apologies if I've been confusing, coding and Arduino are still very new to me!

    Any help is very greatly appreciated.

  2. #2
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,112
    I once wrote an extension to Paul's single-piezo example code for someone....

    https://forum.pjrc.com/threads/49815...l=1#post169389

    I never tested it but others said it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •