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);
}
}