OK so my problem was in the stepperX.run(); command. with the code originally, my loop() the flow was like this:
wait 10s -> read value -> make sure position is not outside the HOME or MAX positions -> set new target position -> take ONE step if a step is due -> check for movement completed never returns true -> wait 10s -> read value ->...
I ended up using the millis() but extended it to 30 second interval and its working flawless. this is the current working code;
/* Valve Control with Analog input v2
Created by Paul Wheeler
designed to proportionally control large butterfly valve using 0-5VDC input
from Building Management System
*/
#include "AccelStepper.h"
#define ANALOG_READ_INTERVAL 30000 //30000ms
// AccelStepper Setup
AccelStepper stepperX(1, 2, 3); // Pin 2 connected to PUL- pin of Driver PUL+ connected to 5v+
// Pin 3 connected to DIR- pin of Driver DIR+ connected to 5v+
// Define the Pins used
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch N/O)
#define ENA 13 //pin 13 is to drive ENA+ and ENA- connected to GND
#define ANALOG_IN A0 //analog 0-5VDC input + to A0 - to GND (External power source)
// Stepper Travel Variables
long TravelA; // Used to store the first travel value
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
unsigned long time_last_analog_read = 0L; //last time the analog value was read
// Read new position every 10 seconds
if (millis() - time_last_analog_read > ANALOG_READ_INTERVAL)
{
time_last_analog_read = millis(); //reset timer
int analog_val = analogRead(ANALOG_IN);
move_finished = 0; // Set variable for checking move of the Stepper
Serial.println(analog_val); //for monitoring
long TravelA = (analog_val * 142L); // need close to 146000 steps to get from home(closed) to full Open
if (TravelA < 0 || TravelA > 146000) { // Make sure the position calculated is not outside of the HOME or MAX position
} else {
Serial.print("Moving stepper into position: ");
Serial.println(TravelA);
}
if (TravelA >= 0 && TravelA <= 146000) {
digitalWrite(ENA, LOW); // sets digital pin 13 enabling drive
stepperX.moveTo(TravelA); // Set new moveto position of Stepper
}
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
move_finished = 1; // Reset move variable
digitalWrite(ENA, HIGH); // sets digital pin 13 disabling drive
}