View unanswered posts | View active topics It is currently Wed Jul 23, 2014 2:39 pm






Reply to topic  [ 4 posts ] 
Define a Range of numbers, If Statements 
Author Message
Rookie

Joined: Sat Nov 27, 2010 9:26 pm
Posts: 2
Post 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
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3162
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Rookie

Joined: Sat Nov 27, 2010 9:26 pm
Posts: 2
Post 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 :bigthumb:


Sun Nov 28, 2010 3:33 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 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

Search for:
Jump to:  



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