I have been using your problem to learn about the stepper board. And the problem you are having is due to the Shield using A0 as current sensing. Move the Pot to A2 and amend the code accordingly.
Secondly, the mapping are slightly wrong - the last if statement needs to be reversed.
Finally, I have improved the response time when in the slow speed by mapping the number of steps to the speed.
I have put some prints in to look at the values and also there is some servo stuff to show the position of the pot - this can all be removed
regards,
James
#include <Stepper.h>
#include <Servo.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;
int Pot1 = 2;
int val;
Servo myservo; // create servo object to control a servo
Stepper stepper(num_step, dir_cha, dir_chb);
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);
myservo.attach(2);
}
void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{ // fill in with the one you need to make it brake
}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(S); //this set the direction and the number of step it would take
Serial.print("Forward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(-S); //this set the direction and the number of step it would take
Serial.print("Backward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
// This next bit just outputs the pot position to a servo
val = map(PVal, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}