Pages: 1 [2]   Go Down
Author Topic: Arduino Joystick Coding  (Read 1724 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 147
Posts: 9741
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
//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);
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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=(jlState*7);
  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
 
Logged

Pages: 1 [2]   Go Up
Jump to: