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

JohnWatson

Site Admin

Joined: Thu May 24, 2012 12:15 pmPosts: 663

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 &&.

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

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, ==.

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

Who is online

Users browsing this forum: No registered users and 2 guests

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