Can't trigger two instances of Objects simultaneously.

edited September 2017 in Library Questions

I have a simple system where I am using midi (instead of sensors for now), that trigger a very simple animation in a grid made of an array of cell objects. I've set midi pitch to trigger a particular indexed cell. Everything works fine, but when I try to trigger two cells a one, only the last one I press is triggering.

I have some counters that trigger different actions in some conditionals, and maybe that is where the problem lies, but couldn't figure it out.

Main Code:

import themidibus.*;

MidiBus bus1;

Grid g;

int sensorIndex;
boolean sensor;

int cellsize = 50;

// Number of columns and rows in the grid
int cols = 6;
int rows = 10;

void setup() {
  size(cellsize*cols, cellsize*rows);

  MidiBus.list();
  bus1 = new MidiBus(this, 0, 1);

  g = new Grid(cols, rows, cellsize);
}

void draw() {
  background(0);
  g.display();
}

void noteOn(Note note) {
  // Receive a noteOn
  /*
  println();
   println("Note On:");
   println("--------");
   println("Channel:"+note.channel());
   println("Pitch:"+note.pitch());
   println("Velocity:"+note.velocity());
   */



  sensorIndex = note.pitch() - 48;
  //println("sensor index: ", sensorIndex);

  sensor = true;

}

void noteOff(int channel, int pitch, int velocity) {
  // Receive a noteOff
  //println();
  //println("Note Off:");
  //println("--------");
  //println("Channel:"+channel);
  //println("Pitch:"+pitch);
  //println("Velocity:"+velocity);

  sensor = false;
}

Cell Object Code:

class Cell {

  // A cell object knows about its location in the grid as well as its size with the variables x, y, w, h.
  float x, y;   // x,y location
  float w, h;   // width and height
  int cellIndex;
  boolean on;
  boolean click = false;
  float ramp = 0;
  float sensorTime;
  float duration;
  boolean sensorOn = false;

  float timeToCharge = 180;
  float counter = timeToCharge;


  // Cell Constructor
  Cell(float tempX, float tempY, float tempW, float tempH, int tempIndex, boolean tempOn, float tempSensorTime, float tempDuration, boolean tempSensorOn) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    cellIndex = tempIndex;
    on =  tempOn;
    sensorTime = tempSensorTime;
    duration = tempDuration;
    sensorOn = tempSensorOn;
  }

  void display() {
    stroke(255, 0, 0);

    if (sensor) {
      sensorOn = true;
    } else {
      sensorOn = false;
    }



    if (on == true) {
      counter--;
      if (counter > 0) {
        fill(0);
      } else if (counter <= 0) {
        //println(counter);

        fill(255, ramp++);
        if (ramp >=255) {
          ramp = 255;
        }
      }
    } else if (on == false) {
      fill(255, ramp--);  
      if (ramp <=0) {
        ramp = 0;
        counter = timeToCharge;
      }
    }

    rect(x, y, w, h);

    //fill(255, 0, 0);
    if (on == true) {
      fill(0, 255, 0);
    } else if (on ==false) {
      fill(255, 0, 0);
    }
    textSize(12);
    textAlign(CENTER);
    text(cellIndex, x+w/2, y+h/2+6 );
  }
}

Grid Object Code:

class Grid {

  Cell[] grid;
  float animationTime = 600;

  Grid(int tempCols, int tempRows, int tempSize) {

    grid = new Cell[cols*rows];

    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
        int index = i + j * cols;

        grid[index] = new Cell(i*cellsize, j*cellsize, cellsize, cellsize, index, false, 180, animationTime, false);
      }
    }
  }

  void update() {
  }

  void display() {
    for (int i = 0; i < cols; i++) {     
      for (int j = 0; j < rows; j++) {

        int index = i + j * cols;
        grid[index].display();

        //CODE FOR SENSORS (hopefully)

        //if sensor index is same as midi CC && sensor[index] is on && cell is off 
        if (sensorIndex == grid[index].cellIndex  && grid[index].sensorOn && grid[index].on == false) {
          // start coundown of sensor time to trigger
          grid[index].sensorTime--;
          println("Sensor ", grid[index].cellIndex, "time: ", grid[index].sensorTime);
          // cell on when sensor time is 0
          if (grid[index].sensorTime <= 0) {

            grid[index].on = true;
            grid[index].sensorTime = 180;
          }
        } else if (grid[index].sensorOn == false) {  // if sensor if before sensor time is 0 reset sensor time
          grid[index].sensorTime = 180;
        }

        // animation time will start countdown here
        if ( grid[index].on) {
          grid[index].duration--;
          println("Sensor ", grid[index].cellIndex, "duration: ", grid[index].duration);
        }

        // turns off animation when duration is 0
        if (grid[index].duration <= 0) {
          grid[index].on = false;
          grid[index].duration = animationTime;
        }
      }
    }
  }
}
Tagged:

Answers

  • There is something you need to consider in your code before moving ahead. Check this:

    g = new Grid(cols, rows, cellsize);

    You are passing the cellSize to your constructor. However, in your constructor you are not using this info. Instead, you are accessing cellSize from the global scope. In other words, you need to use the parameters supplied to your constructor.

    Can you comment on the difference between grid[index].sensorOn and grid[index].on?

    Kf

  • Hi krafjer, thanks for responding.

    You are right it, the cellsize in the Grid constructor was extra since I am using the cellsize variable to instantiate the Cell objects in the Grid. I am using global variables here to adjust size of sketch accordingly.

    grid[index.]sensorOn - is when the actual sensor is ON

    grid[index].on - is when the animation is triggered

    I used these two so in Grid I can use these different variables in different conditionals to make the system work. Their is an element of timing here. Maybe I still could have used one variable?

    Regarding the issue I had where multiple sensors could not be triggered, I've found a solution. In noteOn(Note note) in the main code there is only one sensorIndexvariable which eventually can only trigger one sensor at a time. I solved this by using an IntList() array which contains all the sensors triggered and appends their value to the list. This is then used instead of thesensorIndex variable used in the conditionals in Grid.

Sign In or Register to comment.