Hello all, this is my first post in a good while. I do hope I've put this in the correct spot - if not let me know with info on how to move topic to correct place.
I've got a powerfeed for a milling machine (Grizzly G-619) project using a Nano, NEMA 24 stepper motor, and DM556S driver. The code has worked good for a couple years. I've decided it would be good to put an LCD display for motor RPM and Inches Per Minute (IPM). For testing I used serial monitor to display RPM and IPM. That works good.
BUT, when I use a LCD with I2C the motor then the info is displayed on the LCD, but the update is slow. Also the motor only turns while the LCD update is taking place. Motor turns left in 1/8 turn steps, pauses until next update (couple of seconds or so), then another 1/8th turn. Pressing STOP button stops.
Even with all the lcd portion and from "lcd.backlight(); commented out, and ONLY the "lcd.begin();" line active it doesn't work. That single line "lcd.begin();" causes the problems as described above. As if there were something in the "<LiquidCrystal_I2C.h>" that's causing the problem. Is that when the library actually becomes active when the "lcd.begin();" line is invoked?
I test each section by commenting out (/....../) each unused section.
I edited post to put code in proper window
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 8, 9); //8=PUL, 9=DIR "AccelStepper::DRIVER" cam be changed to "1" ok
// Define our three input button pins
#define LEFT_PIN 5
#define STOP_PIN 4
#define RIGHT_PIN 3
#define ENA 7 //So the stepper will turn freely when STOP is pressed.
// Define our analog pot input pin
#define SPEED_PIN A0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 5000 //this was set to 500, but I changed to higher number, might set higher later
#define MIN_SPEED 0.0 //This was set to 0.1, but I changed to 0 so rotation would stop at min setting
float current_speed = 0.0; // Holds current motor speed in steps/second
int analog_read_counter = 1000; // Counts down to 0 to fire analog read
char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
int analog_value = 0; // Holds raw analog value.
int rpm = 0;
float ips = 0.0;
void setup() {
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (ENA, OUTPUT);
}
void loop() {
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0) {
sign = 1; digitalWrite(ENA, HIGH);
}
else if (digitalRead(RIGHT_PIN) == 0) {
sign = -1; digitalWrite(ENA, HIGH);
}
else if (digitalRead(STOP_PIN) == 0) {
sign = 0; digitalWrite(ENA, LOW);
}
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0) {
analog_read_counter--;
}
else {
analog_read_counter = 3000; //originally at 3000
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// Give the stepper a chance to step if it needs to
stepper1.runSpeed();
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
rpm = (analog_value / 5.5297);
ips = (analog_value / 56.8333);
Serial.print("RPM = ");
Serial.println(rpm);
Serial.println();
//rpm = (analog_value / 5.297); //These numbers will need fiddling with to provide around IPM speed
Serial.print("IPM = "); //the (F) reduces the amount of dynamic memory used
Serial.print(ips); //serial print works good, prints everything as it should and runs motor smooth
Serial.println();
Serial.println();
}
/*
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(2, 0); // set the LCD cursor position
lcd.print("RPM = ");
lcd.print(rpm);
lcd.setCursor(2, 1); // set the LCD cursor position
lcd.print("IPM = ");
lcd.print(ips, 1);
}
*/
// This will run the stepper at a constant speed
stepper1.runSpeed();
}