Stepper Motor Control with Arcade Joystick

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