View unanswered posts | View active topics It is currently Mon Sep 01, 2014 8:11 pm






Reply to topic  [ 3 posts ] 
Sonar Sensor while loop 
Author Message
Rookie

Joined: Tue Sep 24, 2013 10:16 am
Posts: 3
Post 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


Sat Dec 07, 2013 3:51 pm
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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).

_________________
Matt


Sat Dec 07, 2013 4:07 pm
Profile WWW
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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.

_________________
Matt


Sat Dec 07, 2013 4:14 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.