View unanswered posts | View active topics It is currently Wed Jul 23, 2014 6:15 am






Reply to topic  [ 4 posts ] 
joystick buttons problem 
Author Message
Rookie

Joined: Mon Nov 09, 2009 10:51 am
Posts: 23
Post joystick buttons problem
I am embarrassed to say I am using RobotC V 1.4 here, so maybe that is the problem, but I am just trying to do a quick project and not spend time on upgrading...
I'm just controlling a simple NXT vehicle with driving motors A and C and an arm motor B. The left and right joystick positions control the drive motors fine but only button 1 works, button 2 does not register at all in debug mode. I want button 1 to raise the arm and button 2 to lower it. I tried a couple other logic constructs and same thing, button 2 does not register, so I think it is a problem with how I am referencing the button. I think I am not understanding how to add another button so any help is appreciated.
Thanks!
Code:
#include "JoystickDriver.c"

task main()
{

   while (true)
  {
    getJoystickSettings(joystick);
    motor[motorA] = joystick.joy1_y1;
    motor[motorC] = joystick.joy1_y2;
    if(joy1Btn(1) == 1)
      {
         motor[motorB] = 20;
         wait1Msec(500);
         motor[motorB] = 0;
      }
     else if(joy1Btn(2) == 1)
     {
         motor[motorB] = -20;
         wait1Msec(500);
         motor[motorB] = 0;
     }
     else
     {
         motor[motorB] = 0;
     }
  }
}


Tue Oct 22, 2013 8:44 am
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: joystick buttons problem
Try removing the == 1 from your if-conditions.

So
Code:
if(joy1Btn(1) == 1)

becomes
Code:
if(joy1Btn(1))

etc

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Tue Oct 22, 2013 10:07 am
Profile
Rookie

Joined: Mon Nov 09, 2009 10:51 am
Posts: 23
Post Re: joystick buttons problem
Ah, boolean, should have known!
Thank you, works now.


Tue Oct 22, 2013 10:15 am
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: joystick buttons problem
Unnecessary explanation:
Assuming ROBOTC 1.4 works the same as more recent versions, the reason is that joystick buttons are stored as bit fields (button 1 is stored in first bit, button 2 is stored in the second bit, etc). The joy1Btn function then selects out the appropriate button, like:

Code:
short joy1Btn(int btn)
{
    return joystick.joy1_Buttons & (1 << (btn - 1);
}


This means that joy1Btn(2) == 2 when button 2 is pushed; it's not equal to 1 (joy1Btn(3) would equal 4). Since in C, any non-zero number evaluates to true, you don't have to do the comparison.

Recent versions of ROBOTC produce more consistent behavior by changing the definition of joy1Btn slightly:

Code:
short joy1Btn(int btn)
{
    return ((joystick.joy1_Buttons & (1 << (btn - 1))) != 0);
}


which produces a 1 when the button is pushed, regardless of the button.

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Tue Oct 22, 2013 11:30 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:  
cron



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