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

Thread: To Array or not to Array?

  1. #1
    Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    32

    To Array or not to Array?

    Hi folks,

    Just wondering if this bit of code can/should use more arrays ? Or, does it need to be this way to retain separate Note numbers and thresholds per
    input. I'm using Teensy 4.0 and FSRs for my input for MIDI over USB.

    Thanks!

    Code:
    #include "MIDIcontroller.h"
    
    byte MIDIchannel = 10;
    
    const int pressPin [10] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9}; // ANALOG pin
    
    
    // Pin & Note number
    MIDIdrum Pad0(A0, 36);
    MIDIdrum Pad1(A1, 37);
    MIDIdrum Pad2(A2, 38);
    MIDIdrum Pad3(A3, 39);
    MIDIdrum Pad4(A4, 40);
    MIDIdrum Pad5(A5, 41);
    MIDIdrum Pad6(A6, 42);
    MIDIdrum Pad7(A7, 43);
    MIDIdrum Pad8(A8, 44);
    MIDIdrum Pad9(A9, 45);
    
    void setup(){
      // Threshold
      Pad0.setThreshold(30);
      Pad1.setThreshold(30);
      Pad2.setThreshold(30);
      Pad3.setThreshold(30);
      Pad4.setThreshold(30);
      Pad5.setThreshold(30);
      Pad6.setThreshold(30);
      Pad7.setThreshold(30);
      Pad8.setThreshold(30);
      Pad9.setThreshold(30);
    
    }
    
    void loop(){
      Pad0.send();
      Pad1.send();
      Pad2.send();
      Pad3.send();
      Pad4.send();
      Pad5.send();
      Pad6.send();
      Pad7.send();
      Pad8.send();
      Pad9.send();
    
    // Crash Protection
      while(usbMIDI.read()){}
    
    }

  2. #2
    Senior Member
    Join Date
    Jul 2020
    Posts
    969
    Data is data, code is code, try to keep them separate is always good.

    Here an array would be far better, assuming you can make it work with the object construction.

    I think you can do:
    Code:
    MIDIdrum Pads[10] { {A0, 36}, {A1, 37}, {A2, 38}, {A3, 39}, {A4, 40}, 
                        {A5, 41}, {A6, 42}, {A7, 43}, {A8, 44}, {A9, 45} };
    
    void setup()
    {
      for (int i = 0 ; i < 10 ; i++)
        Pads[i].setThreshold (30) ;
    }
    
    void loop()
    {
      for (int i = 0 ; i < 10 ; i++)
        Pads[i].send() ;
    // Crash Protection
      while(usbMIDI.read()){}
    }
    But I'm not sure.

  3. #3
    Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    32
    Thank for the reply Mark. I'll give it a try .. I assume that in this setup, the threshold becomes global (for all pads) ?

    Thanks

  4. #4
    Senior Member
    Join Date
    Jul 2020
    Posts
    969
    You are free to set them up different - I just noticed your code was setting them all the same, and assumed
    that's what you want.

  5. #5
    Member
    Join Date
    Nov 2019
    Location
    Johnson City TN
    Posts
    32
    Thanks Mark,

    I'm just trying to get my head around it all. There will likely be times I'm using various FSRs and may need individual thresholds. but great to know I can "shrink" things down a bit if I only need a single threshold setting.

Posting Permissions

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