1. Got a question or need help troubleshooting? Post to the troubleshooting forum or Search the forums!

Arduino related code questions (sketches).

Discussion in 'Off Topic' started by BrooklynBay, Jul 1, 2021.

  1. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    Which way is the best way to set up these encoders? Some websites show them wired to pins 2 & 3. I saw some places mention interrupts in the code. Most schematics show a potentiometer/encoder combination. I saw code listing 0 - 179 or 0 - 1,023 steps added to the code while other places don't list steps but the display showed steps.
     
  2. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    The encoder type will determine how it needs to be wired and the steps are specific to the encoder. Also the data/code it reads back for position will be encoder specific too. So 0-179 could be valid for one encoder and 0-1023 might be valid for another. I've used more in the 0-1023 ranges than others, but they all work about the same. You read the pulses (codes) from the encoder and that tells you how many turns ...
     
  3. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I was combining code from two different websites, and I ran into several issues. I was setting up the LCD to look similar to the one here: https://circuitdigest.com/microcontroller-projects/interfacing-rotary-encoder-with-arduino. The code was written for a standard multi pin 16X2 display. My display is I2C, so I was attempting to combine the code with the one from this website: https://create.arduino.cc/projecthub/Arnov_Sharma_makes/lcd-i2c-tutorial-664e5a. I was attempting to add code for a second encoder, but it wouldn't allow the second encoder to run with interrupts. Error messages kept coming up about the LCD not being defined. The library was added to the code as well as the name of the library. I want to have the 16X2 display show stop switch activation, steps in numbers, and orientation in clockwise & counter clockwise on each separate line. The code in the website is set up with a regular multi connection LCD, and one encoder. Their LCD splits the data on two separate lines for one encoder. I didn't attempt to add code for the serial monitor yet since I was trying to get the LCD working first.
     
  4. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Yes, that was my concern with your use of the encoders.
    They require interrupt processing and while an Arduino Due might handle multiple interrupts I would expect that to be a challenge on an Uno or Leonardo. One interrupt is not a problem, but more than one can be dicey. The issue is how the Arduino handles interrupts and the ISR (interrupt service routine). Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have.

    If you are doing enough control that you need multiples you MIGHT want to consider something like a Teensy rather than an Arduino.
     
  5. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
  6. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    That link you posted pretty well describes the error. You have a class with a MAIN that contains another function declaration inside of it (or, if not MAIN then one of the member functions has another embedded within it).
    Post your code (and the entire error text from your compile) and I can take a look.
     
  7. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    Arduino: 1.8.13 (Windows 7), Board: "Arduino Leonardo"
    C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Meir\Documents\Arduino\libraries -fqbn=arduino:avr:leonardo -ide-version=10813 -build-path C:\Users\Meir\AppData\Local\Temp\arduino_build_371330 -warnings=none -build-cache C:\Users\Meir\AppData\Local\Temp\arduino_cache_543365 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\Meir\Desktop\PID_steering_control\PID_steering_control\PID_steering_control..ino\PID_steering_control..ino.ino
    C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Meir\Documents\Arduino\libraries -fqbn=arduino:avr:leonardo -ide-version=10813 -build-path C:\Users\Meir\AppData\Local\Temp\arduino_build_371330 -warnings=none -build-cache C:\Users\Meir\AppData\Local\Temp\arduino_cache_543365 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\Meir\Desktop\PID_steering_control\PID_steering_control\PID_steering_control..ino\PID_steering_control..ino.ino
    Using board 'leonardo' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
    Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
    Detecting libraries used...
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Alternatives for PinChangeInterrupt.h: [PinChangeInterrupt@1.2.6]
    ResolveLibrary(PinChangeInterrupt.h)
    -> candidates: [PinChangeInterrupt@1.2.6]
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Alternatives for TimerOne.h: [TimerOne-master@1.1]
    ResolveLibrary(TimerOne.h)
    -> candidates: [TimerOne-master@1.1]
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Alternatives for Wire.h: [Wire@1.0]
    ResolveLibrary(Wire.h)
    -> candidates: [Wire@1.0]
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Alternatives for LiquidCrystal_I2C.h: [LiquidCrystal_I2C-1.1.2@1.1.2]
    ResolveLibrary(LiquidCrystal_I2C.h)
    -> candidates: [LiquidCrystal_I2C-1.1.2@1.1.2]
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src\\PinChangeInterrupt.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src\\PinChangeInterrupt0.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
     
  8. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src\\PinChangeInterrupt1.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src\\PinChangeInterrupt2.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src\\PinChangeInterrupt3.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master\\TimerOne.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src\\Wire.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src\\utility\\twi.c" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2\\LiquidCrystal_I2C.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Generating function prototypes...
    "C:\\Program Files (x86)\\Arduino\\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=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\preproc\\ctags_target_for_gcc_minus_e.cpp" -DARDUINO_LIB_DISCOVERY_PHASE
    "C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\preproc\\ctags_target_for_gcc_minus_e.cpp"
    Compiling sketch...
    "C:\\Program Files (x86)\\Arduino\\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 -MMD -flto -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_LEONARDO -DARDUINO_ARCH_AVR -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_MANUFACTURER=\"Unknown\"" "-DUSB_PRODUCT=\"Arduino Leonardo\"" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\leonardo" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\PinChangeInterrupt\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\TimerOne-master" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Meir\\Documents\\Arduino\\libraries\\LiquidCrystal_I2C-1.1.2" "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp" -o "C:\\Users\\Meir\\AppData\\Local\\Temp\\arduino_build_371330\\sketch\\PID_steering_control..ino.ino.cpp.o"
    C:\Users\Meir\Desktop\PID_steering_control\PID_steering_control\PID_steering_control..ino\PID_steering_control..ino.ino: In function 'void setup()':
    PID_steering_control..ino:48:2: error: a function-definition is not allowed here before '{' token
    { lcd.init(); // initialize the lcd
    ^
    PID_steering_control..ino:79:5: error: expected initializer before 'setpoint'
    setpoint = map(analogRead(0), 0, 1024, 1024, 0) * 110; // setpoint position is made with a potentiometer but could be given by serial monitor or other...
    ^~~~~~~~
    PID_steering_control..ino:90:5: error: 'pwmOut' was not declared in this scope
    pwmOut(output); //Change the analog write for the motor control
    ^~~~~~
    PID_steering_control..ino:93:24: error: a function-definition is not allowed here before '{' token
    { void pwmOut(int out) { // to H-Bridge board
    ^
    PID_steering_control..ino:104:5: error: expected initializer before 'if'
    if (PINB & 0b00000001) encoderPos++; // if(digitalRead(encodPinB1)==HIGH) count ++; //We increase steps by 1
    ^~
    PID_steering_control..ino:105:5: error: 'else' without a previous 'if'
    else encoderPos--; // if (digitalRead(encodPinB1)==LOW) count --; //We decrease steps by 1
    ^~~~
    PID_steering_control..ino:109:1: error: a function-definition is not allowed here before '{' token
    {}
    ^
    PID_steering_control..ino:109:2: error: expected '}' at end of input
    {}
    ^
    Using library PinChangeInterrupt at version 1.2.6 in folder: C:\Users\Meir\Documents\Arduino\libraries\PinChangeInterrupt
    Using library TimerOne-master at version 1.1 in folder: C:\Users\Meir\Documents\Arduino\libraries\TimerOne-master
    Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire
    Using library LiquidCrystal_I2C-1.1.2 at version 1.1.2 in folder: C:\Users\Meir\Documents\Arduino\libraries\LiquidCrystal_I2C-1.1.2
    exit status 1
    a function-definition is not allowed here before '{' token
     
  9. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I had to post the full error code in two separate posts since it exceeded the maximum size of the post on this site. Here's the sketch:

    Code:
    //Include the libraries which we need
    #include <PinChangeInterrupt.h>               //http://electronoobs.com/eng_arduino_PinChangeInterrupt.php
    #include <PinChangeInterruptBoards.h>
    #include <PinChangeInterruptPins.h>
    #include <PinChangeInterruptSettings.h>
    #include <TimerOne.h> // Timer one interrupts
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h> (0x27,16,2);  // set the LCD address to 0x27 for a 16 character, 2 line display
    //////////////////////////////////////////////////////////////
    /////////////////////DEFINE PID constants/////////////////////
    //////////////////////////////////////////////////////////////
    int kp = 2, ki = 0.01, kd = 0.02;
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    
    int input, output, setpoint;
    int iTerm = 0, lastInput = 0, dInput = 0, error = 0;
    int outMin = -255, outMax = 255;
    int sampleTime = 10;                                //This values is in milliseconds
    volatile long encoderPos = 0;
    int Encoder_OutputA_CLK  = 2;
    int Encoder_OutputB_DT   = 3;
    int END_stop_SW          = 4;
    
    int Previous_Output;
    int Encoder_Count;
    
    //////////////////////////////////////////////////////////////
    ////////////////////Define the pins we use////////////////////
    //////////////////////////////////////////////////////////////
    #define Encoder_A_CLK  2                                    // Quadrature encoder A pin
    #define Encoder_B_DT   3                                    // Quadrature encoder B pin
    #define Encoder_A_CLK  0                                    // Quadrature encoder A pin
    #define Encoder_B_DT   1                                    // Quadrature encoder B pin
    #define Motor_CW       8                                    // PWM outputs to L298N H bridge motor driver module
    #define Motor_CCW      9
    #define Motor_CW       10
    #define Motor_CCW      11
    #define END_stop_SW    4
    
    
    
    void setup(void)
    {
    
     {void setup ()
    
     {  lcd.init();                      // initialize the lcd
        lcd.begin(16, 2); //Initialize 16*2 LCD
        // Print a message to the LCD.
        lcd.backlight();
        lcd.setCursor(1, 0);
    
        lcd.print(" Rotary Encoder "); //Intro Message line 1
        lcd.setCursor(0, 1);
        lcd.print("  With Arduino  "); //Intro Message line 2
    
        delay(2000);
        lcd.clear();
        pinMode(Encoder_A_CLK, INPUT);                                // quadrature encoder input A
        pinMode(Encoder_B_DT, INPUT);                                 // quadrature encoder input B
        pinMode(END_stop_SW, INPUT_PULLUP);                           // Input from the end stop switch
        attachPCINT(digitalPinToPCINT(Encoder_A_CLK), encoder, FALLING);  // We update encoder position for each falling edge detected in the interruption
        TCCR0B = TCCR0B & 0b11111000 | 1;                             // set 31Kh PWM to prevent motor whine (timer 2)
        Timer1.initialize(sampleTime * 1000);                         // setup timer 1
        Timer1.attachInterrupt(Compute);
        Serial.begin(115200);                                         //Just for debugging if you want to print values
        while (digitalRead(END_stop_SW))
        {
          analogWrite(Motor_CW, 0);                                   // Rotate the motor CCW
          analogWrite(Motor_CCW, 255);
        }
        analogWrite(Motor_CW, 0);                                     // Stop the motor
        analogWrite(Motor_CCW, 0);
      }
    
      void Compute()
        setpoint = map(analogRead(0), 0, 1024, 1024, 0) * 110;        // setpoint position is made with a potentiometer but could be given by serial monitor or other...
        input = encoderPos;                                           // we get the data from the encoder interruption
        error = setpoint - input;
        iTerm += ki * error * sampleTime;
        if (iTerm > outMax) iTerm = outMax;                           // prevent that the I term from PID gets too big
        else if (iTerm < outMin) iTerm = outMin;
        dInput = (input - lastInput) / sampleTime;
        output = kp * error + iTerm - kd * dInput;                    // The PID output is the sum of P I and D values
        if (output > outMax) output = outMax;                         // limit output to 0 and 255 for the analog write
        else if (output < outMin) output = outMin;
        lastInput = input;                                            //Remember to save the last input value for the next loop
        pwmOut(output);                                               //Change the analog write for the motor control
      }
    
    { void pwmOut(int out) {                                          // to H-Bridge board
          if (out > 0) {
            analogWrite(Motor_CW, out);                                 // Rotate the motor CW
            analogWrite(Motor_CCW, 0);
          }
          else {
            analogWrite(Motor_CW, 0);
            analogWrite(Motor_CCW, abs(out));                           // Rotate the motor CCW
          }
    }
        void encoder()                                      // pulse and direction, direct port reading to save cycles
        if (PINB & 0b00000001)    encoderPos++;             // if(digitalRead(encodPinB1)==HIGH)   count ++; //We increase steps by 1
        else                      encoderPos--;             // if (digitalRead(encodPinB1)==LOW)   count --; //We decrease steps by 1
      }
    
    void loop(void)
    {}
    
     
  10. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    There are certain issues with the sketch such as it doesn't show the interrupts for the second encoder, it shows code for a potentiometer which I'm not using, and it should show step numbers & position for each encoder on separate lines on the 16X2 LCD display. I was trying to combine 3 separate sketches from 3 different websites to run together, so it has issues.
     
  11. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Your problem is this:

    void setup(void)
    {

    {void setup ()


    You can't redeclare the setup() inside the code block for setup()

    :)
     
  12. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I was trying to remove different parts of it but then it caused errors in another section of the sketch.
     
  13. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    When I remove {void setup (), I get an LCD not declared error.
     
  14. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    You need to restructure your code to work correctly with the Arduino.
    You have all of the code in the setup() which is called once to initialize the device and then the loop() is where you add all of you code that will continuously run.

    here is a stab at it, but you need to customize it for your case. setup() is for just that -- setup all of your variables and initialize any other hardware (like your LCD). Then you perform the endless loop and scan sensors and react to them. The subroutines you have declared are fine (Compute, PWMOut, etc) but they should be called from in the loop() when you write it.

    Code:
    void setup(void)
    {
        lcd.init();                      // initialize the lcd
        lcd.begin(16, 2); //Initialize 16*2 LCD
        // Print a message to the LCD.
        lcd.backlight();
        lcd.setCursor(1, 0);
    
        lcd.print(" Rotary Encoder "); //Intro Message line 1
        lcd.setCursor(0, 1);
        lcd.print("  With Arduino  "); //Intro Message line 2
    
        delay(2000);
        lcd.clear();
        pinMode(Encoder_A_CLK, INPUT);                                // quadrature encoder input A
        pinMode(Encoder_B_DT, INPUT);                                 // quadrature encoder input B
        pinMode(END_stop_SW, INPUT_PULLUP);                           // Input from the end stop switch
        attachPCINT(digitalPinToPCINT(Encoder_A_CLK), encoder, FALLING);  // We update encoder position for each falling edge detected in the interruption
        TCCR0B = TCCR0B & 0b11111000 | 1;                             // set 31Kh PWM to prevent motor whine (timer 2)
        Timer1.initialize(sampleTime * 1000);                         // setup timer 1
        Timer1.attachInterrupt(Compute);
        Serial.begin(115200);                                         //Just for debugging if you want to print values
      }
    
      void Compute()
        setpoint = map(analogRead(0), 0, 1024, 1024, 0) * 110;        // setpoint position is made with a potentiometer but could be given by serial monitor or other...
        input = encoderPos;                                           // we get the data from the encoder interruption
        error = setpoint - input;
        iTerm += ki * error * sampleTime;
        if (iTerm > outMax) iTerm = outMax;                           // prevent that the I term from PID gets too big
        else if (iTerm < outMin) iTerm = outMin;
        dInput = (input - lastInput) / sampleTime;
        output = kp * error + iTerm - kd * dInput;                    // The PID output is the sum of P I and D values
        if (output > outMax) output = outMax;                         // limit output to 0 and 255 for the analog write
        else if (output < outMin) output = outMin;
        lastInput = input;                                            //Remember to save the last input value for the next loop
        pwmOut(output);                                               //Change the analog write for the motor control
      }
    
    { void pwmOut(int out) {                                          // to H-Bridge board
          if (out > 0) {
            analogWrite(Motor_CW, out);                                 // Rotate the motor CW
            analogWrite(Motor_CCW, 0);
          }
          else {
            analogWrite(Motor_CW, 0);
            analogWrite(Motor_CCW, abs(out));                           // Rotate the motor CCW
          }
    }
        void encoder()                                      // pulse and direction, direct port reading to save cycles
        if (PINB & 0b00000001)    encoderPos++;             // if(digitalRead(encodPinB1)==HIGH)   count ++; //We increase steps by 1
        else                      encoderPos--;             // if (digitalRead(encodPinB1)==LOW)   count --; //We decrease steps by 1
      }
    
    void loop(void)
    {
        while (digitalRead(END_stop_SW))
        {
          analogWrite(Motor_CW, 0);                                   // Rotate the motor CCW
          analogWrite(Motor_CCW, 255);
        }
        analogWrite(Motor_CW, 0);                                     // Stop the motor
        analogWrite(Motor_CCW, 0);
    }
    
     
  15. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I'm getting an LCD was not declared error message with the new code.
     
  16. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    This website said that the LCD has to be declared like this:

    Code:
    #include <LiquidCrystal_I2C.h> (0x27,16,2);  // set the LCD address to 0x27 for a 16 character, 2 line display
    LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
    This part is working but now I'm getting an error about the encoder not being declared. This is the line being highlighted:
    Code:
    attachPCINT(digitalPinToPCINT(Encoder_A_CLK), encoder, FALLING);  // We update encoder position for each falling edge detected in the interruption
    I was able to fix it by changing the code to:
    Code:
     attachPCINT(digitalPinToPCINT(Encoder_OutputA_CLK),Encoder_OutputA_CLK, FALLING);  // We update encoder position for each falling edge detected in the interruption
    Now I'm getting another error about the compute not being declared in this code:
    Code:
    Timer1.attachInterrupt(Compute);
    So far I wasn't able to correct this error.
     
    #76 BrooklynBay, Aug 3, 2021
    Last edited: Aug 3, 2021
    mark tomlinson likes this.
  17. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Post the entire code and I can look at it. The snippets will tell me where your problems are.
     
  18. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    Code:
    //Include the libraries which we need
    #include <PinChangeInterrupt.h>               //http://electronoobs.com/eng_arduino_PinChangeInterrupt.php
    #include <PinChangeInterruptBoards.h>
    #include <PinChangeInterruptPins.h>
    #include <PinChangeInterruptSettings.h>
    #include <TimerOne.h> // Timer one interrupts
    #include <Encoder.h>
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h> (0x27,16,2);  // set the LCD address to 0x27 for a 16 character, 2 line display
    LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
    
    //////////////////////////////////////////////////////////////
    /////////////////////DEFINE PID constants/////////////////////
    //////////////////////////////////////////////////////////////
    int kp = 2, ki = 0.01, kd = 0.02;
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    
    int input, output, setpoint;
    int iTerm = 0, lastInput = 0, dInput = 0, error = 0;
    int outMin = -255, outMax = 255;
    int sampleTime = 10;                                 //This values is in milliseconds
    volatile long encoderPos = 0;
    int Encoder_OutputA_CLK  = 2;                       // Quadrature encoder A pin
    int Encoder_OutputB_DT   = 3;                       // Quadrature encoder B pin
    int END_stop_SW          = 4;
    
    int Previous_Output;
    int Encoder_Count;
    
    //////////////////////////////////////////////////////////////
    ////////////////////Define the pins we use////////////////////
    //////////////////////////////////////////////////////////////
    #define Encoder_A_CLK  2                                    // Quadrature encoder A pin
    #define Encoder_B_DT   3                                    // Quadrature encoder B pin
    #define Encoder_A_CLK  0                                    // Quadrature encoder A pin
    #define Encoder_B_DT   1                                    // Quadrature encoder B pin
    #define Motor_CW       8                                    // PWM outputs to L298N H bridge motor driver module
    #define Motor_CCW      9
    #define Motor_CW       10
    #define Motor_CCW      11
    #define END_stop_SW    4
    
    void setup(void)
    {
      lcd.init();                      // initialize the lcd
      lcd.begin(16, 2); //Initialize 16*2 LCD
      // Print a message to the LCD.
      lcd.backlight();
      lcd.setCursor(1, 0);
    
      lcd.print(" Rotary Encoder "); //Intro Message line 1
      lcd.setCursor(0, 1);
      lcd.print("  With Arduino  "); //Intro Message line 2
    
      delay(2000);
      lcd.clear();
      pinMode(Encoder_A_CLK, INPUT);                                // quadrature encoder input A
      pinMode(Encoder_B_DT, INPUT);                                 // quadrature encoder input B
      pinMode(END_stop_SW, INPUT_PULLUP);                           // Input from the end stop switch
      attachPCINT(digitalPinToPCINT(Encoder_OutputA_CLK), Encoder_OutputA_CLK, FALLING); // We update encoder position for each falling edge detected in the interruption
      TCCR0B = TCCR0B & 0b11111000 | 1;                             // set 31Kh PWM to prevent motor whine (timer 2)
      Timer1.initialize(sampleTime * 1000);                         // setup timer 1
      Timer1.attachInterrupt(Compute);
      Serial.begin(115200);                                         //Just for debugging if you want to print values
    }
    
    void Compute()
    setpoint = map(analogRead(0), 0, 1024, 1024, 0) * 110;        // setpoint position is made with a potentiometer but could be given by serial monitor or other...
    input = encoderPos;                                           // we get the data from the encoder interruption
    error = setpoint - input;
    iTerm += ki * error * sampleTime;
    if (iTerm > outMax) iTerm = outMax;                           // prevent that the I term from PID gets too big
    else if (iTerm < outMin) iTerm = outMin;
    dInput = (input - lastInput) / sampleTime;
    output = kp * error + iTerm - kd * dInput;                    // The PID output is the sum of P I and D values
    if (output > outMax) output = outMax;                         // limit output to 0 and 255 for the analog write
    else if (output < outMin) output = outMin;
    lastInput = input;                                            //Remember to save the last input value for the next loop
    pwmOut(output);                                               //Change the analog write for the motor control
    }
    
    { void pwmOut(int out) {                                          // to H-Bridge board
        if (out > 0) {
          analogWrite(Motor_CW, out);                                 // Rotate the motor CW
          analogWrite(Motor_CCW, 0);
        }
        else {
          analogWrite(Motor_CW, 0);
          analogWrite(Motor_CCW, abs(out));                           // Rotate the motor CCW
        }
      }
      void encoder()                                      // pulse and direction, direct port reading to save cycles
      if (PINB & 0b00000001)    encoderPos++;             // if(digitalRead(encodPinB1)==HIGH)   count ++; //We increase steps by 1
      else                      encoderPos--;             // if (digitalRead(encodPinB1)==LOW)   count --; //We decrease steps by 1
    }
    
    void loop(void)
    {
      while (digitalRead(END_stop_SW))
      {
        analogWrite(Motor_CW, 0);                                   // Rotate the motor CCW
        analogWrite(Motor_CCW, 255);
      }
      analogWrite(Motor_CW, 0);                                     // Stop the motor
      analogWrite(Motor_CCW, 0);
    }
    
     
  19. mark tomlinson

    mark tomlinson ༼ つ ◕_ ◕ ༽つ
    Staff Member

    Joined:
    Feb 21, 2013
    Messages:
    23,421
    Likes Received:
    7,299
    Setup() is only for initializing devices. Do not try to use them there, rather do something like this:


    Code:
    void setup() {
     
      //initialize lcd screen
      lcd.init();
      // turn on the backlight
      lcd.backlight();
    }
    void loop() {
      //wait for a second
      delay(1000)
      // tell the screen to write on the top row
      lcd.setCursor(0,0);
      // tell the screen to write “hello, from” on the top row
      lcd.print(“Hello, From”);
    
     
  20. BrooklynBay

    BrooklynBay Active Member

    Joined:
    Apr 7, 2018
    Messages:
    410
    Likes Received:
    49
    I'm getting a stray 342 in program error now. Delay (1000) needs a semicolon ; at the end.
     
    #80 BrooklynBay, Aug 5, 2021
    Last edited: Aug 5, 2021

Share This Page