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

Thread: Teensy Noob needs help

  1. #1

    Teensy Noob needs help

    I do a project in college for kids. I have 4 plates which lay on the ground and the kids can jump from plate to plate to play melodies or rhythms. It's more like a proof of concept, if it makes fun and works for kids. I saw the piezo drum example sketch and get one plate to work with that sketch. Now I want to connect 4 plates playing 4 different midi notes. I thought that should not be that difficult but because I am total beginner I was not able to get it running. I thought about to use the array command but I am not really sure where and how to put it in. Could someone of you help me with a solution?

    Sorry for any mistakes, I am not a native speaker.

  2. #2
    Senior Member
    Join Date
    May 2018
    Posts
    172
    Hi,

    do you already have added a MIDI out? o
    Or do you want to use MIDI over USB?

    It would be a big advantage if you could post your current code.

    As a fast way to show how MIDI could work for you, here comes some pseudo-code:

    Code:
    #include <MIDI.h>
    
    ...
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
    #define MIDI_CHANNEL_OUT 1
    
    ...
    
    setup()
    {
       ...
       MIDI.begin(MIDI_CHANNEL_OMNI);
    }
    
    loop()
    {
      MIDI.sendNoteOn(80, 100, MIDI_CHANNEL_OUT);
      // wait some time befor sending a note-off
      delay(1000); // very bad hack... better using elapsedMillis()
      MIDI.sendNoteOff(80, 0, MIDI_CHANNEL_OUT);
      delay(1000); // very bad hack... better using elapsedMillis()
    }
    HtH, Holger

  3. #3
    This is the standard piezo drum example:

    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
    }
    }
    }

    I want to use MIDI over USB and generate sounds via sunvox.

    Thanks for respond!

  4. #4
    Senior Member
    Join Date
    May 2018
    Posts
    172
    Ah, ok - I understand...

    Yes, keeping an array for notes, piezos, velocity should be a solution... like:

    Code:
    ...
    int notes[] = { 29, 42, 30 , 33 };
    int piezos[] = { A0 ,A1 ,A2, A5};
    int velocity[4];
    ...
    ... and looping inside the loop()-function and checking values...

  5. #5
    Yeah that is what I also figured out but I am not sure how the code in the loop section looks exactly.

  6. #6
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,414
    This has come up before. Here is a post with some partially tested code.

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

Tags for this Thread

Posting Permissions

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