We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Flux control (Read 245 times)
Flux control
Jul 18th, 2008, 8:28pm
 
Is there a way of writing a processing code rather than using the "setup() and draw()" structure (without losing the possibility of using functions)?

What I need is to control the execution of the beginning of my code. I want that the window pop's up with the information "press a key to start" before the code starts running the serial. When I run the following program, it only prints the information after the connection is done (I press a key, the connection is made, and then, it prints the information).

[code]void setup()
{
 size(1024, 768);
 
 background(random(255),random(255), random(255));

 // Set the font and its size (in units of pixels)  
 PFont fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
 textFont(fontA, 45);
 text( "press a key",84, 339);
 text( "to start",114, 384);
 
 while (!keyPressed) {
 }

 port = new Serial(this, "COM40", 115200);
}[\code]

Any ideas?
Re: Flux control
Reply #1 - Jul 20th, 2008, 9:05pm
 
I would use something like a mode switch :

Code:
int mode;
final int MODE_WAIT_FOR_KEY = 0;
final int MODE_MAIN_LOOP = 1;

PFont fontA;

void setup() {
fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
textFont(fontA, 45);
mode = MODE_WAIT_FOR_KEY;
}

void draw() {
switch(mode) {
case MODE_WAIT_FOR_KEY:
waitForKey();
break;
case MODE_MAIN_LOOP:
mainLoop();
break;
}
}

void waitForKey() {
background(255);
text( "press a key",84, 339);
text( "to start",114, 384);
}

void mainLoop() {
background(255);
text( "connection has been made.",84, 339);
text( "running the main loop now.",114, 384);
}

void keyPressed() {
if (mode == MODE_WAIT_FOR_KEY) {
port = new Serial(this, "COM40", 115200);
mode = MODE_MAIN_LOOP;
}
}
Page Index Toggle Pages: 1