Stepper Motor Control with Arcade Joystick

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

pm to me if you need any advice.

Hi Ash

Can you show me on my last connection diagram how to connect the second button? I only did the connection for one button, but I guess the second button will also have a transistor and my logic tells me that somehow some button pins will have to be connected as well...

Please advice.

Regards

Ian

Ian,
For connecting button there are 2 way to connect multiple button to the arduino, first is the digital way if you have enough port for it. second is by using a resistor ladder and switch set up that is kinda hard to explain unless theres a diagram.
anyway for your setup, just you the first type since you have more then enough port for your project .
anyway for button 2 you need to make another set of the same connection as you did for the first button, now the middle bit between the resistor and the switch is put to port 4

Hey Ash

Good news, I connected the second button of the arcade joystick and with a little bit of trial and error I got it working. However, I connected the wires slightly different from the way you mentioned, but it is still working just fine.

Now, since I decided to use the arcade joystick for rotation control, I need to be able to set a variable speed for the motor by using a potentiometer. I'll connect the pot to an analog input... What do I do code-wise? I think this will be the last scope of the project and then I can start with a box design for everything. As time goes on I'll think about connecting a LCD screen for visual speed references to show what the speed is while turning the pot. But for now it is not a real concern to me.

I'll appreciate your help my friend! In theory I know that the same mapping method we used for the thumb joystick will be used to determine what the value of the pot is and then this value will be used for the speed of the motor, right? But practical I'm still learning the ropes, hehe.

Thanks

Ian

i can ssay that the sam eidea apply but now the mapping is different ....

Hey Ash

I gave it a go in the code and here is what I though it should look like in theory:

#include <Bounce.h>
#include <Stepper.h>

const int num_step = 40;

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 potPin = 0; // Declaring int for Potentiometer on analog pin 0;
int spr=10;

Stepper stepper(num_step, dir_cha, dir_chb);
Bounce Bounce1 = Bounce(Pin1,2);
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(potPin, INPUT); // Declaring Potentiometer as INPUT
pinMode(Pin1,INPUT);
pinMode(Pin2,INPUT);
digitalWrite(Pin1,HIGH);
digitalWrite(Pin2,HIGH);
}

void loop()
{
int potValue = analogRead(potPin); // Declare potentiometer value
int motorSpeed = map(potValue, 0, 1023, 0, 200); // Declare a motor speed value based on pot mapping

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(motorSpeed); // Set speed to mapped motorSpeed
}

if(!valB1 && valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
stepper.step(-spr);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
}

if(!valB1 && !valB2){
digitalWrite(pwm_cha, LOW);
digitalWrite(pwm_chb, LOW);
}

}

Do you think it will work? I haven't tested it yet, need to get a proper potentiometer first. But I guess if the code logic is right then it should work when everything is connected and wired up, right?

Please advice.

Cheers

in theory yes it could work, i have a few think i would like you to clarify about

Bounce Bounce1 = Bounce(Pin1,2);
Bounce Bounce2 = Bounce(Pin2,5);

why is it that you use 2 ms for your bounce duration for pin one where as you use 5ms for the second switch?

int motorSpeed = map(potValue, 0, 1023, 0, 200);

why only 200 and not 255 as max?

other then that i guess i have nothing to complain about haha. Just joking. Wow you are able to make your own program and for that i solute you.

As for the bounce, I wasn't actually sure what the lines meant until now. Thanks for clearing that up for me, hehe. I thought thet the 2ms and 5ms you're talking about was just another pin declaration. No wonder I'm still a newbie, hehe. I'll change the 2ms to 5 as well then.

As for the speed, my stepper can only do 200 and not 255. Anything above 200 and my motor starts to get jittery. I must say, the arcade joystick together with the potentiometer will provide me with just the solution that I need. Set speed with pot, rotate motor with joystick. It works great!

But thanks for the compliment! By now I should be able to put together some sort of code, even though there might be some errors, hehe.

Thanks for confirming the code. I'll test it practically.

hope to hear good news from you soon

Hey Ash

Okay, so I've tried a practical test with the code below, but nothing is happening apart from the stepper motor doing some random steps.

#include <Bounce.h>
#include <Stepper.h>

const int num_step = 40;

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 potPin = 0; // Declaring int for Potentiometer on analog pin 0;
int spr=10;

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(potPin, INPUT); // Declaring Potentiometer as INPUT
pinMode(Pin1,INPUT);
pinMode(Pin2,INPUT);
digitalWrite(Pin1,HIGH);
digitalWrite(Pin2,HIGH);
}

void loop()
{
int potValue = analogRead(potPin); // Declare potentiometer value
int motorSpeed = map(potValue, 0, 1023, 0, 200); // Declare a motor speed value based on pot mapping

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(motorSpeed); // Set speed to mapped motorSpeed
}

if(!valB1 && valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
stepper.step(-spr);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
}

if(!valB1 && !valB2){
digitalWrite(pwm_cha, LOW);
digitalWrite(pwm_chb, LOW);
}

}

Can you see something wrong?

Regards

Ian

Ash, I'm not sure why, but it seems to be working now. Strange, but anyway.

maybe you should see this line

int potPin = 0; // Declaring int for Potentiometer on analog pin 0;

like the one b4 you should use A2.
Sorry i miss it the first time i proof read your code

Thanks Ash. But it seems to be working now on pin A2. So now the project is complete and I can start with a design for a box to enclose everything. Will send you some photos when I'm done.

Thanks for your help and patience with me.

Regards

Ian

Hey Ash

I hope you are good!

Listen, I need your help one more time... When I use the potentiometer to determine the speed variable, the joystick and everything else works perfectly. However, when the joystick is not moved, in other words, when a micro switch is not pressed in and the joystick is in the center, the motor stops, right.... Now, when it stops, it doesn't have a break. So now when the joystick is not moved, the motor's shaft can still be turned with my fingers which is not entirely what it should do. It shouldn't move and must still have some sort of break on it...

In the code below there is a function to check to whether one or both buttons are not pressed and then in that function I set the break on HIGH. It doesn't seem to be working though. Maybe I'm missing something? It also seems as if the power to the motor is off when it is not rotating while using the joystick.

#include <Bounce.h>
#include <Stepper.h>

const int num_step = 40;

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 potPin = 2; // Declaring int for Potentiometer on analog pin 0;
int spr=10;

Stepper stepper(num_step, dir_cha, dir_chb);
Bounce Bounce1 = Bounce(Pin1,10);
Bounce Bounce2 = Bounce(Pin2,10);

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(potPin, INPUT); // Declaring Potentiometer as INPUT
pinMode(Pin1,INPUT);
pinMode(Pin2,INPUT);
digitalWrite(Pin1,HIGH);
digitalWrite(Pin2,HIGH);
}

void loop()
{
int potValue = analogRead(potPin); // Declare potentiometer value
int motorSpeed = map(potValue, 0, 1023, 80, 350); // Declare a motor speed value based on pot mapping

Bounce1.update ( );
Bounce2.update ( );

int valB1=Bounce1.read();
int valB2=Bounce2.read();

if (valB1 && !valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
stepper.step(spr);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
}

if(!valB1 && valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
stepper.step(-spr);
}

if(!valB1 && !valB2){
digitalWrite(pwm_cha, LOW);
digitalWrite(pwm_chb, LOW);
digitalWrite(brake_cha, HIGH);
digitalWrite(brake_chb, HIGH);
}

}

Any ideas?

Cheers

int potPin = 2; // Declaring int for Potentiometer on analog pin 0;

pin A2...

  pinMode(Pin1,INPUT);
  pinMode(Pin2,INPUT);
  digitalWrite(Pin1,HIGH);
  digitalWrite(Pin2,HIGH);

your using internal resistor?

if so then

 if(!valB1 && !valB2){
    digitalWrite(pwm_cha, LOW);
    digitalWrite(pwm_chb, LOW);
    digitalWrite(brake_cha, HIGH);
    digitalWrite(brake_chb, HIGH);

never valid.

when you use the internal resistor, it mean that port will always be high, so when joystick at middle, both valB1 and valB2 is actually high not low as in the program suggest.
just change that part to this

 if(valB1 && valB2){
    digitalWrite(pwm_cha, LOW);
    digitalWrite(pwm_chb, LOW);
    digitalWrite(brake_cha, HIGH);
    digitalWrite(brake_chb, HIGH);

it should work