We have a basic joystick and don’t know how to program the buttons on it, could someone explain or link us to a tutorial?
P.S. Would it be legal to switch joystick ports in the middle of a competition?
Well, first off, what are you using EasyC or MPlab?
in MPLab, here’s a sample alias: p1_sw_trig
what this means:
“p1” is the port it’s plugged into
“sw” means it’s a switch (you always need
“trig” means the trigger button. There’s also “top” (which is the side button), aux1(the top left button), aux2 (the top right button), and wheel (the wheel, but i don’t know what it returns)
they all return a 1 or 0 depending on whether the button is pushed or not.
For EasyC… get someone else. I have only used MPLab
joystick related code is delt with in user_routines.c. Each button has it’s own variable. Joystick positions have two variables, 1 for the x directions and one for the y directions. The values for these positions are mapped like pwm values: 0 is full one direction, 127 means the joystick is in it’s neutral position, and 255 means the joystick is being pushed as far as it goes in the other direction. Because of the nature of the values, putting the line of code:
pwm01 = p1_y;
… would be legit, because when the joystick is just sitting there, it doesn’t move, and when the joystick is pushed forward/backward all the way, the motor goes as fast as it goes in the appropriate direction. This setup is a bit twitchy, because slight movements in the joystick gets responses from the motor.
Variable names are in the format joystick_port_name_button/axis.
This means p2_x is the variable dealing with the joystick plugged into the second control port, and the has to do with the x axis directions.
As for unplugging the joystick in the middle of a competion; don’t. It confuses the robot, and bad things happen :yikes: .
you can write a function to cancel out values near the middle range. we call ours, “DEADZONE”:
unsigned char Deadzone(unsigned char x){
if ( (x <= (127 + DEADZONE)) && (x >= (127 - DEADZONE)) ) // if variable is within DEADZONE, stop motor
return STOP;
else
return x;
}
you need to #define deadzone as something, around 15-20
just implement it:
p1_y = Deadzone(p1_y);
p1_x = Deadzone(p1_x);
p2_y = Deadzone(p2_y);
p2_x = Deadzone(p2_x);
p3_y = Deadzone(p3_y);