Pages: [1]   Go Down
Author Topic: Printing to LCD slows down stepper motors?  (Read 1194 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have written a simple sketch to control 2 small stepper motors with a joystick. The sketch works fine. Then I added my I2C LCD to print the movement of the stepper motors to the LCD. The sketch still compiles and works but there are some issues. My motors suddenly turn a lot slower. (they are not powered by the Arduino Uno) The printing to the LCD works but not like it should, it looks more like the LCD is flashing (the words I'm sending, not the blacklight). Could it be that the signals sent are to fast for the LCD to handle? (LCD= YWrobot LCM1602).
I saw in example LCD sketches that they use a delay(100) after printing to the LCD. How could I integrate such a thing into my code without it interfering with the rest of my program. Arduino is pretty new to me, I have no clue how to do this. Any pointers how I could do this?
My sketch:

Code:
// 2 stepper motors controlled by a joystick, write direction to I2C LCD

#include <Stepper.h>             //library for stepper motors
#include <Wire.h>                //library for I2C bus, needed for I2C LCD
#include <LiquidCrystal_I2C.h>   //library for I2C LCD

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27

const int stepsPerRevolution = 64;  // change this to fit the number of steps per revolution for your motor
                                         
//initialize the stepper library for both steppers:
Stepper small_stepperX(stepsPerRevolution, 8,10,9,11); 
Stepper small_stepperY(stepsPerRevolution, 3,5,4,6);



void setup() {

  //set the speed of the motor
  small_stepperX.setSpeed(200);    // set first stepper speed
  small_stepperY.setSpeed(200);   // set second stepper speed
 
 lcd.init();                     // initialize the lcd
 lcd.backlight();
}

void loop() {
 
  int joystickXReading = analogRead(A0); // read value from joystick X-axis
  int joystickYReading = analogRead(A1); // read value from joystick Y-axis

  if (joystickXReading < 490) { small_stepperX.step(1); lcd.print("backward"); }   // step backward
  if (joystickXReading > 540) { small_stepperX.step(-1); lcd.print("forward"); }  // step forward
 
  lcd.setCursor(0, 1);
   
  if (joystickYReading < 490) { small_stepperY.step(1); lcd.print("left"); }     // step left
  if (joystickYReading > 540) { small_stepperY.step(-1); lcd.print("right"); }  // step right
 
  lcd.clear();
 }
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lcd.print() is a blocking function. That is, it takes some time to send the data to the LCD and get a reply that the data was received. The function does not return until that reply is received. Printing after every step will slow things down.

lcd.clear() is also a blocking, and very slow function. You should not clear the screen any more than you absolutely have to.

Print shorter strings ("b", "f", "r", "l") if you must print anything. Get rid of the clear() function. Position the cursor, instead, and write over the text already there.
Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the fast reply. I've deleted lcd.clear() and I'm playing with lcd.setCursor and that definitely speeds up things. Are there no other ways to keep the lcd printing out of the main program?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use interrupts to control the time-critical stuff like the motor control.
I am still new to arduino myself and haven't yet done interrupt driven timesliced hardware control, still looking around how to do it best.
Basically, I need a timer interrupt somewhat like the 18.2 tick PC timer which I can attach my list of callback functions at.
Still not sure how to do it best.

But, here is an article you might find interesting to read:
http://forums.trossenrobotics.com/tutorials/how-to-diy-128/an-introduction-to-interrupts-3248/

Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use interrupts to control the time-critical stuff like the motor control.
I am still new to arduino myself and haven't yet done interrupt driven timesliced hardware control, still looking around how to do it best.
Basically, I need a timer interrupt somewhat like the 18.2 tick PC timer which I can attach my list of callback functions at.
Still not sure how to do it best.

But, here is an article you might find interesting to read:
http://forums.trossenrobotics.com/tutorials/how-to-diy-128/an-introduction-to-interrupts-3248/



That's an interesting article. I'm also new to Arduino. Building a small robot is the next step smiley
Logged


Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is a sad reality that the more you task your mpu the slower it will be at completing the task at large. Some things are and will always be best performed in hardware and merely have the mpu direct the show, not do the acting, stage lighting, and the orchestra too. In your final bot you may want an arduino dedicated to UI and one to Motor control, and one for System control. Dedicated stepper drive ICs are abundant in scope and are inexpensive and many require only 3 signals:Step,Direction, and Brake.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use interrupts to control the time-critical stuff like the motor control.
I am still new to arduino myself and haven't yet done interrupt driven timesliced hardware control, still looking around how to do it best.
Basically, I need a timer interrupt somewhat like the 18.2 tick PC timer which I can attach my list of callback functions at.
Still not sure how to do it best.

An easy way to get a regular tick interrupt is to use the MsTimer2 library.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An easy way to get a regular tick interrupt is to use the MsTimer2 library.

Thank you! This is what I need!
Logged

Pages: [1]   Go Up
Jump to: