Interrupt not compiling

I have similar code with the exact same statement:

attachInterrupt(0, rising, RISING); // Interrupt 0 is pin D2

That compiles fine.

The ISR is the same in both sketches:

void rising() {
if (enabFlag == 1) {
state = STOP_NOW;
enabFlag = 0;
}

Arduino: 1.8.15 (Windows 10), Board: "Adafruit Metro"

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=adafruit:avr:metro -vid-pid=0403_6015 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_205495 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_500531 -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\AppData\Local\Temp\arduino_modified_sketch_605353\WORKING.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=adafruit:avr:metro -vid-pid=0403_6015 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_205495 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_500531 -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\AppData\Local\Temp\arduino_modified_sketch_605353\WORKING.ino

Using board 'metro' from platform in folder: C:\Users\Ralph\AppData\Local\Arduino15\packages\adafruit\hardware\avr\1.4.13

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_METRO -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_205495\\sketch\\WORKING.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_METRO -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_205495\\sketch\\WORKING.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_METRO -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_205495\\sketch\\WORKING.ino.cpp" -o nul

Using cached library dependencies for file: C:\Users\Ralph\Documents\Arduino\libraries\AccelStepper\AccelStepper.cpp

Using cached library dependencies for file: C:\Users\Ralph\Documents\Arduino\libraries\AccelStepper\MultiStepper.cpp

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_METRO -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_205495\\sketch\\WORKING.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_205495\\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_205495\\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_METRO -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_205495\\sketch\\WORKING.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_205495\\sketch\\WORKING.ino.cpp.o"

C:\Users\Ralph\AppData\Local\Temp\arduino_modified_sketch_605353\WORKING.ino: In function 'void setup()':

WORKING:32:21: error: 'rising' was not declared in this scope

  attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

                     ^~~~~~

C:\Users\Ralph\AppData\Local\Temp\arduino_modified_sketch_605353\WORKING.ino:32:21: note: suggested alternative: 'isinf'

  attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

                     ^~~~~~

                     isinf

C:\Users\Ralph\AppData\Local\Temp\arduino_modified_sketch_605353\WORKING.ino: In function 'void loop()':

WORKING:48:15: error: a function-definition is not allowed here before '{' token

 void rising() {

               ^

WORKING:122:3: error: expected '}' at end of input

   }

   ^

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

'rising' was not declared in this scope


Do you have a question?
Code?
Details?
Code tags?

Look carefully at the code and make sure every opening "{" has a corresponding "}"

The compiler is complaining bcs there is a "}" missing...

Pay attention to the words In function 'void loop()' ... a function-definition is not allowed.

I think I don't see matching braces { } in your snippet.

Autofort tool in the IDE is your friend. Also you can put the cursor on a brace and find its matching brace hilighted.

void rising() {
    if (enabFlag == 1) {
        state = STOP_NOW;
        enabFlag = 0;
    }

// uh oh...

a7

Thanks, I was looking for where to start looking. The error message is not very helpful.
There was a missing bracket at the very end which has been corrected.
I am still getting the same error.

Thanks for the replies.

Ralph

To get any actual help, you will need to post your code.

The compiler cannot read your mind, only the code you give it - and once you've confused the compiler it can only try its best to explain whats wrong - which it did as it pointed out a missing brace and that a function definition appeared not at top-level.

It rather impossible to help without seeing your full sketch. More often than not the problem is NOT where you think it is but somewhere else in your sketch.

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

You appear to have multiple tabs open (guess)

Please remember to use code tags when posting code.

Please, don't do anything that might strain you.

That is in most cases a PiCniC (Problem in Chair not in Computer) :wink:

I know that also, but found always that the damned compiler is strictly dumb but true ... Usually it is a missing semicolon or curly bracket somewhere that leads to those problems.

May I recommend to clean up the loop() a little bit? I am a fan of short(er) functions that are called inside loop() instead of squeezing everything into it. One of the advantages is that you get a better overview and avoid too many brackets/if clauses etc. in a row ...

Unfortunately the code you posted is not completely in between the code tags ... I tried to copy it into notepad++ to restore it, but cannot be sure I got the whole thing ... There are definitely parts missing because there are case statements, but no switch() to be found ...

If you have trouble to copy the code there are two helpful options:

  1. Create the code tags first
    image

copy the code to the clipboard, set the cursor between the code tags and paste the code.

  1. If you use the Arduino IDE
  • Mark the complete code (eg. by CTRL A)
  • Right click in the IDE Window to see the local menue
  • Use the "Copy for Forum" option in the menue
  • Paste the code in your post

That should make sure that you are on the safe side...

I used the code tags.

I copied the sketch to a new folder and new name.

tada the code compiles!!

I would like suggestions on cleaning up the loop().

Here is the original sketch with the compiler error:

// Include the AccelStepper Library
#include <AccelStepper.h>
#include <elapsedMillis.h>

// Motor Connections (constant current, step/direction bipolar motor driver)
const int dirPin = 6;
const int stepPin = 7;

AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);           // works for a4988 (Bipolar, constant current, step/direction driver)

// State definitions
#define READ_BUTTONS 01
#define RJSTR 02
#define JOG_CMD 03
// State variable
long STOP;
int state;
elapsedMillis printTime;

long int myStart = 0;
long int endLimit = 10000;
bool findLimit = true;
//Button pin assignments
int JogRt = A1;
int Run = A3;
int GoToStop = A4;
int Home =A2;

void setup () {
  Serial.begin(115200);
 attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2
  state = READ_BUTTONS;   // initial state is to read buttons
  myStepper.setMaxSpeed(400.);
  myStepper.setAcceleration(50.0);
 
  // Buttons
  pinMode(JogRt, INPUT_PULLUP);
  pinMode(Run, INPUT_PULLUP);
  pinMode(GoToStop, INPUT_PULLUP);
  pinMode(Home, INPUT_PULLUP);

  
}

void loop () {
  switch (state) {
    case READ_BUTTONS:
      if ( digitalRead(JogRt) == LOW )
      {
        Serial.println("jog right");
        myStepper.move (100 );    // right is assumed positive
        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;
      }
      if ( digitalRead(Run) == LOW)
      {
        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = false;
          
        }
        else
        {
          STOP = myStepper.currentPosition();        // WORKS records stop position but not JOG position.
          
          myStepper.moveTo(myStart);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = true;
        }       
        state = RJSTR;
      }
      delay(200);  // button response delay
      break;
    case JOG_CMD:
      Serial.println("jogging");
      myStepper.setSpeed(100);   // this will be the jogging speed
      while (myStepper.distanceToGo() > 0) 
      { // jog one step before proceeding
        myStepper.runSpeed();
        
        //STOP = myStepper.currentPosition();           // Prints 300
      }
      state = READ_BUTTONS;   // read buttons again.
      break;
    case RJSTR:
      //Serial.println("running");
      if (printTime >= 1000) 
      {    // happens once per second
        printTime = 0;
        Serial.print(myStepper.speed());
        Serial.print("  ");
        Serial.println(myStepper.currentPosition());
        //STOP = myStepper.currentPosition();
        Serial.print(STOP);
        Serial.print("--- ");
      }
      if (!myStepper.run()) 
      {
        state = READ_BUTTONS;
        Serial.println("done with run");
      }
      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;
  }
  //*/
}

