Stepper Motor Control with Arcade Joystick

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

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