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.
IndexProgramming Questions & HelpSyntax Questions › while() included into if()
Page Index Toggle Pages: 1
while() included into if() (Read 496 times)
while() included into if()
Nov 13th, 2007, 8:56am
 
to locate a mobile position in a plane i use 3 fixed ultrasonic transducers which are HIGH when receiving a us pulse from the mobile. arduino sends serially the corresponding sets of data, value1, 2 and 3 to processing.
to get the mobile coordinates i need to measure the time during which transducer 1 being HIGH transducer 2 is still LOW: t12 and similarly t13 and t23.
Now, i need an advice: is there something wrong with the code below? in particular is it possible to have a while() inside an if()?

 // importing the processing serial class
 import processing.serial.*;
// variables for serial connection, portname and baudrate have to be set
 Serial port;  
 int value = 0;
// buffer and value variable for each value and boolean to switch between values
 String bufA="", bufB="", bufC="";  
 int buf;  // integer switch to distinguish values
int value1, value2, value3;  
 int t,ta,tb,tc,t12,t13,t23;  
// lets user control DisplayItems properties, value output and drawing attributes
void setup(){
 // set size and framerate
 frameRate(10);
 // establish serial port connection      
 port = new Serial(this, Serial.list()[2], 9600);
 println(port);
}
// reads the serial events, stores events in buffer and asigns values
void serialEvent(int serial){
 if(serial!=10) {      
     if (serial=='A') buf = 1;
     if (serial=='B') buf = 2;
     if (serial=='C') buf = 3;
     if (buf==1){ if (serial!='A') bufA += char(serial);
               }else if (buf == 2){
               if (serial!='B') bufB+= char(serial);
               }else if (buf == 3){
               if (serial!='C') bufC+= char(serial);
               }
     } else {
               if (buf == 1){  value1 = int(bufA); bufA="";
               } else if (buf == 2){ value2 = int(bufB); bufB="";
               }else if (buf == 3){ value3 = int(bufC); bufC="";
               }
     }      
}
void draw(){
// listen to serial port and trigger serial event    
 while(port.available() > 0){
       value = port.read();
       serialEvent(value);
  }
t = millis();
if(value1 > 300 && value2 < 300 && value3 < 300){
while(value1 > 300 && value2 < 300 && value3 < 300){
ta = millis() - t;
}
if(value2 < 300){
  t13 = ta;
  }
   if(value3 < 300){
  t12 = ta;
  }
}
else if(value2 > 300 && value1 < 300 && value3 < 300){
while(value2 > 300 && value1 < 300 && value3 < 300){
tb = millis() - t;
}
if(value1 < 300){
  t23 = tb;
  }
   if(value3 < 300){
  t12 = -tb;
  }
}
else if(value3 > 300 && value2 < 300 && value1 < 300){
while(value3 > 300 && value2 < 300 && value1 < 300){
tc = millis() - t;
}
if(value2 < 300){
  t13 = -tc;
  }
   if(value1 < 300){
  t23 = -tc;
  }
}

   println("t12: "+t12+" - t13: "+t13+ " - t23: "+t23);
}

thanks!
Re: while() included into if()
Reply #1 - Nov 13th, 2007, 9:31am
 
several things that i ran into from a first look:

while ( condition ) {
   statement;
}

this will loop until condition is true, meaning that your code here:

while(value3 > 300 && value2 < 300 && value1 < 300){
           tc = millis() - t;  
       }

will probably never exit the loop since you rely on serialEvent setting value1, .., which is only being called by:

serialEvent(value);

it is only being called by your code because you have your serialEvent declaration wrong. the reference states:

void serialEvent(Serial which) {
 statements
}

but you have:

void serialEvent (int serial) {
...
}

just because the name is the same does not mean it's the same thing. you have to declare it to take exactly the same parameters as stated in the reference, "(Serial whatevernameyouwant)" that is. otherwise the library won't be able to call it and you can't receive anything ..

btw, you don't need to do both: read values via serialEvent and have a "while ( port.available() > ) { ... }" loop. either one does the job fine. serialEvent is async with draw, but since you are only setting variables in there it's probably ok.

F

Re: while() included into if()
Reply #2 - Nov 13th, 2007, 9:41am
 
oh, and: sure you can put a while inside if ..
Page Index Toggle Pages: 1