View unanswered posts | View active topics It is currently Sun Feb 07, 2016 2:58 am

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Greater than and less than in same command code
Author Message
Rookie

Joined: Mon Feb 03, 2014 11:34 am
Posts: 5
Greater than and less than in same command code
Is it possible write a command that will read a range of values that are less than a certain amount and greater than a certain amount in the same line of code. In other words, I wanted a servo to move to a position if the sonar was reading less than 20 inches but greater than 10 inches. Above that range the servo would go to one position and below that range the servo would set to another position.
Thanks

Tue Feb 04, 2014 12:23 pm
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
Re: Greater than and less than in same command code
 Rod wrote:Is it possible write a command that will read a range of values that are less than a certain amount and greater than a certain amount in the same line of code. In other words, I wanted a servo to move to a position if the sonar was reading less than 20 inches but greater than 10 inches. Above that range the servo would go to one position and below that range the servo would set to another position.Thanks

You actually have most of the psuedocode already taken care of, which is good:

 Quote: if the sonar was reading less than 20 inches but greater than 10 inches

To convert this to real code, we will first convert it to psuedocode:

 Code:if (sonar reading is less than 20 and sonar reading is greater than 10)   {      //Move servo to position 1   }

Now that we have the psuedocode, we can start filling in with actual command lines. To read the sonar value, we will use the SensorValue[] command. Less than a value will use the less than symbol <, while greater than will use the greater than symbol >. Finally, since we need the value to be both less than 20 AND greater than 10, we will use the AND operator &&.

 Code:if (SensorValue[sonar] < 20 && SensorValue[sonar] > 10)   {      //Move servo to position 1   }

Finally, we need to decide what happens if the sonar value is greater than the highest value (20) and what happens if the sonar value is less than the lowest value (10). We can use the "else if" conditions to check for these other conditions

 Code:if(SensorValue[sonar] < 20 && SensorValue[sonar] > 10)   {      //Move servo to position 1   }else if(sonar reading is greater than 20)  {     //Move servo to position 2   }else if(sonar reading is less than 10)   {      //Move servo to position 3   }

There is one more, unique condition that you will have to check for. When a Cortex sonar sensor is first initialized or when it receives no echo back (if there are no objects in range), it will return a value of -1. You will need to check for this in the 'greater than 20' else if using the OR operator || (if the value of the sonar is greater than 20 OR -1, move to position 2). To compare for an exact value (EQUAL TO), you will need to use the double-equal signs comparator, ==.

 Code:else if(sonar reading is greater than 20 || sonar reading is equal to -1)  {     //Move servo to position 2   }

Let us know if you have any other questions, and be sure to take a look through the VEX Cortex Video Trainer for more help on how to use the boolean operators in if/else statements (specifically, under the Movement -> Automated Straightening section, but it's always good to go through the entire curriculum if you need a refresher): http://www.education.rec.ri.cmu.edu/pro ... index.html

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our updated help documentation and the ROBOTC Forums.

Tue Feb 04, 2014 12:46 pm
Rookie

Joined: Mon Feb 03, 2014 11:34 am
Posts: 5
Re: Greater than and less than in same command code
Thank you John for your help. We will give that a try today. I have another question but I will put it in another post.

Fri Feb 07, 2014 9:23 am
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    2014 Robotics Academy Summer of Learning    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

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