Stepper motor movement

Hi all

I've been working on this code for a more time than I like to admit. New to programming and Arduino

So far I've managed to control the speed of a stepper motor movement, the length of delay between steps. I'm using and Arduino Uno and Easydriver v4. The code is based off some previous post by people.

I have 3 POTs controlling the stepper.
POT1 controls the speed (input pin A2)
POT2 controls the delay between steps (input pin A0)
POT3 is supposed to control the distance moved. (input pin A1)

The only part I can't seem to get is controlling the distance that the stepper moves. I can set the distance travelled in the program, but I can't vary it through the POT3. Code is below. Weird thing on this one is that the stepper mover will move 225degrees, if I move POT3 to smaller distances, then the stepper does this very odd, move correctly 7 times, then rotate 20-30 times without stopping, then it will move correctly again for 2 turns, then back to 20-30 continuous turns, then maybe 4-7 turns and again back to 20-30 turns, etc. Seems random, not sure why that should happen. Any suggestions or alternative codes that might be able out there would be appreciated. Searched, and can't seem to figure it out.

#include <Potentiometer.h>
#include <Stepper.h>

Potentiometer potentiometera = Potentiometer(0); // Pot at analog-in 0  
Potentiometer potentiometer = Potentiometer(2); // Pot at analog-in 2
Potentiometer potentiometerb = Potentiometer(1); // Pot at analog-in 1

#define stepPin 9          // Set step pin
#define dirPin 8           // Set direction pin
const int buttonPin = 10;  // Set direction button

int buttonState = 0;       // Record the value of the button
int Distance = 0;          // Record the number of steps taken
int usTime;                // Store the value of the time delay here
int Value;                 // Store the value of the full step move required

void setup()              // Static setup 
{
  pinMode(buttonPin, INPUT);      // Direction switch
  pinMode(stepPin, OUTPUT);       // Stepper pin
  pinMode(dirPin, OUTPUT);        // Direction pin
  digitalWrite(dirPin, HIGH);     // Sets the direction on way
  digitalWrite(stepPin, LOW);     // Starts the stepping motion
}

void loop()
{
  
  Value = map(analogRead(A1), 1, 100, 1, 1000);
  Value = constrain(Value, 1, 1000);

  
  digitalWrite(stepPin, HIGH);                    
  delayMicroseconds(10 + potentiometer.getValue());
  digitalWrite(stepPin, LOW);
  delayMicroseconds(10);
  
  Distance = Distance + 1;        // Record the step

// Check to see if the full step move as completed  
  
  if (Distance == Value)           
 { // Yes required distance travelled then stop and delay for the set time (according to Pot 2)
   if (digitalRead(stepPin) == LOW)
   {
     digitalWrite(stepPin, LOW);
   }
   else
   {
     digitalWrite(stepPin, HIGH);
   }
   
// Reset the distance back to zero before beginning new move at top of loop
    Distance = 0;

// Pause time between steps    
    for(int i=0; i<1600*1; i++){
      usTime = map(analogRead(A0), 1, 100, 1, 1000);
      usTime = constrain(usTime, 1, 1000);
      delayMicroseconds(usTime);
    }
 }
 
 // Check to see if direction button has been pressed
 // If so direction will reverse
 
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH) {
   digitalWrite(dirPin, HIGH);
 }
 else
 {
   digitalWrite(dirPin, LOW);
 }
}

Value = map(analogRead(A1), 1, 100, 1, 1000);

A1 is the code used for accessing an analog pin as a digital input/output.

To work with analog, just use 1 not A1.

Thanks the for the tip
Changed from A1 to 1. Stepper motor still rotates continuously after a few correct distance moves. Can't see anything in the code that would cause this motion.

Maybe this causes the problem.

  if (Distance == Value)

Is it possible that at times Value does not equal Distance when you think it should? Try a different comparison, like:

  if (Distance > Value)

Just a thought...

Cheers, that seems to have fixed the continuous spinning every few moves. Sometimes it's the simplest things!

Still working on getting the distance of the step to be somewhat more accurate in setting. Any suggestion on how to do this with coding? (There is a limit to how far a potentiometer can be used)

Also, movement seems to go back to small if the potentiometer is turned too much, but this may just be the potentiometer I'm using exceeding its max turns.

Potentiometers are odd things. I thought that they were relatively linear, but that is not true for most, especially the lower end models. They "step" up and down. On an o-scope, it looks like stairs when you monitor a cheap pot output while changing the pot setting. After a while, they will even go to open circuit in certain places.

I'll say. The smallest movement in the potentiometer and I can go from 1 step to 6 full revolutions. Will need to find a better potentiometer in the meantime...unless there is a digital solution?

The goal would be to control from a stand-alone board, set the values via push-button (written onto a LCD display for the user) then sent through to Easydriver via the arduino. Have seen a few codes to write to a 16x2 LCD but nothing so far for digital read/write to the Arduino to substitute the potentiometers.

If anyone has suggestions on this they are welcome.

Wire wound pots may tend to have steps. Also - is it a linear or Audio Taper (logrhythmic)

Also - POT1 & POT2 are doing the same thing - the speed is controlled by the amount of time between steps.