Arduino Joystick Coding

For my senior design project, I need to modify a go-kart for our client. Our client has cerebral palsy and cannot drive a normal go-kart; therefore we need to make this go-kart joystick accessible. We are in the process of connecting an 8-way joystick to the Arduino Due in hopes of the following:

  • Movement in positive y will accelerate
  • Movement in negative y will brake
  • Movement in positive x/y will turn the wheels right/left

3 pins of the Arduino are then outputted to Motor Driver which is connected to 3 actuators to power the acceleration, braking, and steering. Our goal is when he moves the joystick in any direction, the Arduino will read the input and supply a voltage to the driver which will then power the corresponding actuator. My group for some reason cannot get past the coding though. We are novices with the Arduino coding as this is our first time working with this microprocessor. We normally would not ask a question this advanced, but since this is going to a good cause we are hopeful that you might be able to help. If not, is there anyone you would recommend talking with.

Reading material

https://www.google.com/search?hl=en&as_q=joystick&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Farduino.cc%2Fforum%2F&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

If that doesn't help, post what code you have. Hopefully you are reading the joystick X, Y and Z values.

It would also help to post a link to the joystick in question, and the motors. We can't read minds you know.

Read this before posting a programming question

Thank you for your help, sorry for not posting the objects being used

Arduino Due as the microprocessor
8-way universal joystick UNIVERSAL JOYSTICK WITH MICROSWITCHES BLACK KNOB - 50-6048-10
Using a motor driver - VNH5019 Motor Driver Carrier

As of now, our code is not working at all. We would like to start over at best due to the fact that we are very inexperienced

Connect the joystick pots to the arduino A0 and A1 lines, then just do analogRead to get their values. If the joystick is self-centering, then the initial reading is your center. Moving one way will increase the reading and moving the other will decrease the reading. Compare these against the initial reading to determine the direction.

However, I am not sure how you are going to control three actuators with just three pins when you need forward and reverse for each motor for drive and steering. For steering, you will need two pins (and possibly an enable signal) to do forward/reverse of the motor. For the acceleration and braking, you will need 2 pins (and possibly an enable) for forward/reverse. You could probably get away with just one line for braking, though. If you use the electronic brake on most H-Bridge circuits, you just reuse the forward/reverse lines (setting them both HIGH at the same time) to brake.

How will you be controlling speed? Through PWM or using a commercial ESC (electronic speed controller?)

The code is very simple.

If X is greater than 0, go forward.
Else if X is less than 0, go backward.
Else stop

Same with Y.

Usual joysticks are analog which means they have a measured value between 0 and 1023.
So half of 1023,=> 512, would be your "0", so anything greater than 512 would go forward.
Less than, reverse, and is 512, stop.

A bit dangerous to have a stick control both turning and acceleration/brake. If someone pushes the stick to a corner, it's gonna be a bad day (accelerating and turning with almost full strength at the same time).

Post your code.

No cross posting!

