Pages: 1 [2] 3   Go Down
Author Topic: Stepper Motor Control with Arcade Joystick  (Read 3675 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Yes, I donwloaded the library to my Arduin/Libraries folder. Not sure why it won't compile though... Mmmm  smiley-confuse
Logged

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

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pm to me if you need any advice.
Logged

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

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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?

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

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

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hope to hear good news from you soon
Logged

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

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
Logged

Pages: 1 [2] 3   Go Up
Jump to: