Stepper motor

When i insert the follow stepper motor code into my main program it doesn't run like it does when it is just the main motor code. Can anyone tell me why? When the motor code is by itself it runs smoothly and slow how i want it. But when put into my main program it runs even slower and jittery.

Is the arduino uno not powerful enough to handle everything im having it do?

The stepper code for running the stepper motor on the main program (attached file) starts on line 223

#include <Wire.h> 
#include "DHT.h"

void setup()
{      
  Wire.begin();
  
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);     
}

void loop()
{
  digitalWrite(9, HIGH);
  delayMicroseconds(11000); //70 minimal         
  digitalWrite(9, LOW); 
  delayMicroseconds(11000);  //70 minimal  
}

Thanks,

Time_Temp_LCD_V_1.1.ino (9.07 KB)

iuf you re-phrase that to :

my stepper code runs fine, but HERE IS MY PROGRAM that does not work...

posting code that does work does not give us anyhing to fix.

if you have any delay in your program, get rid of them,

if you have any serial write, comment it out and try again.

I don't see any stepper motor code there. What are you using for a driver ? If you had a chopper type driver it would have two control lines, a STEP and a DIR. If you were using something like an L293 or L298 it would have a total of 4 control lines (A,B,C,D) sometimes referred to as IN1,IN2,IN3, and IN4. In either case two lines are for one coil and the other two lines are for the second coil of the bipolar stepper motor. I don't see anything remotely similar to either in the code you posted.

1- What driver are you using ? (I'm talking about the HARDWARE driver)
2- Have you tested the stepper using the Stepper library example ?

/*
 Stepper Motor Control - one step at a time

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor will step one step at a time, very slowly.  You can use this to
 test that you've got the four wires of your stepper wired to the correct
 pins. If wired correctly, all steps should be in the same direction.

 Use this also to count the number of steps per revolution of your motor,
 if you don't know it.  Then plug that number into the oneRevolution
 example to see if you got it right.

 Created 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one step:
  myStepper.step(1);
  Serial.print("steps:" );
  Serial.println(stepCount);
  stepCount++;
  delay(500);
}

Note the 4 control pins :

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

my stepper code runs fine, but HERE IS MY PROGRAM that does not work...

posting code that does work does not give us anyhing to fix.

if you have any delay in your program, get rid of them,

if you have any serial write, comment it out and try again.

What Dave means (but is too tackful to come right out and say ) is :

Your code sucks. (to put it simply , it's garbage (in so far as the stepper motor is concerned. I have no comment about any non-stepper related code there.

I'm using an easy driver so only using 2 wires, STEP and DIR. Anytime i've tried any test code for the stepper it works fine, but when i implement it into my main program it never works properly and not too sure why. I have an LCD, temp sensor, time module, rotary encoder and easy driver connected so don't know if thats too much for the arduino or if the way i've done my coding isnt effective.

In case you haven't noticed, there is no STEP and DIR signals in the STEPPER library. You have to use a different library like Accelstepper:

//This is an example of how you would control 1 stepper

#include <AccelStepper.h>

int motorSpeed = 9600; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate

int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 



void setup(){
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
  
  stepper.moveTo(32000); //move 32000 steps (should be 10 rev)
}

void loop(){
  
  //if stepper is at desired location
  if (stepper.distanceToGo() == 0){
    //go the other way the same amount of steps
    //so if current position is 400 steps out, go position -400
    stepper.moveTo(-stepper.currentPosition()); 
  }
  

  
  //these must be called as often as possible to ensure smooth operation
  //any delay will cause jerky motion
  stepper.run();
}

Accelstepper

If someone knows a way to use the stepper library that uses 4 control pins with a driver that uses only a STEP and a DIR signal, I'd love to hear it because AFAIK, they are
APPLES & ORANGES.

Well i switched to using the accellstepper library. Same issue as before when just the motor code works how i want but when in my main program it doesn't. I'm going to assume that the issue is how i've implemented the motor run code in the loop isn't done properly with all the other code i have after it?

Time_Temp_LCD_V_1.0.ino (7.52 KB)

sorry if it feels like we are picking on you.

your steps work in solo mode because the timing is based on only your delay and execution time.

in your program ,the timing if altered by the writing to the LCD.

so the problem is the timing in the combined program

is there any way to get around that? I do have a second ardunio to use but if it's possible with one arduino would be my preferred way.

Dude, WTF ?! What are you thinking ? How can you have two totally different stepper libraries in the same sketch ? Every line of code related to the Stepper library must be removed (and presumably replaced by some Accelstepper equivilent). The code you put the red box around is Stepper library code. That is obvious because it starts with the word "Stepper" (stepper.runspeed(); Look at the Accelstepper code I posted. Does it say
"stepper.runspeed" ? No, it doesn't. It says :

 stepper.setSpeed(motorSpeed);

is there any way to get around that? I do have a second ardunio to use but if it's possible with one arduino would be my preferred way.

Obviously, the only way you are going to believe us is for you to start commenting out ALL of the LCD print commands, in stages.

put "// " in front of the LCD code, and test your stepper. Comment out ALL of the LCD code to start and then test the stepper, and if it works fine , then go back and remove the "//" for SOME of the LCD code and then test the stepper. By trial and error , you should be able to confirm for yourself whether of not the issue is a timing issue.

Hi,

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

Can you please post a picture of your project?

What is your stepper?
What is your stepper (hardware) driver.

Tom.... :slight_smile:

I agree with Reply #9. Almost certainly all those LCD calls are slowing things down.

You don't need to update the LCD in every iteration of loop().

You should put all the LCD code into a separate function (or a few functions) and only call the LCD functions once per second or so.

Have a look at planning and implementing a program.
Your aim should be code that allows loop() to repeat hundreds or thousands of times per second.

You could also call the LCD functions from setup() thus shortening your code and reducing the risk of typos.

...R

The uC does what YOU tell it to do Nothing more, nothing less.

Well i've implented functions for pretty much everything in my setup and loop which has fixed the lagging issue. Just now have to find a way to call my temp_function in the loop and not have it interupt the motor as it is when being called.

Time_Temp_LCD_V_1.2.ino (7.79 KB)

Eurotrax:
Well i've implented functions for pretty much everything in my setup and loop which has fixed the lagging issue. Just now have to find a way to call my temp_function in the loop and not have it interupt the motor as it is when being called.

It seems to me there are two options.

Use a flag or another timer (with millis() ) so that temp_function() is never called in the same iteration of loop() when temp_minMax() is called. That way you spread the burden.

Better (IMHO) would be to take all the LCD stuff out of temp_function()

I don't know if the call to dht.readTemperature() takes an appreciable time.

...R

An ATmega328 chip with the OPTIBOOT bootloader preinstalled is only $5.

Maybe you could add a second uP.

raschemmel:
An ATmega328 chip with the OPTIBOOT bootloader preinstalled is only $5.
Maybe you could add a second uP.

and at least $150 worth of extra trouble getting it all to work. :slight_smile:

...R

That's not really true. All you need it the 18pF caps and the crystal and a few other caps.
Breadboarding ATmega328
total cost <$20

although the idea of using a second uP may be flawed.

raschemmel:
That's not really true. All you need it the 18pF caps and the crystal and a few other caps.
Breadboarding ATmega328
total cost <$20

although the idea of using a second uP may be flawed.

I was not thinking of the cost of supplementary items but of the extra time the OP would need to get it all working.

...R

It's plug&play (almost)