I recommend that you make the steering control speed sensitive, with steering authority reducing as the speed rises. Also consider designing the speed control so that 'full throttle' ramps the power up progressively rather than slamming you into full power. (I guess there will be plenty of cross-talk between steering and throttle inputs when it's being used by somebody with poor limb control.)

If you're planning to use a conventional IC gokart then the controls are likely to be quite heavy so you'll need a substantial power supply. Make sure you consider what happens if the supply fails or the battery goes flat while the kart is in motion. Also I'd think that safety might be a major concern - have you addressed that aspect thoroughly?

Cross posting is bad! Ask everyone to forgive you and admin to combine posts!

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

... we are very inexperienced ...

Are you inexperienced in general forum etiquette, too?

Threads merged.

  • Moderator

Ahh, now I see that you are using a digital joystick. So, forget the analog pins. You will need to manually increase and decrease acceleration based on how long the joystick is pushed forward/backward. Basically a timer that checks the switch for its state and if it is still low, it will increase/decrease the speed. How that is done depends on how you are controlling speed (ESC, H-bridge with PWM?)

And I agree with the poster above that using the same joystick for both steering and speed could be dangerous. A wheelchair does this, yes, but it is using two wheels and going rather slow. I do assume that your disabled client knows how to use these controls, though since that IS how a wheelchair is operated. Just understand that at high speeds, there is going to be jerking of the rider going on, which may cause unwanted pushes of the joystick.

You can counteract that in the control scheme mentioned above. Check the state of the switches on timer. Then if a sudden bump happens, it will not accelerate/decelerate. Only when the stick is continually held in a direction will it respond.

If you are looking for analog joysticks, the below might be of interest.

http://www.ebay.com/itm/3-X-New-Handle-Joystick-3D-for-Sony-Playstation2-PS2-PS-2-10V-/230803703512?pt=LH_DefaultDomain_0&hash=item35bcf906d8

stevekap25, Does the go-cart have automotive-type steering? If so, can your client use the steering wheel and use the joystick for throttle and brakes?

An update to the design we're now working with involves using the joystick to control both acceleration and braking only. We will still be using the previously mentioned components for the system (actuators and drivers) in order to control the throttle and braking of the kart. We ARE NOT using a potentiometer joystick, but one with simple activation switches when pressed either up, down, left, or right.

With that said, ideally, we want the actuators to be enabled and extend when pressed either up or down (throttle or brake) and retracted whenever the joystick is placed back into its natural, neutral position. This is where we would appreciate any help with the coding aspect since we are unsure of how to enable and retract the actuators depending on which position the joystick is in.

Thanks

stevekap25, Does the go-cart have pedals for throttle and brakes? If so, you can add a "joystick" connected to the pedals. The linkages between the "joystick" and pedals would be made so that when the client pushes the "joystick" forward, the linkage would push the throttle pedal forward, making the go-cart run. When the client pulls the "joystick" backward, the linkage would push the brake pedal forward, stopping the go-cart. The linkage would have sliding joints so that one pedal is pushed forward at a time.

With that said, ideally, we want the actuators to be enabled and extend when pressed either up or down (throttle or brake) and retracted whenever the joystick is placed back into its natural, neutral position.

Code for simple "button" (or switch) operation.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

I know this is not timely, but i stumbled on this post while trying to solve the issue of using a digital (four microswitch type) joystick for a similar project. the issue I had was isolating the diagonal values when you use two switches. What I did was assign each button a number then sum the four buttons together to get a single value. Then I can use that vale to trigger the motors.

// Joy Stick Test
#define ju 6 // up Pin, (Pushes lower switch when the joystick is up.
#define jr 7 //Right Pin (jr for joystick right)
#define jl 8 //Left Pin
#define jb 9 //Back Pin
#define LED 13// onboard LED

int u=0;// Variable for up button
int r=0;//Variable for right button
int l=0;//Variable for left button
int b=0;//Variable for back button

int juState=LOW;//Variable for up button state
int jlState=LOW;//Variable for left button state
int jrState=LOW;//Variable for right button state
int jbState=LOW;//Variable for up button state

int Action=0;//Variable for up the sum of the u+r+l+b
int OldAction=0;//Variable to compare changes in the button state
int motion=0;//Variable used to say what the motors will do, the hope is that the motors will run uninterrupted until the old action changes.

void setup()
{
pinMode (ju, INPUT);//up
pinMode (jr, INPUT);//right
pinMode (jl, INPUT);//left
pinMode (jb, INPUT);//back
pinMode (LED, OUTPUT);
Serial.begin(9600);//set serial comunication for 9600
}
void loop(){
//read the button states
juState=digitalRead(ju);
jrState=digitalRead(jr);
jlState=digitalRead(jl);
jbState=digitalRead(jb);

/the joystick button state will return a 1 for high or a 0 for low.
multiply that times the asigned value for the button.
example:if the joystick button is high, then 1x5=5; if it's low then 0x5=0
/
u=(juState
5);
r=(jrState
1);
l=(jlState7);
b=(jbState
3);

Action = (u+r+l+b);//by adding button variable together Action will contain 1 of 9 values(5,1,7,3,12,6,4,10,0)
Serial.println (Action); //Serial print the sum of u+r+l+b

OldAction=Action;//compare changes in the button state

if (OldAction != motion){ //the hope is this will give the stepper motors an uninterrupted variable to run off of until the button state changes.
motion = OldAction;}

if (motion==5){
Serial.println ("Forward");
}

if (motion==6){
Serial.println ("Forward Right");
}

if (motion==12){
Serial.println ("Forward Left");
}

if (motion==3){
Serial.println ("Back");
}

if (motion==4){
Serial.println ("Back Right");
}
if (motion==10){
Serial.println ("Back Left");
}

if (motion==1){
Serial.println ("Righ");
}

if (motion==7){
Serial.println ("Left");
}

if (motion==0){
Serial.println ("Stop");
}

}//End of Void Loop