I finally noticed in all of my trial and error, that the 5+ came unhooked from my stepper driver witch is why I couldnt getting any of the variations I tried to get it to CCW to work.
Here is the code now, and it works perfect. I also found out 61 steps is more accurate of my gear box. This gear box is an old wiper motor from a car that I retrofitted with a stepper motor.
/* Sketch uses 9492 bytes (29%) of program storage space. Maximum is 32256 bytes.
Global variables use 464 bytes (22%) of dynamic memory, leaving 1584 bytes for local variables. Maximum is 2048 bytes.
*/
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Define our three input button pins
#define LEFT_PIN 11
#define STOP_PIN 1
#define RIGHT_PIN A2
#define LEFT_PIN2 A5
#define RIGHT_PIN2 A4
#define LIMIT_PIN A3
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 2, 3);
AccelStepper stepper2(AccelStepper::DRIVER, 12, 13);
//Non Blocking Code Millis for the Potentiometer Speed Display on the LCD
unsigned long previousMillis = 1000;
const long interval = 2000;
// Define our maximum and minimum speed in steps per second (scale pot to these)
const int MAX_SPEED = 6000; //Max potentiometer speed, 2000 steps per second divide by 200 steps (1 revolution)=10 revultions per second,=600rpm. Geared at 3:1 this =200rpm per min on the PM727, that is 2 inchs per minute travel
const int MIN_SPEED = 8; //Min potentiometer speed
int buttonStateCw = 0;
int buttonStateCcw = 0;
const int angleSpeed = 4000; //Speed the Angle will Move when button is pushed
const int RPM = 12; // RPM lcd display of Indexing Head
void setup() {
// Define our analog potentiometer input pin
const int SPEED_PIN = A1; // Input Pin location on the Arduino
// Define our maximum and minimum speed in steps per second (scale pot to these)
const int MAX_SPEED = 4000; //Max potentiometer speed, 2000 steps per second divide by 200 steps (1 revolution)=10 revultions per second,=600rpm. Geared at 3:1 this =200rpm per min on the PM727, that is 2 inchs per minute travel
const int MIN_SPEED = 8; //Min potentiometer speed
long initial_homing = -1; // Used to Home Stepper to Up or Down Limit Switchs
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(LEFT_PIN2, INPUT_PULLUP);
pinMode(RIGHT_PIN2, INPUT_PULLUP);
pinMode(LIMIT_PIN, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("RPM:");
lcd.setCursor(8, 0);
lcd.print("DIR:");
lcd.setCursor(0, 2);
lcd.print("ANGLE:");
lcd.setCursor(7, 1);
lcd.print("HOMING");
Serial.begin(9600);
while (digitalRead(LIMIT_PIN)) {
stepper2.moveTo(-initial_homing); // Set the position to move to
stepper2.setMaxSpeed(4000); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper2.setAcceleration(100); // Set Acceleration of Stepper
initial_homing--; // Decrease by 1 for next move if needed
stepper2.run(); // Start moving the stepper
}
stepper2.setCurrentPosition(0); // Set the current position as zero for now
stepper2.setMaxSpeed(200); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper2.setAcceleration(100); // Set Acceleration of Stepper
initial_homing = 1;
stepper2.setCurrentPosition(0);
stepper2.setMaxSpeed(20000.0);
stepper1.setMaxSpeed(20000.0);
stepper2.setAcceleration(10000);
lcd.setCursor(7, 1);
lcd.print("0 ");
lcd.setCursor(10, 1);
lcd.print("DEGREE");
}
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
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 int sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
int analog_value = 0; // Holds raw analog value.
static float previouscurrent_speed = 0.05; //declares "currentMillis" as to reflect Arduino millis
unsigned long currentMillis = millis();
buttonStateCw = digitalRead(LEFT_PIN2);
buttonStateCcw = digitalRead(RIGHT_PIN2);
if (digitalRead(LEFT_PIN) == 0) {
sign = 1; lcd.setCursor(13, 0);
lcd.print(" CW ");
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1; lcd.setCursor(13, 0);
lcd.print("CCW ");
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0; lcd.setCursor(12, 0);
lcd.print("STOP");
}
// 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(A1);
// 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);
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (current_speed != previouscurrent_speed ) {
previouscurrent_speed = current_speed;
lcd.setCursor(4, 0);
lcd.print(float(float(current_speed) / float(RPM)), 1);
}
}
// When a Direction Button is pressed, make it run in that direction indefinetly untill another button is pressed
if (sign != 0) {
stepper1.runSpeed();
}
int curPos = stepper2.currentPosition(); //stores a copy of stepper2.currentPositon()
if ((buttonStateCw == LOW) && (curPos % 61 == 0) && (curPos < 7000)) {
//(curPos % 100) is curPos modulo 100, which gives the remainder after dividing by 100
//also need to limit curPos to a maximum of 7000
stepper2.moveTo(curPos + 61); //move to 100 more than the current position
stepper2.setMaxSpeed(angleSpeed);
lcd.setCursor(7, 1);
int curAngle = (curPos / 61) + 1; //angle start at 35 when current position is 0, then increases by 1 for each 100 steps
if (curAngle > 90) { //limits angle to 90 (your original code reached 90 at 5400 but continues on to 6900
curAngle = 90;
}
if (curAngle < 10) { //prints a leading space if angle is a single digit
lcd.print(" ");
}
lcd.print(curAngle); //prints the angle
lcd.print(F(" DEGREES ")); //print the remaining text after the angle
}
if ((buttonStateCcw == LOW) && (curPos % 61 == 0) && (curPos > 0)) {
//(curPos % 100) is curPos modulo 100, which gives the remainder after dividing by 100
//also need to limit curPos to a maximum of 7000
stepper2.moveTo(curPos - 61); //move to 100 more than the current position
stepper2.setMaxSpeed(angleSpeed);
lcd.setCursor(7, 1);
int curAngle = (curPos / 61) - 1; //angle start at 35 when current position is 0, then increases by 1 for each 100 steps
if (curAngle > 90) { //limits angle to 90 (your original code reached 90 at 5400 but continues on to 6900
curAngle = 90;
}
if (curAngle < 10) { //prints a leading space if angle is a single digit
lcd.print(" ");
}
lcd.print(curAngle); //prints the angle
lcd.print(F(" DEGREES ")); //print the remaining text after the angle
}
while (stepper2.currentPosition() != stepper2.targetPosition()) { // only move it while we're not at the target
stepper2.runSpeedToPosition(); // I don't think this is blocking
}
if (sign != 0) {
stepper1.runSpeed();
}
}