Hi guys,
I am trying to control a stepper motor using a potentiometer. Everythibg seems fine but the problem is that the potentiometer value jumps drastically while going from 0 to 100. But when coming down from 100 to 0 the output values seems to be fine. Your help is highly appreciated.
Sketch:
#include <Stepper.h>
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 12, 13);
int stepCount = 0;
int buttonState = 0;
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
int sensorReading = analogRead(A0);
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution / 100);
Serial.println(motorSpeed);
}
else
{
digitalWrite(ledPin, HIGH);
int sensorReading = analogRead(A0);
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
myStepper.setSpeed(motorSpeed);
myStepper.step(-stepsPerRevolution / 100);
Serial.println(motorSpeed);
}
}
