frogeraie
YaBB Newbies
Offline
Posts: 36
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!