View unanswered posts | View active topics It is currently Mon Jun 18, 2018 12:10 am

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Define a Range of numbers, If Statements
Author Message
Rookie

Joined: Sat Nov 27, 2010 9:26 pm
Posts: 2
Define a Range of numbers, If Statements
hi, is there a way to define a range of numbers in robotc? In my case 0-10.
Currently I just have a separate If statement for each number, 10...9...8...7...6..5..

if (abs(joystick.joy2_y2) == 10) // If the right analog stick's Y-axis readings for JOYPAD-2 are above the absolute value (10)

{
motor[ramp] = 0; // Ramp Motor is assigned a power level equal to JOYPAD-2 right analog stick's Y-axis reading
}

that for EVERY digit.
it works but seems inefficient to me.

instead i would like to just have one value to represent all the numbers between 0 and 10, in one If statement

thanks in advance for any help or ideas!

-- the reason for needing this are different If statements for when the joystick is above 10 and when it is below -10, and an Else statement stopping the motors causes a conflict.

Sat Nov 27, 2010 9:46 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Define a Range of numbers, If Statements
If you want to do something different for each individual value between 10 and 5, you could do it with a switch statement like this:
 Code:task main () {  int someValue;  switch (someValue) {    10: performFunction10();        break;     9: performFunction9();        break;     8: performFunction8();        break;     7: performFunction7();        break;     6: performFunction6();        break;     5: performFunction5();        break;             default: doNothing();        break;        }}

If you want to do the same thing for any value between 10 and 5, you could do it like this:
 Code:task main () {  int someValue;    if (someValue >= 5 && someValue <= 10) {    // doSomething();  }}

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sun Nov 28, 2010 3:20 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Define a Range of numbers, If Statements
I am not sure if I understand what you are asking. If you are trying to do something when the y2 value of joystick2 is within the range of -10 to 10, you can do the following:
 Code:int y2 = abs(joystick.joy2_y2);if ((y2 >= 0) && (y2 <= 10)){    // do something here.}

But it sounds like you are trying to implement Deadband (i.e. don't run the motors if the absolute value of y is within the deadband range -10 to +10). If that's what you meant, then you can do this:
 Code:#define DEADBAND(n,t)           ((abs(n) > (t))? (n): 0)#define NORMALIZE(n,sl,sh,tl,th) (int)(((long)(n) - (sl))*((th) - (tl))/((sh) - (sl)) + (tl))//// y will be zero if it is within the deadband -10 to 10.//int y = DEADBAND(joystick.joy2_y2, 10);//// NORMALIZE will transform the joystick range of -128 to 127 to the motor range of -100 to 100.//motor[ramp] = NORMALIZE(y, -128, 127, -100, 100);

Sun Nov 28, 2010 3:25 am
Rookie

Joined: Sat Nov 27, 2010 9:26 pm
Posts: 2
Re: Define a Range of numbers, If Statements
wow that was fast, thanks!

deadband thing is nice to know about, but just the "&&" is perfect i think

thanks for the help again

Sun Nov 28, 2010 3:33 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### 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

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