View unanswered posts | View active topics It is currently Sun Dec 21, 2014 2:15 am






Reply to topic  [ 3 posts ] 
Greater than and less than in same command code 
Author Message
Rookie

Joined: Mon Feb 03, 2014 11:34 am
Posts: 5
Post 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
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 619
Post 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 Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue Feb 04, 2014 12:46 pm
Profile
Rookie

Joined: Mon Feb 03, 2014 11:34 am
Posts: 5
Post 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
Profile
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.