Controlp5 Matrix question.. About triggering..

edited April 2014 in Library Questions

Hi all. In my quest to build the perfect sequencer using the Controlp5 matrix object, I have a problem with triggering an event when the moving bar of the matrix grid reaches the last step... Does anyone know how to trigger an event when the moving bar reaches the last step in the sequence?
If so, I would appreciate some help. Thanks again. :) Steve.

Tagged:

Answers

  • Answer ✓

    Hello Steve, please have a look at the example below. I have included comments that I hope help you to better understand the code to customize a matrix so that you can trigger an event when reaching the last step of a sequence.

    import controlP5.*;
    
    ControlP5 cp5;
    
    void setup() {
      size(400, 400);
      cp5 = new ControlP5(this);
      int x = 20;
      int y = 8; 
      // init our CustomMatrix
      CustomMatrix m = new CustomMatrix(cp5, "matrix");
      // set parameters for our CustomMatrix
      m.setPosition(50, 100)
      .setSize(200, 200)
      .setInterval(200)
      .setGrid(x,y)
      .setMode(ControlP5.MULTIPLES)
      .setColorBackground(color(120))
      .setBackground(color(40));
    
      // initialize the presets for the CustomMatrix
      m.initPresets();
    
    }
    
    void draw() {
      background(0);
    }
    
    // function called by our CustomMatrix with name matrix
    public void matrix(int x, int y) {
      println("trigger", x, y);
    }
    
    
    // extend the Matrix class since we need to override the Matrix's sequencer
    class CustomMatrix extends Matrix {
    
      // add a list to store some presets
      ArrayList<int[][]> presets = new ArrayList<int[][]>();
      int currentPreset = 0;
      Thread update;
    
      CustomMatrix(ControlP5 cp5, String theName) {
        super(cp5, theName);
        stop(); // stop the default sequencer and
        // create our custom sequencer thread. Here we 
        // check if the sequencer has reached the end and if so
        // we updated to the next preset.
        update = new Thread(theName) {
          public void run( ) {
            while ( true ) {
              cnt++;
              cnt %= _myCellX;
              if (cnt==0) {
                // we reached the end and go back to start and 
                // update the preset 
                next();
              }
              trigger(cnt);
              try {
                sleep( _myInterval );
              } 
              catch ( InterruptedException e ) {
              }
            }
          }
        };
        update.start();
      }
    
    
      void next() {
        currentPreset++;
        currentPreset %= presets.size();
        setCells(presets.get(currentPreset));
      }
    
      // initialize some random presets.
      void initPresets() {
        for (int i=0;i<4;i++) {
          presets.add(createPreset(_myCellX, _myCellY));
        }
        setCells(presets.get(0));
      }
    
      // create a random preset
      int[][] createPreset(int theX, int theY) {
        int[][] preset = new int[theX][theY];
        for (int x=0;x<theX;x++) {
          for (int y=0;y<theY;y++) {
            preset[x][y] = random(1)>0.5 ? 1:0;
          }
        }
        return preset;
      }
    
    }
    
  • Hi Andreas & THANK YOU SO MUCH for your code. It works like a charm and I have managed to get it working in my program so now I can have a series of 8 pre stored sequences being triggered sequentially, one after the other. I used youe extended Matrix function in this way:

    // extend the Matrix class since we need to override the Matrix's sequencer
    int seqn=0;
    class CustomMatrix extends Matrix {
    
      // add a list to store some presets
      ArrayList<int[][]> presets = new ArrayList<int[][]>();
      int currentPreset = 0;
      Thread update;
    
      CustomMatrix(ControlP5 cp5, String theName) {
        super(gui2, theName);
        stop(); // stop the default sequencer and
        // create our custom sequencer thread. Here we 
        // check if the sequencer has reached the end and if so
        // we updated to the next preset.
    
        update = new Thread(theName) {
          public void run( ) {
            while ( true ) {
              cnt++;
              cnt %= _myCellX;
              if (cnt==0) {
                // we reached the end and go back to start and 
                // update the preset 
                seqn++;
                if (seqn>7)
                seqn=0;
                next();
              }
              trigger(cnt);
              try {
                sleep( _myInterval );
              } 
              catch ( InterruptedException e ) {
              }
            }
          }
        };
    
        update.start();
      }
    
    
      void next() {
      /*  currentPreset++;
        currentPreset %= presets.size();
        setCells(presets.get(currentPreset));*/
    
        switch(sequenceArray[seqn])
        {
           case 0:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval1[currentrow * columnsnumber + currentcolumn] = gets2[currentrow] [currentcolumn];
    
     value1 =bxval1[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value1);  
      selected=0;
     }
     }
      break;
       case 1:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval2[currentrow * columnsnumber + currentcolumn] = gets4[currentrow] [currentcolumn];
    
     value2 =bxval2[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value2);  
      selected=1;
     }
     }
      break;
       case 2:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval3[currentrow * columnsnumber + currentcolumn] = gets6[currentrow] [currentcolumn];
    
     value3 =bxval3[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value3);  
      selected=2;
     }
     }
      break;
    
          case 3:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval4[currentrow * columnsnumber + currentcolumn] = gets8[currentrow] [currentcolumn];
    
     value4 =bxval4[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value4);  
      selected=3;
     }
     }
      break;
       case 4:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval5[currentrow * columnsnumber + currentcolumn] = gets10[currentrow] [currentcolumn];
    
     value5 =bxval5[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value5);  
      selected=4;
     }
     }
      break;
       case 5:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval6[currentrow * columnsnumber + currentcolumn] = gets12[currentrow] [currentcolumn];
    
     value6 =bxval6[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value6);  
      selected=5;
     }
     }
      break;
       case 6:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval7[currentrow * columnsnumber + currentcolumn] = gets14[currentrow] [currentcolumn];
    
     value7 =bxval7[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value7);  
      selected=6;
     }
     }
      break;
       case 7:
         for ( int currentrow = 0; currentrow < rowsnumber; currentrow++)
     {
     for (int currentcolumn = 0; currentcolumn < columnsnumber; currentcolumn++)
     {
     bxval8[currentrow * columnsnumber + currentcolumn] = gets16[currentrow] [currentcolumn];
    
     value8 =bxval8[currentrow * columnsnumber + currentcolumn];
    
      gui2.get(CustomMatrix.class, "Sequencer").set(currentrow,currentcolumn,value8);  
      selected=7;
     }
     }
      break;
      default:
      break;
    
        }
        print("HDJDGHKIDHKI");
    
      }
      // initialize some random presets.
      void initPresets() {
        for (int i=0;i<4;i++) {
         presets.add(createPreset(_myCellX, _myCellY));
    
        }
       setCells(presets.get(0));
      }
    
      // create a random preset
      int[][] createPreset(int theX, int theY) {
        int[][] preset = new int[theX][theY];
        for (int x=0;x<theX;x++) {
          for (int y=0;y<theY;y++) {
            preset[x][y] = random(1)>0.5 ? 1:0;
    
          }
        }
        return preset;
      }
    
    }
    

    Once again I seriously appreciate this. Now to work out something nice to do with the Android accelerometers and gyroscopes..

  • Hi sojamo I have a doubt and I hope you can help me, I want to trigget every step of the sequencer and I don't know how I am doing a patch in pure data with some inlets and I am able to make sound with sliders but not with the matrix. can you look at it ? i can paste it here

Sign In or Register to comment.