Stepper Motor Control with Arcade Joystick

Hi

I recently asked for help and advice on controlling a stepper motor's rotation with a thumb joystick and thanks to forum users "noisyvoid" and "ash901226" I got enough help to setup my project successfully. However, I now want to enclose the project and bought an arcade joystick to replace the thumb joystick. When I got the joystick I saw that it was not a potentiometer joystick, but a relay switch joystick instead. Since I'm still learning to get the hang of things, I'm not sure how the code will have to affected in order for the joystick to work with my current code...

Here is the code:

#include <Stepper.h>
#include <Servo.h>

const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = 2;
int val;
Servo myservo; // create servo object to control a servo
Stepper stepper(num_step, dir_cha, dir_chb);

void setup()
{
Serial.begin(9600);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);

myservo.attach(2);
}

void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{ // fill in with the one you need to make it brake
}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(S); //this set the direction and the number of step it would take
Serial.print("Forward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(-S); //this set the direction and the number of step it would take
Serial.print("Backward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}

// This next bit just outputs the pot position to a servo
val = map(PVal, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}

I need to know what the new code will look like using the relay switches. The above code worked on a mapping for the thumb joystick and as the joystick moved further away from the center pin, the stepper motor rotated faster and the other way around. Now, with the new relay switch joystick, I'm not sure if this function can still be the same since it is only a switch. So now the code must be amended in such a way that when a relay switch is pressed with the joystick, the motor must rotate in one direction and when the joystick is not pressed and goed back to its default state (center) the motor must stand still. This however will affect the way in which I control the speed since I'll have to use a normal pot to set the speed and then use that as the speed for the motor.

Attached are two pictures of the joystick that I'll be using. The relay at the bottom is just for the switch button on top of the joystick. I don't think I'll be using it.

I also need to know how to physically connect the wires from the joystick to the Arduino.

I hope this makes enough sense for someone who can help me. Like I said, code wise I'm still very dum and learning.

Regards

TheBlommie

joystick_02.jpg

joystick_01.jpg

well ok, i seen your arcade joystick b4, i made one for a robot control during my high school years, though it is not as pretty looking as this one. this one a babe. haha
anyway for your information this type of joystick is only a regular push button arrange in a way that allow the use of a stick to trigger it. IMHO, this is a 3 axis joystick since it can sense the movement in the x and y axis and a button trigger on top for the third axis. anyway for this set up, you cant control the speed as you do with the pot type joystick. in term of programming and the wire connection between the board and the joystick must all be change to accommodate the different type of joy stick. i presume that you still want to change the speed, but it must use a much different way of making it work.

in term of wire connection is that it must be digital, and you must have a resistor to pull it down to ground, or you could use the internal pull up resistor, but that is kinda different. you need to wire atleast 2 in the opposing direction to get the left and right direction.

for the programming, lets say you are ok with only to speed you could do is if on of the directional switch on while the other off then it go fast / slow which ever you prefer, then if you want it to go fast/ slow then you can use middle button as a trigger that process.

but if you really want a better speed control then use the middle button to trigger the speed but instead measure the amount of time the button is press. then map that so that you could have speed control.

how ever if you would like to continue with you idea of using the pot and joystick then the programming part would be very trivial.

I would like to continue with the arcade joystick. I'll worry about setting the speed at a later stage. For now the joystick must be able to trigger the motor when pushing it left or right and so the respective micro switches must toggle movement. When the joystick is in the center, the motor must stop. So basically the micro switch will only do something when pressed in and when released the motor should stop.

How will the code then look for this to work?

Thanks

Hi Ash

You mentioned using a transistor when connecting the switch to the arduino... I connected the COM of the switch to the 5V and the NO to D2 pin on the arduino... Is this correct or not? I don't have a breadboard, so I need to know how to physically connect my wires directly from the joystick to the arduino.

Please advice.

Thanks

did i mention about transistor? if i am not mistaken, i did only say about resistor... anyway ok in that case just put common in of your switch to ground and the normally open to port 2 while the other switch to port 3.

like always debounce your input. you don't want it to chatter. remember to activate the internal resistor by putting

 digitalWrite(Pin,HIGH);

into your setup.

controlling the direction is as easy as it can be.
just use the if statement.

if ( Pin2==LOW){stepper.step(spr);stepper.setSpeed(400);}
if ( Pin3==LOW){stepper.step(-spr);stepper.setSpeed(400);}
if ( Pin2==HIGH &&Pin3==HIGH){} /* fill inside the curly bracket with something that make the motor brake.*/

Hey Ash

Thanks for the code... I tried to incorporate it into the code, but had no luck. Maybe you can show me where exactly in the code that I posted should I put in your code?

Thanks man

basically, the whole program need to be redo since the hardware have been change.
go to this Arduino Playground - Bounce and download the library.
change the for the switch from port 3 to port 4 cause port 3 is use to control the stepper pwm_cha.

#include <Bounce.h>
#include <Stepper.h> 
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;
const int Pin1=2;
const int Pin2=4;

Stepper stepper(num_step, dir_cha, dir_chb);
Bounce B1 = Bounce( Pin1,5 );
Bounce B2 = Bounce(Pin2,5); 
void setup()
{
    Serial.begin(9600);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
    pinMode(Pin1,INPUT);
    pinMode(Pin2,INPUT);
    digitalWrite(Pin1,HIGH);
    digitalWrite(Pin2,HIGH);
 }
 
void loop()
{
B1.update ( );
B2.update ( );
int ValB1=!B1.Read();
int ValB2=!B2.Read();
if (ValB1 &&!ValB2)
{digitalWrite(pwm_cha, HIGH);digitalWrite(pwm_chb, HIGH);
stepper.step(spr);stepper.setSpeed(400);}
if(!ValB1&&ValB2)
{digitalWrite(pwm_cha, HIGH);digitalWrite(pwm_chb, HIGH);
stepper.step(-spr);stepper.setSpeed(400);}
if ( !ValB1 && !ValB2){digitalWrite(pwm_cha, LOW);digitalWrite(pwm_chb, LOW);}
}

Hi Ash

I get these errors when I try to upload the sketch to the arduino:

Bounce_Example:14: error: expected unqualified-id before numeric constant
Bounce_Example.ino: In function 'void loop()':
Bounce_Example:36: error: request for member 'update' in '1', which is of non-class type 'int'
Bounce_Example:39: error: request for member 'Read' in '1', which is of non-class type 'int'
Bounce_Example:40: error: 'class Bounce' has no member named 'Read'
Bounce_Example:46: error: 'spr' was not declared in this scope
Bounce_Example:53: error: 'spr' was not declared in this scope

I did donwload the library to my Arduino folder's libraries folder under ..\Bounce\Bounce.h

Here is the code as I tried to use it:

#include <Bounce.h>
#include <Stepper.h>
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;
const int Pin1=4;
const int Pin2=5;

Stepper stepper(num_step, dir_cha, dir_chb);
Bounce B1 = Bounce(Pin1,5);
Bounce B2 = Bounce(Pin2,5);

void setup()
{
Serial.begin(9600);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
pinMode(Pin1,INPUT);
pinMode(Pin2,INPUT);
digitalWrite(Pin1,HIGH);
digitalWrite(Pin2,HIGH);
}

void loop()
{
B1.update ( );
B2.update ( );

int ValB1=!B1.Read();
int ValB2=!B2.Read();

if (ValB1 &&!ValB2)
{
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
stepper.step(spr);
stepper.setSpeed(400);}

if(!ValB1&&ValB2)
{
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
stepper.step(-spr);
stepper.setSpeed(400);}

if(!ValB1 && !ValB2)
{
digitalWrite(pwm_cha, LOW);
digitalWrite(pwm_chb, LOW);}
}

Any ideas what the problem might be?

Ash, I connected my micro switch as indicated in the attached drawing. I know everyone says that a 10k pull down resistor should be in the connection as well, however, I'm not using a breadboard so therefore I physically need to know how to connect the resistor within my current connection setup.

From the switch pins I connect to the Arduino as follows:

Switch COM to 5V on Arduino
Switch NO to D4 on Arduino

I appreciate your help very much!

Thanks

Switch.jpg

about the programming. i will do it for you later. for the connection part its just
gnd to resistor leg one then the other leg to NO.The C should be to 5V the middle connection between the resistor and the switch need to have a wire to the port you want to use.
resistor switch
gnd---///--------/-------5V
_________ |
port # |---|

hope you understand this ascii art.

Hi Ash

No problem. Take your time with the code. You are my sidekick, hehe. Okay, I think I understood the acii drawing. Have a look at my drawing and tell me if I have it right?

Thanks a mil!

Switch_02.jpg

Yup that is about rite.

Cool stuff! I'll be waiting for the code and then I'll have to experiment.

Thanks Ash

try this one, it can compile.

#include <Bounce.h>
#include <Stepper.h> 
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;
const int Pin1=2;
const int Pin2=4;
int spr=100;

Stepper stepper(num_step, dir_cha, dir_chb);
Bounce Bounce1 = Bounce(Pin1,5);
Bounce Bounce2 = Bounce(Pin2,5); 
void setup()
{
    Serial.begin(9600);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
    pinMode(Pin1,INPUT);
    pinMode(Pin2,INPUT);
    digitalWrite(Pin1,HIGH);
    digitalWrite(Pin2,HIGH);
 }
 
void loop()
{
Bounce1.update ( );
Bounce2.update ( );
int ValB1=Bounce1.read();
int ValB2=Bounce2.read();
if (ValB1 &&!ValB2)
{digitalWrite(pwm_cha, HIGH);digitalWrite(pwm_chb, HIGH);
stepper.step(spr);stepper.setSpeed(400);}
if(!ValB1&&ValB2)
{digitalWrite(pwm_cha, HIGH);digitalWrite(pwm_chb, HIGH);
stepper.step(-spr);stepper.setSpeed(400);}
if ( !ValB1 && !ValB2){digitalWrite(pwm_cha, LOW);digitalWrite(pwm_chb, LOW);}
}

Hi Ash, I'm starting to think that maybe there is a problem with the library because I get the following errors with the new code:

Stepper_Arcade_Joystick_Control_FINAL.cpp.o: In function __static_initialization_and_destruction_0': C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:15: undefined reference to Bounce::Bounce(unsigned char, unsigned long)'
C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:16: undefined reference to Bounce::Bounce(unsigned char, unsigned long)' Stepper_Arcade_Joystick_Control_FINAL.cpp.o: In function loop':
C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:36: undefined reference to Bounce::update()' C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:37: undefined reference to Bounce::update()'
C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:38: undefined reference to Bounce::read()' C:\Users\Ian\Desktop\arduino-1.0.2/Stepper_Arcade_Joystick_Control_FINAL.ino:39: undefined reference to Bounce::read()'

Please advice.

Thanks

did you download the library and save inside your library folder for arduino?

Yes, I donwloaded the library to my Arduin/Libraries folder. Not sure why it won't compile though... Mmmm :~

Wait, I deleted the library and re-installed it again and now it seems to be working.

Ash, thanks a million for all your advice and help. I'm gonna do a practical hands-on and see if I can manage. First I must get the connection right with the resistors etc and then hopefully the code will work. If I do struggle, I'll let you know. But thanks again.

I appreciate it!

Ian

finally i got your name Ian. HI I'm Ash from Malaysia. Haha its interesting that you are doing this project, can you tell me much more about your project? really would like to see you make it.

Hehe, yes, my name is Ian and I'm from South Africa. Glad to meet you. I'll keep you updated on the progress of the rig that I'm busy with.

But thanks again for your help and advice. I'll let you know if I run into problems with the code.

Cheers