LED array simulation with multiple animation types in response to input

I'm using Processing to simulate a 3x144 RGB LED array. I want the array to be able to animate in a variety of different ways in response to sensors - running a fire animation when it's held vertically, having the pixels slide from one end to another when it's held horizontally, set off explosions when it's tapped, all that sort of fun. Call it an interactive walking stick.

I want the various animation routines to be in separate subroutines, for the sake of neatness. A global array holds the current state of the pixels - and I'm having trouble working out how to make things happen sequentially in this array. In other situations, I'd have a loop that runs and calls draw() from within the loop. I can't do this in Processing as far as I know. I can call a routine from the draw() loop, but to maintain state I'd have to declare all my variables globally, or they'd be reinitialised every time, and that seems like an odd way to do it.

Can anyone point me in the right direction? This is my code, and I'm trying to come up with a way to bounce a pixel from one end to another in response to a keypress (which will eventually be a sensor event).

pixel[][] pix;
int videoScale = 7;
boolean noise=true;

// Number of columns and rows in our system
int cols, rows;

void setup() {
  size(3*8,144*8);
  int greyscale=int(random(255));
  // Initialize columns and rows
  cols = 3;
  rows = 144;
  pix=new pixel[cols][rows];
  for (int k=0; k<cols; k++){
    for (int l=0; l<rows; l++){
      pix[k][l]=new pixel(greyscale,greyscale,greyscale);
    }
  }
}

void random(){
for (int k=0; k<cols; k++){
    for (int l=0; l<rows; l++){
        int rfactorr=int(random(-20,20));
        int rfactorg=int(random(-20,20));
        int rfactorb=int(random(-20,20));
        pix[k][l].r=pix[k][l].r+rfactorr;
        pix[k][l].g=pix[k][l].g+rfactorg;
        pix[k][l].b=pix[k][l].b+rfactorb;
    }
  }
}

void fadetoblack(int speed) {
 for (int k=0; k<cols; k++){
    for (int l=0; l<rows; l++){
       pix[k][l].r=pix[k][l].r-speed;
       pix[k][l].g=pix[k][l].g-speed;
       pix[k][l].b=pix[k][l].b-speed;
       if (pix[k][l].r<0){pix[k][l].r=0;}
       if (pix[k][l].g<0){pix[k][l].g=0;}
       if (pix[k][l].b<0){pix[k][l].b=0;}
    }
   }
}

void bounce(){



}

void keyPressed() {
  if (noise==false){noise=true;}else{noise=false;}
}

void draw() {
  if (noise==true){random();} else {fadetoblack(5);}

  // Begin loop for columns
  for (int i = 0; i < cols; i++) {
    // Begin loop for rows
    for (int j = 0; j < rows; j++) {

      // Scaling up to draw a rectangle at (x,y)
      int x = i*videoScale;
      int y = j*videoScale;

      fill(pix[i][j].r,pix[i][j].g,pix[i][j].b);
      stroke(0);
      // For every column and row, a rectangle is drawn at an (x,y) location scaled and sized by videoScale.
      rect(x,y,videoScale,videoScale); 
    }
  }
}

class pixel {
  int r,g,b;

  //constructor
  pixel(int tempR, int tempG, int tempB){
    r=tempR; 
    g=tempG;
    b=tempB;
  }

}

Answers

  • edited June 2015 Answer ✓

    I am not sure what you are getting at, but here I show you a sketch that shows you how to have different sub-sketches all called from draw().

    Once your event triggers another sub-sketch you have to init your canvas (reset the array) and make other inits.

    //
    // states
    final int stateMenu                  = 0;
    final int stateSeeChartFootball      = 1;
    final int stateSeeFootballStatistics = 2;
    int state = stateMenu;
    //
    // font 
    PFont font; 
    //
    // ----------------------------------------------------------------------
    // main functions
    
    void setup()
    {
      // runs only once
      //
      size(700, 600);
      smooth();
      font = createFont("ARCARTER-78.vlw", 14);
      textFont(font);
    } // func
    
    // 
    
    void draw()
    {
      // the main routine. It handels the states.
      // runs again and again 
      switch (state) {
    
      case stateMenu:
        showMenu();
        break;
    
      case stateSeeChartFootball:
        handleStateSeeChartFootball();
        break;
    
      case stateSeeFootballStatistics:
        handleStateSeeFootballStatistics();
        break;
    
      default:
        println ("Unknown state (in draw) "
          + state
          + " ++++++++++++++++++++++");
        exit();
        break;
      } // switch
    
      //
    } // func
    
    // ----------------------------------------------------------------
    // keyboard functions 
    
    void keyPressed() {
      // keyboard. Also different depending on the state.
      switch (state) {
    
      case stateMenu:
        keyPressedForStateMenu();
        break;
    
      case stateSeeChartFootball:
        keyPressedForStateSeeChartFootball();
        break;
    
      case stateSeeFootballStatistics:
        keyPressedForStateSeeFootballStatistics();
        break;
    
      default:
        println ("Unknown state (in keypressed) "
          + state
          + " ++++++++++++++++++++++");
        exit();
        break;
      } // switch
      //
    } // func
    
    void keyPressedForStateMenu() {
      //
      switch(key) {
    
      case '1':
        state = stateSeeChartFootball;
        break;
    
      case '2':
        state = stateSeeFootballStatistics;
        break;
    
      case 'x':
      case 'X':
        // quit
        exit();
        break;
    
      default:
        // do nothing
        break;
      }// switch
      //
    } // func
    
    void keyPressedForStateSeeChartFootball() {
      // any key is possible
      switch(key) {
    
      default:
        state = stateMenu;  // go back to menu
        break;
      } // switch
      //
    } // func
    
    void keyPressedForStateSeeFootballStatistics() {
      // any key is possible
      switch(key) {
    
      default:
        state = stateMenu;  // go back to menu
        break;
      } // switch
      //
    } // func
    
    // ----------------------------------------------------------------
    // functions to show the menu and functions that are called from the menu. 
    // They depend on the states and are called by draw(). 
    
    void showMenu() {
      background(255, 204, 0);
      fill(0);
      textSize(32);
      text(" Your football program ", 150, 100, 3);
      textSize(14);
      text("Press 1 See chart football ", 100, 200);
      text("Press 2 See football statistics ", 100, 220);
      // 
      text("Press x to quit ", 100, 260);
      //
    } // func
    
    void handleStateSeeChartFootball() {
      background(255, 204, 0);
      fill(0);
      textSize(32);
      text(" See chart football ", 150, 100, 3);
      textSize(14);
      text("..... some text ", 100, 200);
      //
    } // func
    
    //
    
    void handleStateSeeFootballStatistics() {
      background(255, 204, 0);
      fill(0);
      textSize(32);
      text(" See football statistics ", 150, 100, 3);
      textSize(14);
      text("..... some text ", 100, 200);
      //
    } // func
    
    // ----------------------------------------------------------------
    //
    
  • edited June 2015 Answer ✓
    • here you can see a similiar sketch with separate setup0, setup1, setup2 etc. and draw0, draw1, draw2 etc. and all called from draw()

    http://forum.processing.org/two/discussion/10678/menu-in-processing

    ;-)

  • Ah, I get it - treat draw() as a simple loop to monitor input, and break the actual rendering out into the subroutines. That's the idea I was looking for, thanks!

Sign In or Register to comment.