Here is the error again:

Arduino: 1.8.15 (Windows 10), Board: "Adafruit Metro"

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=adafruit:avr:metro -vid-pid=0403_6015 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_10265 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_116717 -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\Z_axis\Z_axis.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=adafruit:avr:metro -vid-pid=0403_6015 -ide-version=10815 -build-path C:\Users\Ralph\AppData\Local\Temp\arduino_build_10265 -warnings=all -build-cache C:\Users\Ralph\AppData\Local\Temp\arduino_cache_116717 -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\Z_axis\Z_axis.ino

Using board 'metro' from platform in folder: C:\Users\Ralph\AppData\Local\Arduino15\packages\adafruit\hardware\avr\1.4.13

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_METRO -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_10265\\sketch\\Z_axis.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_METRO -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_10265\\sketch\\Z_axis.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_METRO -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_10265\\sketch\\Z_axis.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_METRO -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_METRO -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_METRO -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_10265\\sketch\\Z_axis.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_10265\\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_10265\\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_METRO -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_10265\\sketch\\Z_axis.ino.cpp" -o "C:\\Users\\Ralph\\AppData\\Local\\Temp\\arduino_build_10265\\sketch\\Z_axis.ino.cpp.o"

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\Z_axis\Z_axis.ino: In function 'void setup()':

Z_axis:31:21: error: 'rising' was not declared in this scope

  attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

                     ^~~~~~

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\Z_axis\Z_axis.ino:31:21: note: suggested alternative: 'isinf'

  attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

                     ^~~~~~

                     isinf

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\Z_axis\Z_axis.ino: In function 'void loop()':

Z_axis:113:15: error: a function-definition is not allowed here before '{' token

 void rising() {

               ^

Z_axis:119:1: error: expected '}' at end of input

 }

 ^

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

'rising' was not declared in this scope


If you comment out:


attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

And the ISR:

/*  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++ 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;
  }
  //*/

The code compiles!!

As has been suggested, you are missing a curly bracket at the end of loop, resulting in the ISR being seen by the compiler as part of loop.

E.g. change this

 switch (state) {
    case READ_BUTTONS:
      if ( digitalRead(JogRt) == LOW )
      {
        Serial.println("jog right");
        myStepper.move (100 );    // right is assumed positive
        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;
      }
      if ( digitalRead(Run) == LOW)
      {
        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = false;
          
        }
        else
        {
          STOP = myStepper.currentPosition();        // WORKS records stop position but not JOG position.
          
          myStepper.moveTo(myStart);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = true;
        }       
        state = RJSTR;
      }

to this

 switch (state) {
    case READ_BUTTONS:
        if ( digitalRead(JogRt) == LOW )   {     
              JogRight();  
        }
        if ( digitalRead(Run) == LOW)     { 
            RunLOW();
            state = RJSTR;
        }

// ...
//after end of loop()

void JogRight(){
        Serial.println("jog right");
        myStepper.move (100 );    // right is assumed positive
        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;
}

void RunLOW(){
        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = false;
        }
        else
        {
          STOP = myStepper.currentPosition();        // WORKS records stop position but not JOG position.
          
          myStepper.moveTo(myStart);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = true;
        }       
 }

If you create meaningful names for the functions you will find it much easier to follow what the sketch is (or should be) doing. The overview of open and close brackets becomes better as the number of lines decreases between the adjacent once also ...

But it is finally up to you how to do it ... just a hint ...

( I did this just in the forum as an example, I may have also "lost" some brackets ... :wink: )

David, how is the ISR being seen as part of the loop()?

https://photos.app.goo.gl/vuCwKUBwHVUcQu5y5

Ralph

[quote="ec2021, post:16, topic:966510"]

switch (state) {
    case READ_BUTTONS:
        if ( digitalRead(JogRt) == LOW )   {     
              JogRight();  
        }

Thanks but, since there is no debugger tp step through the code I need the Serial.print()'s.

Also I need the run commands:

 myStepper.move (10);    
        Serial.println(myStepper.targetPosition());
        myStepper.setSpeed(100.); // this is the jog speed
        

Post code, not pictures of code.

I posted the picture of the code that that I had posted previously to illustrate that the ISR was not in the loop.

Ralph