Go Down

Topic: Stepper Motor Code/Workaround when I use Sonar Ping and PulseIn (Read 199 times) previous topic - next topic

designo

Code: [Select]
/* the steppermotor runs fine and round when I
 * do not use sonar and/or pulsein lines.
 * when I use sonar and/or pulsein code, then
 * every 5 seconds, motor make a "clack"
 * sonarping AND pulsein both have probably
 * "delay-problems" in loop-section with steppermotor
 * driver - because? Too long sonar/pulsein responses?
*/

#include <NewPing.h>              // newping library
#include <dmtimer.h>              // dmtimer library
#define trigPin     1             // trigPin: pwm 1
#define echoPin     2             // echoPin: pwm 2
#define Switch      3             // Switch: pwm 3
#define DistanceMax 300

int distance;
unsigned int RC_Channel

NewPing sonar(trigPin, echoPin, DistanceMax);
DMTimer myTimer(5000000);         // 5000000 = 5 seconds

// ::
// steppermotor code
// ::

void setup(){
    pinMode(Switch, INPUT_PULLUP);
// ::
// steppermotor code
// ::
}

void loop(){
    if (myTimer.isTimeReached()){ // wait between sonar pings and pulsein checking
        distance = sonar.ping_cm();
        RC_Channel = pulseIn(Switch, HIGH, 25000);
    }
// ::
// steppermotor code
// ::
}

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

designo

Hi, this thread shouldn't be a discuss about "how to write a stepper motor code(..)".

In this forum there exist a lot of fine working stepper motor examples.

The core discussion element in this thread should be ONLY the
  • "multithreading", or better described as
  • delay/waiting response theme
A steppermotor never should have a "delay" in the loop code.

What is the basic programing techniques, when in one Arduino Sketch runs different "parallel things", like - .."waiting" for sonar (distance sensor) responses, "waiting" for pulsein responses, "waiting" for other things responses - all this in combination with «parallel running stepper code», steppermotor with appropriate motorshields like (Big) Easy Driver or similar (..)

Possible answers
  • use two or more arduino board(s)
  • use ...
  • concept behind...
  • idea...
  • ...
See more

Run stepper motor and X-sensors "simultaneously" » http://bit.ly/2vwv4Os

Core
1 | 2 | 3 | 4 (4 = this thread)

Similar
5 | 6 | 7 | 8 | 9 | 10 | 11

Kind regards
Alias «designo»

Robin2

Hi, this thread shouldn't be a discuss about "how to write a stepper motor code(..)".
If you prefer not to post your complete program then that is your prerogative.

There are plenty of other interesting questions on the Forum.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

designo

Sometimes is better to know first the theorie. I won't have a code (later yes - perhaps)! My code is only a dummy code for better understanding. And the concept is very important for a lot of people for most projects! In all my reference links there is no solution (..)

Robin2

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

graffeli

How to run a stepper motor with different sensors simultaneously. I try to show you two little concepts. Of course using dmtimer.h library or similar like millis() (..).

It's easier to make two graphics - enjoy.

@All - if you have some other ideas, please tell.

Concept 1 - run stepper motor and X-sensors „simultaneously"
ONE Arduino Board, millis(), dmtimer.h library or similar (..)


Pros and cons
  • Pros: one Arduino only
  • Cons: difficult for "calibrating stuff"

Concept 2
With TWO Arduino Boards, millis(), dmtimer.h library or similar (..)


Pros and cons
  • Pros: easy for calibrating
  • Cons: TWO Arduinos and coding overhead

Robin2

How to run a stepper motor with different sensors simultaneously. I try to show you two little concepts.
You don't seem to have considered reading the sensor between steps?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

graffeli

@Robin2 - yes, exactly. In concept 1, "reading the sensor between steps(..)" is not possible, because an arduino system is not a multitasking/multithreading system.

As user «designo» has described, pending reading/using a sensor in the loop section in ArduSketch, the response of sensor can be (or is) too long and the "motor/s make a «clack» (..)" - the motor has a millisecond coughing/interrupt.

Robin2

@Robin2 - yes, exactly. In concept 1, "reading the sensor between steps(..)" is not possible, because an arduino system is not a multitasking/multithreading system.
I agree it just has a single CPU, but you can certainly write code to do other things between steps - unless, of course, you ned a very high step rate.

You don't sit patiently in front of the oven while you wait for a chicken to cook :)


I have a program that controls 3 stepper motors for a small CNC lathe. If it could not do work between steps how could motors 2 and 3 move?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up