This is a different compiling problem.
I am working around the previous problem.
I might post the code later.
Every once in a while I cannot compile a sketch that I
compiled 10 minutes ago the same code with minor changes.
Some times after 2 or 3 tries it will compile.
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:27:11: error:
redefinition of 'const int dirPin'
const int dirPin = 4;
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:16:11:
note: 'const int dirPin' previously defined here
const int dirPin = 6;
const int dirPin = 4; does not exist in the code
Same for const int stepPin = 5; not in the code
Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"
C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\arduino-builder -dump-prefs -logger=machine -hardware C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware -hardware C:\Users\Ralph\AppData\Local\Arduino15\packages -hardware C:\Users\Ralph\Documents\Arduino\hardware -tools C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\tools-builder -tools C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -tools C:\Users\Ralph\AppData\Local\Arduino15\packages -built-in-libraries C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\libraries -libraries C:\Users\Ralph\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_126844 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_146739 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -verbose C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino
C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\arduino-builder -compile -logger=machine -hardware C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware -hardware C:\Users\Ralph\AppData\Local\Arduino15\packages -hardware C:\Users\Ralph\Documents\Arduino\hardware -tools C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\tools-builder -tools C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -tools C:\Users\Ralph\AppData\Local\Arduino15\packages -built-in-libraries C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\libraries -libraries C:\Users\Ralph\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_126844 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_146739 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\Ralph\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -verbose C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino
Using board 'uno' from platform in folder: C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Users\Ralph\Documents\Temp\arduino-1.8.15-windows\arduino-1.8.15\hardware\arduino\avr
Detecting libraries used...
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp" -o nul
Alternatives for AccelStepper.h: [AccelStepper]
ResolveLibrary(AccelStepper.h)
-> candidates: [AccelStepper]
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp" -o nul
Alternatives for elapsedMillis.h: [elapsedMillis@1.0.6]
ResolveLibrary(elapsedMillis.h)
-> candidates: [elapsedMillis@1.0.6]
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\elapsedMillis" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp" -o nul
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\elapsedMillis" "C:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper\\AccelStepper.cpp" -o nul
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\elapsedMillis" "C:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper\\MultiStepper.cpp" -o nul
Generating function prototypes...
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\elapsedMillis" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10815 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Users\\Ralph\\Documents\\Temp\\arduino-1.8.15-windows\\arduino-1.8.15\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\AccelStepper" "-IC:\\Users\\Ralph\\Documents\\Arduino\\libraries\\elapsedMillis" "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_126844\\sketch\\VERSION-4.ino.cpp.o"
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:27:11: error: redefinition of 'const int dirPin'
const int dirPin = 4;
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:16:11: note: 'const int dirPin' previously defined here
const int dirPin = 6;
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:28:11: error: redefinition of 'const int stepPin'
const int stepPin = 5;
^~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:17:11: note: 'const int stepPin' previously defined here
const int stepPin = 7;
^~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:33:38: error: redefinition of 'AccelStepper myStepper'
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin); // works for a4988 (Bipolar, constant current, step/direction driver)
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:19:14: note: 'AccelStepper myStepper' previously declared here
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin); // works for a4988 (Bipolar, constant current, step/direction driver)
^~~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:49:14: error: redefinition of 'volatile int state'
volatile int state; // must survive interrupts
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:35:14: note: 'volatile int state' previously declared here
volatile int state; // must survive interrupts
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:51:15: error: redefinition of 'elapsedMillis printTime'
elapsedMillis printTime;
^~~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:37:15: note: 'elapsedMillis printTime' previously declared here
elapsedMillis printTime;
^~~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:53:14: error: redefinition of 'volatile int enabFlag'
volatile int enabFlag; // controls the end stop sensor interrupt
^~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:39:14: note: 'volatile int enabFlag' previously declared here
volatile int enabFlag; // controls the end stop sensor interrupt
^~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:55:10: error: redefinition of 'long int myStart'
long int myStart = -5; // adjustthis as needed - code will handle it
^~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:41:10: note: 'long int myStart' previously defined here
long int myStart = -5; // adjustthis as needed - code will handle it
^~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:56:10: error: redefinition of 'long int endLimit'
long int endLimit = 3000; // Some nice long distance away - STOP will reset this
^~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:42:10: note: 'long int endLimit' previously defined here
long int endLimit = 300000; // Some nice long distance away - STOP will reset this //HERE
^~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:57:6: error: redefinition of 'bool findLimit'
bool findLimit = true;
^~~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:43:6: note: 'bool findLimit' previously defined here
bool findLimit = true;
^~~~~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:58:6: error: redefinition of 'bool jogged'
bool jogged = false;
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:44:6: note: 'bool jogged' previously defined here
bool jogged = false;
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:60:5: error: redefinition of 'int JogLt'
int JogLt = A2;
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:46:5: note: 'int JogLt' previously defined here
int JogLt = A2;
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:61:5: error: redefinition of 'int JogRt'
int JogRt = A1;
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:47:5: note: 'int JogRt' previously defined here
int JogRt = A1;
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:62:5: error: redefinition of 'int Run'
int Run = A3;
^~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:48:5: note: 'int Run' previously defined here
int Run = A3;
^~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino: In function 'void setup()':
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:64:6: error: redefinition of 'void setup()'
void setup () {
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:51:6: note: 'void setup()' previously defined here
void setup () {
^~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino: In function 'void loop()':
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:82:6: error: redefinition of 'void loop()'
void loop () {
^~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:77:6: note: 'void loop()' previously defined here
void loop () {
^~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino: In function 'void rising()':
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:221:6: error: redefinition of 'void rising()'
void rising() {
^~~~~~
C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:226:6: note: 'void rising()' previously defined here
void rising() {
^~~~~~
Using library AccelStepper in folder: C:\Users\Ralph\Documents\Arduino\libraries\AccelStepper (legacy)
Using library elapsedMillis at version 1.0.6 in folder: C:\Users\Ralph\Documents\Arduino\libraries\elapsedMillis
exit status 1
Error compiling for board Arduino Uno.
/*
Change this!
AccelStepper Demo program. Created in response to a forum question.
Two unipolar steppers are connected as indicated in code. Five push buttons
are connected also as indicated. Motor movements can be programmed using buttons on
A1, A2, A4 and A5. The button presses will be recorded into a buffer. When button A3
is pressed, the programmed sequence will drive the motors using AccelStepper commands.
When all the motions are complete, another sequence may be entered and executed.
11/29/21 - jkl
*/
// Include the AccelStepper Library
#include <AccelStepper.h>
#include <elapsedMillis.h>
const int dirPin = 6;
const int stepPin = 7;
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin); // works for a4988 (Bipolar, constant current, step/direction driver)
//const int sensorPin = 9; // used as an end stop sensor output - driven by a timer
// hook to pin D2 - or can drive D2 directly (tie low, then disconnect to cause interrupt)
// define the time limit to run before stopping if using sensorPin
//const int timeLimit = 10; // number of seconds
// State definitions
#define READ_BUTTONS 01
#define RJSTR 02
#define JOG_CMD 03
#define STOP_NOW 04
#define JOG_RECOVER 05
// State variable
volatile int state; // must survive interrupts
elapsedMillis printTime;
volatile int enabFlag; // controls the end stop sensor interrupt
long int myStart = -5; // adjustthis as needed - code will handle it
long int endLimit = 300000; // Some nice long distance away - STOP will reset this //HERE
bool findLimit = true;
bool jogged = false;
//Button pin assignments
int JogLt = A2;
int JogRt = A1;
int Run = A3;
int Pot = A0;
void setup () {
Serial.begin(115200);
attachInterrupt(0, rising, RISING); // Interrupt 0 is pin D2
enabFlag = 1; // enable the sensor interrupt
state = READ_BUTTONS; // initial state is to read buttons
//myStepper.setMaxSpeed(400.);
//*
myStepper.setMaxSpeed(4000.);
//myStepper.setAcceleration(50.0);
//myStepper.setAcceleration(2500.0);
myStepper.setAcceleration(3000.0);
//*/
// Buttons
pinMode(JogRt, INPUT_PULLUP);
pinMode(JogLt, INPUT_PULLUP);
pinMode(Run, INPUT_PULLUP);
myStepper.moveTo (myStart );
myStepper.runToPosition();
Serial.println(myStepper.currentPosition());
}
void loop () {
switch (state) {
case READ_BUTTONS:
if ( digitalRead(JogRt) == LOW )
{
Serial.println("jog right");
//Serial.println(myStepper.currentPosition());
//myStepper.move (1 ); // right is assumed positive
//myStepper.move (100); // right is assumed positive
myStepper.move (100);
Serial.println(myStepper.targetPosition());
myStepper.setSpeed(100.); // this is the jog speed
state = JOG_CMD;
}
if ( digitalRead(JogLt) == LOW )
{
Serial.println("jog left");
Serial.println(myStepper.currentPosition());
//myStepper.move (-1 );
//myStepper.move (-100);
myStepper.move (-100);
Serial.println(myStepper.targetPosition());
myStepper.setSpeed(-100.);
state = JOG_CMD;
}
if ( digitalRead(Run) == LOW)
{
// if searching for the target limit, set it and then run back to START. Otherwise, just run and wait for STOP
if (findLimit) {
Serial.println("find stop");
Serial.print("start: ");
Serial.println(myStart);
Serial.print("end: ");
Serial.println(endLimit);
myStart = myStepper.currentPosition();
Serial.print("After set -- start: ");
Serial.println(myStart);
Serial.print("end: ");
Serial.println(endLimit);
if (jogged) {
state = JOG_RECOVER;
}
else {
myStepper.moveTo(endLimit);
enabFlag = 1; // enable the sensor interrupt
findLimit = false;
state = RJSTR;
}
}
else { // heading back to start
Serial.println("back to start");
Serial.print("start: ");
Serial.println(myStart);
Serial.print("end: ");
Serial.println(endLimit);
endLimit = myStepper.currentPosition();
Serial.print("After set -- start: ");
Serial.println(myStart);
Serial.print("end: ");
Serial.println(endLimit);
if (jogged) {
state = JOG_RECOVER;
}
else {
myStepper.moveTo(myStart);
enabFlag = 1; // enable the sensor interrupt
findLimit = true;
state = RJSTR;
}
}
}
delay(200); // tune for desired response time
break;
case JOG_RECOVER:
Serial.println("recovering");
myStepper.setCurrentPosition(myStepper.currentPosition());
if (findLimit) {
myStepper.moveTo(endLimit);
findLimit = false;
}
else
{
myStepper.moveTo(myStart);
findLimit = true;
}
enabFlag = 1; // enable the sensor interrupt
jogged = false;
state = RJSTR;
break;
case JOG_CMD:
Serial.println("jogging");
while (myStepper.distanceToGo() != 0) { // make the move before proceeding
myStepper.runSpeed();
}
//Serial.println(myStepper.currentPosition());
state = READ_BUTTONS; // read buttons again.
jogged = true;
break;
case RJSTR:
//Serial.println("running");
if (printTime >= 1000) { // happens once per second
printTime = 0;
// Use Pot to set myStepper.setMaxSpeed(?????);
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a the maximum speed range
int motorSpeed = map(sensorReading, 0, 1023, 5, 4000);
myStepper.setMaxSpeed(motorSpeed);
// set the motor speed:
Serial.print("====");
Serial.print(sensorReading);
Serial.print("====");
Serial.print(motorSpeed);
Serial.print("====");
//*/
Serial.print(myStepper.speed());
Serial.print(" ");
Serial.println(myStepper.currentPosition());
}
if (!myStepper.run()) {
state = READ_BUTTONS;
Serial.println("done with run");
}
// need to run until motor stops, then wait for another button press
// note that STOP button can intrude
break;
case STOP_NOW:
Serial.println("stopping");
//myStepper.setAcceleration(200.0); // this makes motor stop much quicker!
myStepper.setAcceleration(2000.0); // this makes motor stop much quicker!
myStepper.stop();
myStepper.runToPosition(); // brings to a stop!
//myStepper.setAcceleration(50.0); // slow motor acceleration back down
myStepper.setAcceleration(2000.0); // slow motor acceleration back down
state = READ_BUTTONS;
break;
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++ Interrupt service routine +++++++++++++++++++++++++++++
// Come here if rising edge on D2.
// If enable flag is true, enter state STOP_NOW
void rising() {
if (enabFlag == 1) {
state = STOP_NOW;
enabFlag = 0;
}
}
type or paste code here