Go Down

Topic: while (Read 425 times) previous topic - next topic

Frogeraie

to determine the coordinates of a 2-d mobile i use the time-of-flight of ultrasonic pulses. the emitter is on the mobile and the two receivers are fixed. the following code works fine and send serially the data to processing:
Code: [Select]
[code]

// two variables for each input pin to read and store the values
 int digitalInput_1 = 2;  
 int digitalInput_2 = 3;
//ultrasonic pulse is sent when ePin is HIGH
 int ePin = 8;
 int value_1 = 0;  
 int value_2 = 0;

// an identifier for each value
 int id_1 = 'A';  
 int id_2 = 'B';
 int t, tet, tou;
 int ta;
 int tb;
   
void setup() {  

// declaration of pins modes    
 pinMode(digitalInput_1, INPUT);
 pinMode(digitalInput_2, INPUT);
 pinMode(ePin, OUTPUT);

// begin sending over serial port
 beginSerial(9600);
}

void loop()
{
 t = 0;
 tou = 0;
 tet = 0;
 ta = 0;
 tb = 0;
 
// read the values of the digital pins and store them in variables
 value_1 = digitalRead(digitalInput_1);  
 value_2 = digitalRead(digitalInput_2);
 digitalWrite(ePin, HIGH);
 t = millis();

while(value_1 == LOW){
  value_1 = digitalRead(digitalInput_1);  
ta = millis() - t;
}
digitalWrite(ePin, LOW);

  delay(100);
 
  digitalWrite(ePin, HIGH);
 t = millis();
while(value_2 == LOW){
  value_2 = digitalRead(digitalInput_2);  
tb = millis() - t;
}
digitalWrite(ePin, LOW);
// print out the variables preceeded by their IDs and followed by a carriage return
// (that allows us to know where the values end and when to expect the id)

   printByte(id_1);
   printInteger(ta);  printByte(10);
   
   printByte(id_2);
   printInteger(tb);  printByte(10);
     digitalWrite(ePin, LOW);  

   delay(100);      
}
[/code]
to achieve this i have to synchronize emitter and receivers, i.e., to link (through a wire or Xbee) ePin and the ultrasonic transducer.
NOW i want to get rid of this link!
i use three receivers instead of 2 and deduce the mobile position from the time-of-flight differences t12, t13 and t23:
Code: [Select]
 int digitalInput_1 = 2;  
 int digitalInput_2 = 3;
 int digitalInput_3 = 4;  
 int value_1 = 0;  
 int value_2 = 0;
 int value_3 = 0;  
// an identifier for each value
 int id_1 = 'A';  
 int id_2 = 'B';
 int id_3 = 'C';  
 int t,ta,tb,tc,t12,t13,t23;
 
   
void setup() {  

// declaration of pins modes    
 pinMode(digitalInput_1, INPUT);
 pinMode(digitalInput_2, INPUT);
 pinMode(digitalInput_3, INPUT);
// begin sending over serial port
 beginSerial(9600);
}

void loop()
{
 t = 0, ta = 0, tb = 0, tc = 0, t12 = 0, t13 = 0, t23 = 0;  
// read the values od the digital pins and store them in variables
 value_1 = digitalRead(digitalInput_1);  
 value_2 = digitalRead(digitalInput_2);
  value_3 = digitalRead(digitalInput_3);
 t = millis();
if(value_1 == HIGH && value_2 == LOW && value_3 == LOW){
while(value_1 == HIGH && value_2 == LOW && value_3 == LOW){
value_1 = digitalRead(digitalInput_1);  
 value_2 = digitalRead(digitalInput_2);
  value_3 = digitalRead(digitalInput_3);  
ta = millis() - t;
}
if(value_2 == LOW){
  t13 = ta;
  }
   if(value_3 == LOW){
  t12 = ta;
  }
}
else if(value_2 == HIGH && value_1 == LOW && value_3 == LOW){
while(value_2 == HIGH && value_1 == LOW && value_3 == LOW){
value_1 = digitalRead(digitalInput_1);  
 value_2 = digitalRead(digitalInput_2);
  value_3 = digitalRead(digitalInput_3);  
tb = millis() - t;
}
if(value_1 == LOW){
  t23 = tb;
  }
   if(value_3 == LOW){
  t12 = -tb;
  }
}
else if(value_3 == HIGH && value_2 == LOW && value_1 == LOW){
while(value_3 == HIGH && value_2 == LOW && value_1 == LOW){
value_1 = digitalRead(digitalInput_1);  
 value_2 = digitalRead(digitalInput_2);
  value_3 = digitalRead(digitalInput_3);  
tc = millis() - t;
}
if(value_2 == LOW){
  t13 = -tc;
  }
   if(value_1 == LOW){
  t23 = -tc;
  }
}
// print out the variables preceeded by their IDs and followed by a carriage return
// (that allows us to know where the values end and when to expect the id)
   printByte(id_1);
   printInteger(t12);  printByte(10);
   
   printByte(id_2);
   printInteger(t13);  printByte(10);
   
    printByte(id_3);
   printInteger(t23);  printByte(10);
   
   delay(100);
     
}

unfortunately this doesn't work, that is, i get no data sent into processing!
does anybody have a clue about what is wrong in my use of while{} embedded into if{} conditions?
thanks for your help!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy