Go Down

Topic: Stepper Motor Control with Arcade Joystick (Read 4 times) previous topic - next topic

TheBlommie

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

ash901226

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

Code: [Select]
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: [Select]
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.

TheBlommie

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.

ash901226


TheBlommie

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

Go Up