|
Page 1 of 1
|
[ 4 posts ] |
|
Define a Range of numbers, If Statements
Author |
Message |
fredknex
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 |
|
 |
mightor
Site Admin
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: If you want to do the same thing for any value between 10 and 5, you could do it like this: - 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 |
|
 |
MHTS
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: 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 |
|
 |
fredknex
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 |
|
|
|
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 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
|
|