I'm not even sure this is a programming question
I've got a loop that is moving a stepper back and forth between two known point. At the start of each "forward" move loop I check a analog read pot to see what the speed should be
myStepper.setSpeed(Speed);
where Speed is the mapped value from the pot between 5000 and 15000
I've added a print right after that "setSpeed" line to see what value "Speed" is at that moment
When I start the machine, if the pot is anywhere between full CW or CCW the machine will move really, really, really slowly even though the Speed value is say 7000, and I can turn the pot back and forth from say 6000 all the way to 14000 with no affect on the machine speed, it just creeps along
If I turn the pot full CCW (5000 on the print out) while it's moving, the machine will suddenly speed up to proper 5000 speed and then I can turn the pot up and down and it will work properly with the machine following the speed change to the pot.
Same thing if I go full CCW (15000 on the print out), it will shoot up to full speed and then keep following the speed changes on the pot.
(The 5000 and 15000 values are just picked by me as what I consider the slowest and fastest rates are so they shouldn't matter)
I'm going to try "hard coding" different values in after the analog read (like 7000 or 10000) to see but according to the serial print the variable value is correct for the setSpeed line of code.
How's that for a weird one.
#define MAX8BUTTONS // This saves ressources if you don't need more than 8 buttons
#include <MobaTools.h>
// 4 = step
//2 = direction
// 13 = manual/auto select
// 16 = manual up
// 17 = manual down
// 18 = auto start
// 19 = auto stop
// 33 = home limit switch
// 35 = number of cycles
// 26 = dwell time
// 32 = speed
const byte buttonPins [] = { 13,16,17,18, 19,33 };
enum : byte {SW_SEL, MAN_UP, MAN_DOWN, AUT_START, AUT_STOP, HOME_SW };
const byte buttonCount = sizeof(buttonPins);
MoToButtons Buttons( buttonPins, buttonCount, 30, 500 ); // 500ms to distinguish short/long
const int DIR = 2; // Stepper DIR Pin 2
const int STEP = 4; // Stepper STEP Pin 4
const int CYCLES_IN = 35; // Analog Input Number of Cycles
const int SPEED_IN = 32; // Analog Input speed
const int DWELL_IN = 26; // Analog Input dwell
bool stepperRunning;
bool isStepping = false;
bool cycling=false;
bool home_state = 0; // home switch tripped
bool HOMED = false;
long dwell = 1;
int Start_Time = 0;
//int Stop_Time = 0;
int Cycle_To_Do = 0;
int cycle_cnt=0;
long maxSpeed = 15000;//11111; //20000;
int ramp_len=500;
int StepSpeed=maxSpeed;
// ballscrew is 5mm pitch
// gear ratio, 90 drive, 45 driven = 1:2
const long EndPoint = 18288; // = 18"
long nextPos = 0;
MoToStepper myStepper (400,STEPDIR); // setps/rev and type of controller
MoToTimer stepperPause; // Pause between cycles
void setup()
{
Serial.begin(115200);
myStepper.attach( STEP, DIR ); //4, 2
myStepper.setSpeed( StepSpeed );
myStepper.setRampLen(400); //400
delay(2000); // need to prevent partial run when loading
Cycle_To_Do=2; // this will come from selector switch CYCLES_IN
Home();
Speed();
}
void loop()
{
Buttons.processButtons();
Speed(); // <<<<<<<<<< call the POT read for speed <<<<<<<<
if(Buttons.state(SW_SEL)) //*** manual mode ***
{
Serial.println("Manual");
}
else //*** automnatic mode ***
{
//Serial.println("Automatic");
if( !cycling ) // not currently cycling
{
if ( Buttons.pressed(AUT_START) )
{
get_cycles(); //*** get number of cycles to do ***
myStepper.setSpeed(StepSpeed); // Set speed
Serial.println("START button pressed");
Start_Time=millis();
cycling = true;
stepperRunning= true;
myStepper.moveTo(EndPoint);
}
}
else
{
myStepper.setSpeed(StepSpeed);
Serial.println(StepSpeed); // <<<<<<<< verify what speed was returned from pot
if ( stepperRunning )
{
if ( !myStepper.moving() )
{
stepperPause.setTime(dwell);
stepperRunning = false;
}
}
else
{
if ( stepperPause.expired() )
{
if ( nextPos == 0 )
{
nextPos = EndPoint;
dwell=1;
}
else
{
nextPos = 0;
dwell=1; //get_dwell();
cycle_cnt++;
if (cycle_cnt>=Cycle_To_Do)
{
cycling=false;
cycle_cnt=0;
dwell=1;
}
}
myStepper.moveTo( nextPos );
stepperRunning = true;
}
}
if ( Buttons.pressed(AUT_STOP) )
{
Serial.println("STOP button pressed");
myStepper.setSpeed(0); // stop with ramp
cycle_cnt=0; // cancel any outstanding cycles
isStepping = false;
cycling=false;
dwell=1;
myStepper.setSpeed(maxSpeed);
while (myStepper.currentPosition()!=0)
{
myStepper.moveTo(0); // keep moving up
}
}
}
}
}
// *** FUNCTIONS ***
void get_dwell()
{
dwell = map(analogRead(DWELL_IN),0,4095,1000,5000);
}
void get_cycles()
{
int CYCLE_DATA;
CYCLE_DATA = analogRead(CYCLES_IN);
//Serial.println(CYCLE_DATA);
if (CYCLE_DATA >= 0 and CYCLE_DATA < 500) {Cycle_To_Do = 1;}
else if (CYCLE_DATA >= 500 and CYCLE_DATA < 800) {Cycle_To_Do = 2;}
else if (CYCLE_DATA >= 800 and CYCLE_DATA < 1100) {Cycle_To_Do = 3;}
else if (CYCLE_DATA >= 1100 and CYCLE_DATA < 1400) {Cycle_To_Do = 4;}
else if (CYCLE_DATA >= 1400 and CYCLE_DATA < 1700) {Cycle_To_Do = 5;}
else if (CYCLE_DATA >= 1700 and CYCLE_DATA < 2000) {Cycle_To_Do = 6;}
else if (CYCLE_DATA >= 2000 and CYCLE_DATA < 2300) {Cycle_To_Do = 7;}
else if (CYCLE_DATA >= 2300 and CYCLE_DATA < 2600) {Cycle_To_Do = 8;}
else if (CYCLE_DATA >= 2600 and CYCLE_DATA < 2900) {Cycle_To_Do = 9;}
else if (CYCLE_DATA >= 2900 and CYCLE_DATA < 3400) {Cycle_To_Do = 10;}
else if (CYCLE_DATA >= 3400 and CYCLE_DATA < 3800) {Cycle_To_Do = 11;}
else {Cycle_To_Do = 12;}
//Serial.println(Cycle_To_Do);
Cycle_To_Do = 1;
}
void Speed()
{
StepSpeed = map(analogRead(SPEED_IN),0,4095,5000,maxSpeed);
//Serial.println(StepSpeed);
}
void Home()
{
myStepper.setZero(0);
myStepper.setSpeed( 1500 ); // slow speed for homing
myStepper.moveTo(1400); // move the carrige down some
while ( myStepper.distanceToGo()!=0 )
{
Serial.println("Staging");
myStepper.moveTo(1400);
}
while (HOMED == false ) // move up to home switch
{
Serial.println("Homing");
while (!Buttons.state(HOME_SW))
{
Buttons.processButtons();
myStepper.moveTo(myStepper.currentPosition()-400); // keep moving up
}
myStepper.stop();
myStepper.setZero(1200); // come down off the switch
myStepper.moveTo(0);
HOMED=true;
Serial.println("HOMED");
}
}