Hi Guys.
I am working on a project for stepper motor control in two mode: 1. Locally by pressing buttons, 2. through serial.
What I want:
-
Local control - a button to enable local control, forward and backward movements by press & hold their respective buttons. A fourth button I added for resetting.
-
Serial Control: Various different movements by sending keys.
For local control, I already have a working code (greatly based on Example 5):
For serial control, I tested a working code posted in here.).
Now I am trying to combine these both codes. Result is:
#include <AccelStepper.h>
int Stepping = false;
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 2, 3);
const int buttonPin = 11;
const int ledPin = 12;
const int restPin = 13;
//Variables
int buttonState = 0;
int flag=0;
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
// Define our three input button pins
#define LEFT_PIN 9
#define STOP_PIN 11
#define RIGHT_PIN 10
#define EN 6
// Define our analog pot input pin
#define SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 5000
#define MIN_SPEED 0.1
char user_input;
int x;
int y;
int state;
void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
digitalWrite(LEFT_PIN, HIGH);
digitalWrite(STOP_PIN, HIGH);
digitalWrite(RIGHT_PIN, HIGH);
digitalWrite(restPin, HIGH);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(EN, OUTPUT);
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
resetEDPins(); //Set step, direction, microstep and enable pins to default states
Serial.begin(9600); //Open Serial connection for debugging
Serial.println("Begin motor control");
Serial.println();
//Print function list for user selection
Serial.println("Enter number for control option:");
Serial.println("1. Turn at default microstep mode.");
Serial.println("2. Reverse direction at default microstep mode.");
Serial.println("3. Turn at 1/8th microstep mode.");
Serial.println("4. Step forward and reverse directions.");
Serial.println();
}
void(*resetFunc) (void) = 0;
void loop() {
static float current_speed = 0.0; // Holds current motor speed in steps/second
static int analog_read_counter = 1000; // Counts down to 0 to fire analog read
static char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
static int analog_value = 0; // Holds raw analog value.
buttonState = digitalRead(buttonPin);
//If button pressed...
if (buttonState == LOW) {
if ( flag == 0){
digitalWrite(ledPin, HIGH);
digitalWrite(EN, LOW);
flag=1; //change flag variable
}
//...twice, turn led off!
else if ( flag == 1){
digitalWrite(ledPin, LOW);
digitalWrite(EN, HIGH);
flag=0; //change flag variable again
}
delay(200);
}
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1;
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1;
}
if ((digitalRead(LEFT_PIN) == 1) && digitalRead(RIGHT_PIN) == 1)
{
sign = 0;
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000;
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
}
// This will run the stepper at a constant speed
stepper1.runSpeed();
int RESET = digitalRead(restPin);
if (RESET == LOW)
{
digitalWrite(ledPin, LOW);
digitalWrite(EN, HIGH);
resetFunc();
digitalWrite(ledPin, HIGH);
digitalWrite(EN, LOW);
}
while(Serial.available()){
user_input = Serial.read(); //Read user input and trigger appropriate function
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (user_input =='1')
{
StepForwardDefault();
}
else if(user_input =='2')
{
ReverseStepDefault();
}
else if(user_input =='3')
{
SmallStepMode();
}
else if(user_input =='4')
{
ForwardBackwardStep();
}
else
{
Serial.println("Invalid option entered.");
}
resetEDPins();
}
}
//Reset Easy Driver pins to default states
void resetEDPins()
{
digitalWrite(stp, LOW);
digitalWrite(dir, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}
//Default microstep mode function
void StepForwardDefault()
{
Serial.println("Moving forward at default step mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
for(x= 1; x<1000; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
//Reverse default microstep mode function
void ReverseStepDefault()
{
Serial.println("Moving in reverse at default step mode.");
digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
for(x= 1; x<1000; x++) //Loop the stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
// 1/8th microstep foward mode function
void SmallStepMode()
{
Serial.println("Stepping at 1/8th microstep mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
digitalWrite(MS1, HIGH); //Pull MS1, and MS2 high to set logic to 1/8th microstep resolution
digitalWrite(MS2, HIGH);
for(x= 1; x<5000; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
//Forward/reverse stepping function
void ForwardBackwardStep()
{
Serial.println("Alternate between stepping forward and reverse.");
for(x= 1; x<5; x++) //Loop the forward stepping enough times for motion to be visible
{
//Read direction pin state and change it
state=digitalRead(dir);
if(state == HIGH)
{
digitalWrite(dir, LOW);
}
else if(state ==LOW)
{
digitalWrite(dir,HIGH);
}
for(y=1; y<1000; y++)
{
digitalWrite(stp,HIGH); //Trigger one step
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
}
Serial.println("Enter new option:");
Serial.println();
}
Problem is:
My local control for forward and backward movements doesn't work.
Now, If I disable:
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
My local controls are working but serial controls are affected since I disabled the stepping function.
I believe AccelStepper.h has some compatibility issues with MS1 and MS2.
I will be thankful if you guys can help me sort out this problem.
Thanks.
Z