Trying to use Accelstepper for the first time, no stepper moves at all.
The hardware is tested using other home brewed code and works.
For a project controlling a rotating table on my mill a stepper is attached to the crank handle. An UNO, a TB6600 and some code is put together, all fed by a 24V, 17A, switched supply. The code is verified to work up until Accelstepper is called. No stepper moves so far.
In this version the execution is stuck in myStepper.runToNewPosition(anglePos);//Blocking until done
myStepper.run();
is called in every turn of loop.
Poking around I find lists of AccelStepper comands. Some are accepted by the controller, some are not. Code below with the current attempt.
What do I miss?
#include<arduino.h>
//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip
AccelStepper myStepper (2, 5);
// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2
enum mode {Sides, Angle };
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4
unsigned long currentStep;
enum cmd {increase, decrease, Go, Chg, GoStep, nobutton};
unsigned long anglePos;
void setup() {
Serial.begin(115200);
Serial.println("Rotation ");
/* //1Hz 90% dutycycle
pinMode(9, OUTPUT); // Set digital pin 9 (D9) to an output
TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11); // Enable the PWM output OC1A on digital pins 9 and invert output
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12); // Set fast PWM and prescaler of 256 on timer 1
ICR1 = 62499; // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
OCR1A = 6249; // Set the duty-cycle to 10%: 62499 / 10 = 6249
pinMode(10, OUTPUT); // 1 Hz pulse output on digital pin 9 (D9)
*/ pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
delay(10);//allow pwm timers to start
// myStepper.begin();
myStepper.setMaxSpeed(500);
myStepper.setAcceleration(100);
#define digitalIncreaseButton 2
pinMode (digitalIncreaseButton, OUTPUT);
#define digitalDecreaseButton 3
pinMode (digitalDecreaseButton, OUTPUT);
#define digitalChgButton 4
pinMode(digitalChgButton, OUTPUT);
#define digitalGoButton 5
pinMode(digitalChgButton, OUTPUT);
#define guard1 6
#define guard2 7
#define cuttingSpeed 100 // when cutting during rotation
#define movingSpeed 2000 //when moving without cutting
/*
int status;
status = mylcd.begin(LCD_COLS, LCD_ROWS);
if (status) // non zero status means it was unsuccesful
{
status = -status; // convert negative status value to positive number
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}
mylcd.clear();
*/
// Print start message to the LCD
/* mylcd.print("221118a Rotating");
delay(5000);
mylcd.print("Running");
delay(1000);
*/
}
enum cmd readButtons(void)
{
char cmd = 'q';
/*
if ((digitalIncreaseButton) == 0) return (increase);
if ((digitalDecreaseButton) == 0) return (decrease);
if ((digitalGoButton) == 0) return (Go);
if ((digitalChgButton) == 0) return (Chg);
*/
if (Serial.available() > 0)
{
cmd = Serial.read();
// Serial.print("Recieved cmd "); Serial.println(cmd);
}
if (cmd == 's' )
return (GoStep);
if (cmd == 'S' )
return (GoStep);
return (nobutton);
}
boolean guardCheck(void)
{
/* commented out until hardware is designed
if (!digitalRead(guard1) && !digitalRead(guard2))
return (true);//Unlocked, clear to go
else
return false;//Locked, don't go
*/
return (true);
}
void loop()
{
unsigned long lastMillis;
enum cmd lcmd;
enum mode lmode;
float tmpfloat;
lcmd = readButtons();// Read button inputs if any + - Go Chg mode
if ( millis() - lastMillis > 200 )
{
lastMillis = millis();
if ( lmode == Sides ) //number of surfaces
{
switch (lcmd)
{
case increase:
{
faces++;
break;
}
case decrease:
{
faces--;
if (faces < 2)
{
faces = 1; //1 rotate one turn
myStepper.setMaxSpeed(cuttingSpeed);
}
else
myStepper.setMaxSpeed(movingSpeed);
break;
}
case Chg:
{
lmode == Angle; //switch to angle mode
myStepper.setMaxSpeed(movingSpeed);// used i angle mode
break;
}
case Go:
{
while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
anglePos += nrOfStepsPerRev * 360 / faces;
myStepper.runToNewPosition(anglePos);//Blocking until done
break;
}
case GoStep:
{
Serial.print("GoStep received. ");
while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
Serial.print("Stepping to step "); Serial.println(currentStep + 1);
anglePos = nrOfStepsPerRev * 360 * ++currentStep / faces;
currentStep %= faces;
Serial.print("anglePos = "); Serial.println( anglePos);
tmpfloat = 360 * float(currentStep) / float(faces);
Serial.print("Angle = "); Serial.println( tmpfloat);
myStepper.runToNewPosition(anglePos);//Blocking until done
// myStepper.runToPosition(anglePos);//Blocking until done
if (currentStep == 0)// Back on zero position, update AccelStepper position!!!
myStepper.setCurrentPosition(0);//Accelstepper ready for a second turn around
break;
}
default:
break;
}//end of switch
}//end of "if (lmode == Sides)"
else if ( lmode == Angle ) // Angle per step
{
switch (lcmd)
{
case increase:
{
anglePos += nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
break;
}
case decrease:
{
anglePos -= nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
// if (faces < 1) faces = 1; //1 rotate one turn
break;
}
case Chg:
{
lmode == Sides; //switch to sides mode
// goFlag = false;//
break;
}
case Go:
{
while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
myStepper.setMaxSpeed(movingSpeed);
// myStepper.runToNewPosition(anglePos);//Blocking until done
myStepper.moveTo(anglePos);//Blocking until done
anglePos += nrOfStepsPerRev * 5 / 360;
break;
}
default:
break;
}
}
}
// if (guardCheck())
// {
// Serial.print("!");
myStepper.run();
// }
}// End of loop
/*
moveTo KEYWORD2
move KEYWORD2
run KEYWORD2
runSpeed KEYWORD2
setMaxSpeed KEYWORD2
setAcceleration KEYWORD2
setSpeed KEYWORD2
speed KEYWORD2
distanceToGo KEYWORD2
targetPosition KEYWORD2
currentPosition KEYWORD2
setCurrentPosition KEYWORD2
runToPosition KEYWORD2
runSpeedToPosition KEYWORD2
runToNewPosition KEYWORD2
stop KEYWORD2
disableOutputs KEYWORD2
enableOutputs KEYWORD2
setMinPulseWidth KEYWORD2
setEnablePin KEYWORD2
setPinsInverted KEYWORD2
maxSpeed KEYWORD2
enum flag {const1, const2, ..., constN};
By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).
// Changing default values of enum constants
enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3,
};
*/