We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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.
setup0
,setup1
,setup2
etc. anddraw0
,draw1
,draw2
etc. and all called fromdraw()
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!
great!