I am trying to control a stepper motor with a clockwise, counterclockwise, and stop button, as well as a potentiometer for speed. I got most of the code from other internet sources but i'm having trouble adding the potentiometer speed control. I have attached my code and any help would be greatly appreciated.

#include <Stepper.h>

const int stepsPerRevolution = 48;

Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);

int Pin1 = 10;//IN1 is connected to 10 
int Pin2 = 11;//IN2 is connected to 11  
int Pin3 = 12;//IN3 is connected to 12  
int Pin4 = 13;//IN4 is connected to 13 
int switchCW  =2;//define input pin for CW push button
int switchStop=3;//define input pin for Stop push button
int switchCCW =4;//define input pin for CCW push button

int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values

int poleStep = 0; 
int  dirStatus = 3;// stores direction status 3= stop (do not change)

void setup() 
 pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1 
 pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2   
 pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3   
 pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4   
 pinMode(switchCW,INPUT_PULLUP);// CW push button pin as input
 pinMode(switchStop,INPUT_PULLUP);//Stop push button pin as input 
 pinMode(switchCCW,INPUT_PULLUP);//CCW push button pin as input

 void loop() 
  int sensorReading = analogRead(A2);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  if (motorSpeed > 0) {
    myStepper.step(stepsPerRevolution / 100);
  if(digitalRead(switchCCW) == LOW) 
    dirStatus =1;
  }else if(digitalRead(switchCW) == LOW)
   dirStatus  = 2;  
  }else if(digitalRead(switchStop) == LOW)
    dirStatus =3; 
 if(dirStatus ==1){ 
 }else if(dirStatus ==2){ 

}// loop

 * @brief sends signal to the motor
 * @param "c" is integer representing the pol of motor
 * @return does not return anything
 * www.Robojax.com code June 2019
void driveStepper(int c)
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);   
}//driveStepper end here
  • Have you made sure that the A2 port receives the correct range of voltages from the potentiometer and that the value of motorSpeed reflects that? How have you connected the potentiometer to A2? – StarCat Jul 22 at 14:21
  • Re: "but i'm having trouble" --- What kind of trouble? What's the problem? What's the question? This isn't a tutorial site. You need to ask something specific. If you just want someone to show you how to do this then search the internet and find any one of the thousands of tutorials that do similar things. – Delta_G Jul 22 at 14:50
  • start at the begining ... forget about the buttons and about the joystick ... write code that runs the motor for 1 second at low speed followed by 1 second at medium speed ... it does not have to repeat .... when you are successful doing that, then add joystick code and test again ... keep adding code after each successful test – jsotola Jul 22 at 18:19
  • do not use else if in your button logic ... if one of the direction buttons fails in the pressed position, then you will be unable to use the stop button – jsotola Jul 22 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.