How to application delay code in a part

Hi~ I’m riemeo lee

I have some question about arduino coding

I want to use delay code in a part
but delay and delayMicroseconds code are applied all loop area

How do i do?

arduino code

#include <PololuLedStrip.h>

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<12> ledStrip;

// Create a buffer for holding the colors (3 bytes per color).
#define LED_COUNT 60
rgb_color colors[LED_COUNT];

float amplitude = 75.0;  // Height of wave
float period = 500.0;  // How many pixels before the wave repeats
float g = 0.0;
float theta = 0.0;
float dx;  // Value for incrementing X, a function of period and xspacing
float r;

int dirPin1 = 3;
int stepperPin1 = 2;
int dirPin2 = 7;
int stepperPin2 = 6;

void setup()
{
  pinMode(dirPin1, OUTPUT);
  pinMode(stepperPin1, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(stepperPin2, OUTPUT);
  
  Serial.begin(9600);
  dx = (TWO_PI / period);
}

void step(boolean dir,int steps){
  digitalWrite(dirPin1,dir);
  digitalWrite(dirPin2,dir);
  delay(50);
  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin1, HIGH);
    digitalWrite(stepperPin2, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepperPin1, LOW);
    digitalWrite(stepperPin2, LOW);
    delayMicroseconds(100);
  }
}

void loop()
{
  
  //cicle count
  step(true,1600*5);
  //right , left cicle delay
  delay(500);
  //cicle count
  step(false,1600*5);
  //right , left cicle delay
  delay(500);
  
  theta += 0.005;
  float x = theta;
  
  r = (1+sin(x))*127;
  x+=dx;
  Serial.println(r);
  
  rgb_color color;
  color.red = r;
  color.green = r;
  color.blue = r;

  // Update the colors buffer.
  for(uint16_t i = 0; i < LED_COUNT; i++)
  {
    colors[i] = color;
  }
  // Write to the LED strip.
  ledStrip.write(colors, LED_COUNT);

}

In this code

I use delay, delayMicroseconds code for control stepper motor
but delay and delayMicroseconds code make delay calculation int ‘r’
so… I want how to no delay calculation int ‘r’

thank you

The answer is, as neary always, get rid of delay, and look at the blink without delay example, or the example posted at the top of the project guidance section

  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin1, HIGH);
    digitalWrite(stepperPin2, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepperPin1, LOW);
    digitalWrite(stepperPin2, LOW);
    delayMicroseconds(100);

What stepper motor driver board are you using?

I strongly suspect that the above code could be rewritten as

  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin1, HIGH);
    digitalWrite(stepperPin2, HIGH);
    digitalWrite(stepperPin1, LOW);
    digitalWrite(stepperPin2, LOW);
    delayMicroseconds(200);

which would put all of the interval between steps (200 usecs) in one place. That will make it much easier to rewrite the code using the BWoD technique.

If you want to do other things while the stepper is moving don’t do all the steps in a single call

step(true,1600*5);

Instead do one step in each iteration of loop() and keep count of where you are in a variable in loop() - not in step(). Of course that only works provided loop repeats far more rapidly than the interval between steps. Hence the need NOT to use delay() anywhere - or delayMicroseconds() for anything longer than (say) 20usecs.

Of course it is possible that the only way to get the stepper to move fast enough is to give it exclusive use of the Arduino during every move.

The demo several things at a time illustrates the use of the BWoD technique. It works just as well with micros() as with millis().

…R

Thank you AWOL I given big hint in your advice^^ Thank you very much

Hellow Robin2

Thank you in your advice

I’m using ITEAD DUAL STEP MOTOR DRIVER SHIELD Arduino shild
link: http://imall.iteadstudio.com/im120417015.html

and this code(stepper motor control) be found internet

and I test code

for(int i=0;i<steps;i++){
    digitalWrite(stepperPin1, HIGH);
    digitalWrite(stepperPin2, HIGH);
    digitalWrite(stepperPin1, LOW);
    digitalWrite(stepperPin2, LOW);
    delayMicroseconds(200);

it is show me good function

and I try your code(in your link) but
I don’t know
It can be application in this arduino shield

thank you very much

If you want your program to do other things while the stepper motor is stepping, you need to use a non-blocking method of moving the stepper motor. That code snippet will no NOTHING but move the stepper motor. Lots of other stuff could be done, while delayMicroseconds() is twiddling it's thumbs.

The AccelStepper library handles moving the stepper(s) without blocking the rest of the program.

dnflrkwhr888: and I try your code(in your link) but I don't know It can be application in this arduino shield

You will need to explain what you don't understand in more detail if I am to help.

As I said earlier and, others have also said, you MUST replace your use of delay() and probably delayMicroseconds() to get your project to work properly.

But without a better explanation of what you want to achieve it will be difficult to advise you.

I suggest that you put your own project to one side for a day or two and learn fully how to use the Blink Without Delay technique. When you are familiar with it you should be able to see how to use it in your project.

...R

Not a single conditional statement!

Impressed :)