stepper doesn't run with serialprintln

I have an LSM303 and stepper (28BYJ-48) running in the sketch below. It works fine until I insert either the Serial.println() or delay() or both. The monitor prints the lines but the stepper doesn’t move. If I comment out the two lines it works fine again. (I’ve moved the serialprintln to different locations same result)

Am using the adafruit LSM303 break out, with SCL on A4 and SDA on A5, it’s on the 3.3v output from the Uno. The stepper uses D8-D11 and a ULN2003 that part works fine.

Code:

I’ll probably get flak for my poor format…

#include <Wire.h>
#include <LSM303.h>
#include <CustomStepper.h>

CustomStepper director(8, 9, 10, 11, (byte[]){8, B1000, 1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4096, 10, CW);

LSM303 compass;

float directorvector=0;
float directorvectorlast=0;
float directorturn=0;
int turndirection=0;
float addturn=0;
float directorlastturn=0;






void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
  compass.m_min = (LSM303::vector<int16_t>){-556, -778, -609};
  compass.m_max = (LSM303::vector<int16_t>){+810, +496, +663};
}

void loop() 
{
  compass.read();
  float heading = compass.heading();



   directorvector=-heading;

//Serial.println(directorvector);  do not know why but serial print makes it not work
//delay(10);
      
if (director.isDone())
  {
   directorturn=directorvector-directorvectorlast;
   directorvectorlast=directorvector; 

    if(directorturn>350)
      {
      directorturn=directorturn-360;  
      }

    if(directorturn<-350)
      {
      directorturn=directorturn+360;
      }

   
    if(directorturn<0)
      {
      director.setDirection(CCW);
      directorturn=-directorturn;    
      }
    else
      {
      director.setDirection(CW);  
      }

    
    if(directorturn>1)
    {
      director.setRPM(1);
    } 
    if(directorturn>2)
    {
      director.setRPM(3);
    } 


    if(directorturn>5)
    {
      director.setRPM(5);
    } 

    if(directorturn>10)
    {
      director.setRPM(10);
    } 

if(directorturn>20)
{
  directorturn=0;
}
    
   director.rotateDegrees(directorturn);
  }



director.run();



}
[code]

video of motor/compass operating (w/o serialprintln)

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

capricorn2:
I’ll probably get flak for my poor format…

Yes.

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

…R

PS it does seem strange to continue with a Post when you know you are doing it wrong and have not asked how to do it right.

Sorry!! Holy cow. I figured someone here would be pretty quick to see a problem with it from the setup.

PS: my comment regarding getting flak had to do with the code writing itself, not how it was put in the thread, if I'd have known I did it wrong I wouldn't have done it...

TomG I'll do a quick drawing, but I think it has to do with the comm. Thx, Cap

Here’s my drawing, hopefully I won’t get burned alive :slight_smile:

This is not flak :slight_smile:

Image from Reply #5 so we don’t have to download it. See this Image Guide

3f7ef106514991f324890aafced1b4a31dece3b7.jpg

…R

Your code will be much easier to read (for you and for us) if you use the AutoFormat tool to indent it consistently.

I am not familiar with the CustomStepper library.

...R

Okay Robin, I'm a fool, can't upload a photo correctly either. Yes autoformat probably would help you but I'm a hack, when I read it I see it, and I don't use the autoformat because I don't like it. Sorry for wasting your time, no need to respond. Cap

Hi capricorn2.

People are trying to help you, so be patient with them as they are with you.
Perhaps they are a bit straight forward (so am i), but the replies until now have not been abusive in any way.
You see, if you need help, you do need to help the ones willing to help you.
You can do so by posting things in the correct way, and it can be hard to do that while you're still new to the forums.
And that's no problem at all, you will be pointed to the way to improve your posting skills as you have seen already.
If you do not like a formatted code, open an other instance of the code you have, and do an autoformat so you can post it here.
Close the 2nd. instance without saving it, so now you can have it both ways.

As to your problem:

I can't be sure, because i have no idea about the LSM303 compass and customstepper library.
But delay and serial communication depend on a timer.
I'm also not sure if they use the same timer, but it is a commonality and could be the reason why using any of these interferes with the execution of your code.
Do you really need the delay there (or: why is it in there) ?
The delay is quite short, but there's hardly a good reason to use it anyway.
If you need the printed value, do you really need it printed to the serial monitor ?
Or will an other means to have the value represented do as well ?
Maybe display it on a LCD screen, or have the value represented on an analog pin and read it out using a multimeter (a DC range will show a nice average if you put the analog pin into a not too high impedance load, but do not leave it unloaded).
There's got to be a few more possible ways next to the serial port to catch this data.

Well thanks Mas for the response. Not to be coy but if you read the post you'd see that the delay() or the Serial.println(), or both causes the stepper to stop functioning. I've no use for the delay() whatsoever, I've seen it inserted before along with Serial.println(), assumed it sort of left time for the command to complete or some such thing. In any case I do appreciate the time and effort of your looking at it and suggesting something. I'll get by with or without the println command, it was just the sort of oddity I thought I might find a quick explanation for here. Peace. Cap

Hi.

As stated, delay and serial communications depend on a timer (an internal one, not one that you have to handle or create yourself).
Using that timer might interfere with your stepper library, that's the point i'm making.
But i can't be sure because i don't know about that stepper library.
Perhaps you can find out at the source of that library.

It seems to me you are sending a command to the library, and then check to see the command has been completely executed.
That's this part:

if (director.isDone())
  {

Check to see whether that happens, so if that test results in anything else but a 0.
If the test fails (results in a 0), then a new value for the director to process will not be created.

Check perhaps by adding a resistor and LED to some free pin and have it lit depending on the result of this test.
Or sending it to serial too.

These are debugging measures, which can be taken out of the sketch once your problem has been identified and /or solved.
A serial connection or a LCD can be powerful debugging aids.

Normally I don't like any kind of auto-format or pretty-printing. However the Arduino one is very good. If it screws up your code then your code was wrong. It's as simple as that.

Thanks Mas, Morgan, I figured it was either something with the serial comm interfering with the SDA/SCL or the "stepper is done" probably the latter. The customstepper.h Igor Campos. I'll try to track it down myself. I apologize if I was being a jerk, I had, what I thought was a fairly simple question, probably is, just takes days to solve. I'll relook at the arduino autoformat, I think it's on when I use it, I just reformat it as I go so I can read it... Thanks again, Cap

capricorn2:
Okay Robin, I'm a fool, can't upload a photo correctly either.

That's not true and I took the trouble to say so. You did not know how to post a photo so I did not mind doing it for you and providing a link to the instructions.

...R

Hi.

The I2C (SCL/SDA) is in use for your compass.
If you see changing compass data in serial monitor, then that part works.

So it is relatively easy to find out whether your problem is in that part of your sketch.

You are right Robin, I'm sorry. I was thinking the problem was relatively simple and someone here would have a quick solution, already know about the issue or something. So I went back and looked at the customstepper.h and the thread with it, I should have to begin with. The problem was noted there, but I did not see a solution posted.

Mas, you are correct the problem is with the customstepper library or how it uses the timer etc.

Odd thing is I have a much larger sketch for a different project, it uses customstepper.h and 4 motors, and has serialprint in it and works just fine, it doesn't have the compass though. Unfortunately that sketch uses all the A pins so adding the compass in is painful.

Thanks for your responses. I'll work on it myself for a while and will post if I am able to pin it down so others can possibly benefit.

Partial success

If I put serial print in after "stepper is done" check it works, but I loose serial comm while the stepper is running.

This is at the end of the sketch

if(directorturn>20)
{
  directorturn=0;
}
    
   director.rotateDegrees(directorturn);
  }



director.run();

if (director.isDone())
{
Serial.println(heading);  
Serial.println(directorturn); 
}

}

I wonder if switching to the AccelStepper library would avoid the problem?

...R

Thanks for sticking in there Robin, you're a gem :slight_smile: I think you say wonder, but mean: use accelStepper, and you are likely quite right, I'm just stubborn but will do as suggested. My little project didn't depend on the serial comm, so I'm almost there, but will look at the alternative library. Thanks again for all your help, all of you. Cap (I'll post a video of the dumb thing when it's done...)