ROBOTC.net forums
http://www.robotc.net/forums/

Sonar Sensor while loop
http://www.robotc.net/forums/viewtopic.php?f=1&t=7425
Page 1 of 1

Author:  Bluerope2013 [ Sat Dec 07, 2013 3:51 pm ]
Post subject:  Sonar Sensor while loop

Recently at my school, we have been working with the Sonar Sensor for Robot C. One idea that we came up with is to write the while loop in a different format. Instead of writing it like:
while(SonarSensor < 23);
we wrote it as:
while(1==1);
When we wrote the program like this the robot would run the program over and over again. Causing the robot to go on forever. My teacher and I were wondering if there is a program that would let us put the program like that, but we could tell how many times it would go around instead of going on forever. If there is anyone out there that can help me please send me a message explaining it or send me the program for it. Thank you and have a nice day.

- Bluerope2013

Author:  mattallen37 [ Sat Dec 07, 2013 4:07 pm ]
Post subject:  Re: Sonar Sensor while loop

You want a loop in the program that loops for a pre-defined number of times? One of many ways is to use a "for" loop, like this:
Code:
for(int i = 0; i < LOOPS; i++){

}

The equivalent with a "while" would be:
Code:
int i = 0;
while(i < LOOPS){
 
  i++;
}

Set you can replace "LOOPS" with either a constant or a variable, and it defines how many times to loop.

int is only 16 bits (IIRC ROBOTC doesn't support unsigned, so the limit is about 32 thousand), so you might need to use long which is 32 bits (where the limit is about 2 billion) for variable "i" (the counter).

Author:  mattallen37 [ Sat Dec 07, 2013 4:14 pm ]
Post subject:  Re: Sonar Sensor while loop

And BTW:
Code:
while(SonarSensor < 23);
would essentially cause the program to pause until SonarSensor >= 23. What you probably want is:
Code:
while(SonarSensor < 23){

}
where the code between the { and } gets repeated for as long as SonarSensor < 23.

And this:
Code:
while(1==1);
would cause the program to pause indefinitely.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/