Simple stepper control using Bourns rotary encoder & LCD V2

I have a similar problem as troylefevre in this thread Simple stepper control using Bourns rotary encoder & LCD - Programming Questions - Arduino Forum

My Code

#include <LiquidCrystal_I2C.h>  // I2C LCD Library by Francisco Malpartida https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#include <AccelStepper.h>  // AccelStepper Library https://www.arduinolibraries.info/libraries/accel-stepper


#define I2C_ADDR 0x27  // I2C address of typical I2C LCD Backpack

// LCD Pins to I2C LCD Backpack - These are default for HD44780 LCD's
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

// Create instance for LCD called: i2c_lcd
LiquidCrystal_I2C i2c_lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


const int pinSTEP=11;  // STEP pin of EasyDriver connected to pin 11 of UNO
const int pinDIR=10;  // DIR  pin of EasyDriver connected to pin 10 of UNO

AccelStepper stepper(1, pinSTEP, pinDIR);  // Stepper setup

// Include the Bourns EAW Encoder library and maps
#include <ACE128.h>  // https://github.com/arielnh56/ACE128

#include <ACE128map12345678.h> // mapping for pin order 12345678

// Pin x of UNO connected to pin x of Encoder (example: UNO pin 2 connected to pin 1 of encoder, etc...)
ACE128 myACE(2,3,4,5,6,7,8,9, (uint8_t*)encoderMap_12345678);


int16_t multiturn_encoder_value;  // Variable to hold multiturn value of encoder (-32768 to 32767)


void setup() {
  i2c_lcd.begin (16,2); //  our LCD is a 16x2, change for your LCD if needed
  
  // LCD Backlight ON
  i2c_lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  i2c_lcd.setBacklight(HIGH);
  
  i2c_lcd.clear(); // Clear the LCD screen

  stepper.setCurrentPosition(0);  // Set current position of stepper at startup to zero
  stepper.setMaxSpeed(1000.0);      // Set Max Speed of stepper
  stepper.setAcceleration(5000.0);  // Acceleration of stepper
  stepper.setSpeed(1000.0);  // Speed of stepper

  myACE.begin();    // initialize the encoder library
}


void loop() {

  multiturn_encoder_value = myACE.mpos();  // get multiturn value from encoder
  stepper.moveTo(multiturn_encoder_value); // set stepper new position to move to
    
  while (stepper.distanceToGo() != 0) {  // if stepper hasn't reached new position
    stepper.runSpeedToPosition();  // move the stepper until new position reached
  }

// Display the encoder multiturn and stepper position on LCD
  i2c_lcd.setCursor(0,0);
  i2c_lcd.print("Encoder: ");
  i2c_lcd.setCursor(9,0);
  i2c_lcd.print(multiturn_encoder_value);
  i2c_lcd.print("      ");
  i2c_lcd.setCursor(0,1);
  i2c_lcd.print("Stepper: ");
  i2c_lcd.setCursor(9,1);
  i2c_lcd.print(stepper.currentPosition()); 
  i2c_lcd.print("      ");  
}

I made the edit suggested by MorganS
After reviewing the library on GitHub and the error message more closely, it seems like you need to edit a line in the library's .h file to enable the "Arduino Pins" option. Find the line below in the .h file and remove the "//" at the beginning of the line...

Code: (Find me) [Select]
// #define ACE128_ARDUINO_PINS

I looked for the library he suggested but could not find it.

My error

Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

In file included from C:\Users\MARK\Documents\Arduino\Encoder\Encoder.ino:27:0:

C:\Users\MARK\Documents\Arduino\libraries\ACE128\src/ACE128.h:66:12: fatal error: EEPROM.h: No such file or directory

#include <EEPROM.h>

^~~~~~~~~~

compilation terminated.

exit status 1

Error compiling for board Arduino Nano.

Looking for some guidance.

Fixed. I found a copy of EEPROM.h
IDE 1.8.13 didn't have this library in the library manager.

round1:
Fixed. I found a copy of EEPROM.h
IDE 1.8.13 didn't have this library in the library manager.

It comes bundled with the IDE for the AVR based boards.

--- bill

If you are using a Stepper and an LCD you can run into problems when updating the LCD slows the loop() code
See my Multi-tasking in Arduino for a detailed stepper example which adds extra calls to run() to avoid/reduce the problems.

Not all boards support EEPROM

Here is the pfodEEPROM.h file I use

#ifndef pfodEEPROM_h
#define pfodEEPROM_h

#if defined( ARDUINO_ARCH_AVR ) || defined( ARDUINO_ARCH_ESP8266 ) || defined( ARDUINO_ARCH_ESP32 ) || defined( ARDUINO_ARCH_ARC32 ) || defined( TEENSYDUINO )
// NOTE: Teensy does not need to use begin() or end() but no harm in calling them 
#include <EEPROM.h>
#else
// all other arch not explicitly mentioned do no support EEPROM.H
// they may support FLASH EEPROM simulation but that is not included here
#define __no_pfodEEPROM__
#endif

#endif //pfodEEPROM_h

drmpf:
If you are using a Stepper and an LCD you can run into problems when updating the LCD slows the loop() code
See my Multi-tasking in Arduino for a detailed stepper example which adds extra calls to run() to avoid/reduce the problems.

Not all boards support EEPROM

Here is the pfodEEPROM.h file I use

#ifndef pfodEEPROM_h

#define pfodEEPROM_h

#if defined( ARDUINO_ARCH_AVR ) || defined( ARDUINO_ARCH_ESP8266 ) || defined( ARDUINO_ARCH_ESP32 ) || defined( ARDUINO_ARCH_ARC32 ) || defined( TEENSYDUINO )
// NOTE: Teensy does not need to use begin() or end() but no harm in calling them
#include <EEPROM.h>
#else
// all other arch not explicitly mentioned do no support EEPROM.H
// they may support FLASH EEPROM simulation but that is not included here
#define no_pfodEEPROM
#endif

#endif //pfodEEPROM_h

Thanks, I was planning on using the structure that Robin2 laid out in this thread once everything functioned.
I have more to add.

drmpf Thanks for the link.

Apart from how Robin2 reads Serial, the approach is very similar. Break your work up into tasks and have the loop() code continually check each one to see if there is something to do.

The loopTimer() class in Multi-tasking in Arduino is useful for tracking down which task is slowing up your code and for checking you are calling the AccelStepper run() method often enough.

Watch out for Serial print's blocking your loop. Again the loopTimer() will identify that.
My tutorial Arduino Serial I/O for the Real World takes you through the solutions to that.

Thanks again drmpf.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.