View unanswered posts | View active topics It is currently Tue Jun 28, 2016 8:17 pm

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Sonar Sensor while loop
Author Message
Rookie

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

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

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 3 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning