Go Down

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

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

ash901226

maybe you should see this line
Code: [Select]
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

ash901226

Code: [Select]
int potPin = 2; // Declaring int for Potentiometer on analog pin 0;
pin A2...

Code: [Select]
  pinMode(Pin1,INPUT);
  pinMode(Pin2,INPUT);
  digitalWrite(Pin1,HIGH);
  digitalWrite(Pin2,HIGH);


your using internal resistor?

if so then
Code: [Select]
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

Code: [Select]
if(valB1 && valB2){
    digitalWrite(pwm_cha, LOW);
    digitalWrite(pwm_chb, LOW);
    digitalWrite(brake_cha, HIGH);
    digitalWrite(brake_chb, HIGH);


it should work

Go Up