Arduino + LCD + Stepper motor (how to avoid the LCD to slow down the stepper motor)
My Arduino hit its own limits (?)... or my programming skills are crap.
And yes. My programming skills are crap, but I also think that my ATMEGA can't handle all the requirements that I am pushing for... but I think there is a real solution for my problem.
Either I can use it for a great LCD display or an awesome-ish step motor controller, but if I dare to use both (LCD or Serial output + stepper control) the stepper motors slows down to a ridiculous rate.
Here is a code example of my problem:
I want to run a stepper motor and output information on the (LCD or Serial) at the same time.
My Setup:
- Arduino Duemilanove
- LCD Keypad Shield (Pins: 8,9,4,5,6,7)
- 1 x EasyDriver (Pins 2 & 3)
- 1 x Stepper motor + DC power supply
Here is the code without LCD output:
#include <stdio.h>
#include <LiquidCrystal.h>
// By Staedtler
#define DIR1_PIN 2 // Direction for Motor A
#define STEP1_PIN 3 // Step for Motor A
#define DELAY 160 // Delay between pulses
void setup()
{ // Open Void Setup
pinMode(DIR1_PIN,OUTPUT); // Dir Motor A
pinMode(STEP1_PIN,OUTPUT); // Step Motor B
} // Close Void Setup
void loop()
{ // Opens 'Void loop'
int Revolutions; // We meed 'Revolutions' to be a integer number
int Steps; // We need 'Steps' to be a integer number
// Revolutions counter
for (Revolutions = 0; Revolutions < 100000 ; Revolutions++) // This is the Revolutions counter
{
for (Steps = 0; Steps < 1600 ; Steps++) // This is the Stepper counter where 1600 steps = 360 degrees for this stepper motor.
{ // Opens 'Steps' semi-loop
// We are about to make one step for a stepper motor here ...
digitalWrite(DIR1_PIN, LOW);
digitalWrite(STEP1_PIN, LOW);
digitalWrite(STEP1_PIN, HIGH);
delayMicroseconds(DELAY); // Pause in between pulses (Can be changed on '#define DELAY XXX''s line.
} // Close 'Steps'
delay(2000); // Pause for 2 seconds
} // Close 'Revolutions'
}
... here is the code with Serial output only:
#include <stdio.h>
#include <LiquidCrystal.h>
// By Staedtler
#define DIR1_PIN 2 // Direction for Motor A
#define STEP1_PIN 3 // Step for Motor A
#define DELAY 160 // Delay between pulses
void setup()
{ // Open Void Setup
Serial.begin(9600); // set up the Serial
pinMode(DIR1_PIN,OUTPUT); // Dir Motor A
pinMode(STEP1_PIN,OUTPUT); // Step Motor B
// set up the LCD //////////////////////
delay(1000); // Wait 1 second
} // Close Void Setup
void loop()
{ // Opens 'Void loop'
int Revolutions; // We meed 'Revolutions' to be a integer number
int Steps; // We need 'Steps' to be a integer number
// Revolutions counter
for (Revolutions = 0; Revolutions < 100000 ; Revolutions++) // This is the Revolutions counter
{
for (Steps = 0; Steps < 1600 ; Steps++) // This is the Stepper counter where 1600 steps = 360 degrees for this stepper motor.
{ // Opens 'Steps' semi-loop
// We are about to make one step for a stepper motor here ...
digitalWrite(DIR1_PIN, LOW);
digitalWrite(STEP1_PIN, LOW);
digitalWrite(STEP1_PIN, HIGH);
// So now, we'll thell to the Serial and LCD that you just made a step next ...
// ... go and tell Mr. Serial that we just made one step...
Serial.println("_______________________");
Serial.println("Steps:");
Serial.println(Steps);
delayMicroseconds(DELAY); // Pause in between pulses (Can be changed on '#define DELAY XXX''s line.
} // Close 'Steps'
delay(2000); // Pause for 2 seconds
} // Close 'Revolutions'
} // Close 'Void loop'
And here is the code with LCD output:
#include <stdio.h>
#include <LiquidCrystal.h>
// By Staedtler
LiquidCrystal lcd(8,9,4,5,6,7); // setup for 'LCD Keypad Shield', if you have a regular LCD display change to 'LiquidCrystal lcd(12, 11, 5, 4, 3, 2);'
#define DIR1_PIN 2 // Direction for Motor A
#define STEP1_PIN 3 // Step for Motor A
#define DELAY 160 // Delay between pulses
void setup()
{ // Open Void Setup
pinMode(DIR1_PIN,OUTPUT); // Dir Motor A
pinMode(STEP1_PIN,OUTPUT); // Step Motor B
// set up the LCD //////////////////////
lcd.begin(2, 16); // Set the size of the LCD
lcd.clear(); // Clear the screen
lcd.setCursor(0,0); // Set cursor for next line
lcd.print("StepperCalc-----"); // Print this line
lcd.setCursor(0,1); // Set cursor for next line
lcd.print("----------------"); // Print this line
delay(3000); // Wait 3 seconds
} // Close Void Setup
void loop()
{ // Opens 'Void loop'
int Revolutions; // We meed 'Revolutions' to be a integer number
int Steps; // We need 'Steps' to be a integer number
// Revolutions counter
for (Revolutions = 0; Revolutions < 100000 ; Revolutions++) // This is the Revolutions counter
{
lcd.clear();
lcd.setCursor(0,0); lcd.print("Steps:");
lcd.setCursor(6,0); lcd.print(Steps);
lcd.setCursor(0,1); lcd.print("Revolutions:");
lcd.setCursor(12,1); lcd.print(Revolutions);
for (Steps = 0; Steps < 1600 ; Steps++) // This is the Stepper counter where 1600 steps = 360 degrees for this stepper motor.
{ // Opens 'Steps' semi-loop
// We are about to make one step for a stepper motor here ...
digitalWrite(DIR1_PIN, LOW);
digitalWrite(STEP1_PIN, LOW);
digitalWrite(STEP1_PIN, HIGH);
// ... and tell Mr LCD that we just made a step here ...
lcd.clear();
lcd.setCursor(0,0); lcd.print("Steps:");
lcd.setCursor(6,0); lcd.print(Steps);
lcd.setCursor(0,1); lcd.print("Revolutions:");
lcd.setCursor(12,1); lcd.print(Revolutions);
delayMicroseconds(DELAY); // Pause in between pulses (Can be changed on '#define DELAY XXX''s line.
} // Close 'Steps'
lcd.clear();
lcd.setCursor(0,0); lcd.print("Steps:");
lcd.setCursor(6,0); lcd.print(Steps);
lcd.setCursor(0,1); lcd.print("Revolutions:");
lcd.setCursor(12,1); lcd.print(Revolutions+1);
delay(2000); // Pause for 2 seconds
} // Close 'Revolutions'
} // Close 'Void loop'
You can actually comment out either the Serial output or LCD, but the result will make no significant effect until both are eliminated.
Now, that's my dilemma. Every time that I try to run the code with the LCD, the stepper motor slows down.
Note that I am talking about the 360 degree process, after that there is an intended 2 seconds pause.
My beef is with the 360 degrees process with vs without LCD.
There is a technique to avoid slowing down the cycle?
My goal is simply to show the information of the stepper motor on the LCD without slowing down everything on the process.
My guts tells me that using different cycles is the key, but I am not that good.
I know there is a solution... I don't know how.
![]()
[edit]To add a picture
[/edit]