Help Running Stepper Motor According to State of Digital Pin

Hello,

I am new to Arduino and I am currently tinkering with an Arduino UNO, a stepper motor, a stepper motor driver and a laser.

I would like to flash my laser on and off and run my stepper motor while the laser is powered off.

Here is my code:

const int DIR = 2; //Direction pin of the driver
const int PUL = 3; //Pulse pin of the driver
const int laser = 4; //Laser pin
int distance = 0; //the distance the motor has traveled since start
const int steps = 25000; //the number of steps per revolution
const int Speed = 10; //the time delay between pulses to the motor

void setup() {
pinMode(PUL, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(laser, OUTPUT);
digitalWrite(PUL, LOW);
digitalWrite(DIR, LOW);
}

void loop() {
digitalWrite(laser, LOW);
delay(3000);
digitalWrite(laser, HIGH);
delay(3000);

int laserstatus;
laserstatus = digitalRead(laser);

if (laserstatus == LOW)
{
digitalWrite(PUL, HIGH); //runs motor at set speed
delayMicroseconds(Speed);
digitalWrite(PUL, LOW);
delayMicroseconds(Speed);
}
}

The portion of code that runs the stepper (within the if statement) works just fine on its own.

Thank you.

I do not understand what you are asking.

This

  digitalWrite(laser, HIGH);
  delay(3000);

always writes the laser pin HIGH, so how do you expect

  int laserstatus;
  laserstatus = digitalRead(laser);
  if (laserstatus == LOW)

to ever be true?

Besides, you seem to declare the laser pin as an output, and then you read it as an input. This is permitted but unusual. What is your intention?

In the future…

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

Apologies for the incorrect formatting of my post.

Unless I'm misunderstanding something intrinsic about how this code is read by the Arduino, shouldn't

digitalWrite(laser, LOW);
delay(3000);
digitalWrite(laser, HIGH); 
delay(3000);

Create a condition such that:

if (laserstatus == LOW)

is true for 3 seconds?

What I am trying to do is run the if statement (powering the motor) when the laser pin is low.

Can you please explain what is unusual about reading the status of a pin declared as an output?

Thank you.

I would write:

void loop() {
  bool laserstatus;
  laserstatus = false;
  delay(3000);
  laserstatus = true);
  delay(3000);

  digitalWrite(laser,laserstatus);

  if (!laserstatus) // if NOT laserstatus

No real difference though, still won't work as is. :slight_smile:

conmcmon:
Apologies for the incorrect formatting of my post.

Unless I'm misunderstanding something intrinsic about how this code is read by the Arduino, shouldn't

digitalWrite(laser, LOW);

delay(3000);
digitalWrite(laser, HIGH);
delay(3000);




Create a condition such that: 



if (laserstatus == LOW)




is true for 3 seconds?

What I am trying to do is run the if statement (powering the motor) when the laser pin is low.

Can you please explain what is unusual about reading the status of a pin declared as an output?

Thank you.

Sure, that condition is true for three seconds. But you don't get to that line of code with the if statement during that three seconds. You only come to the line asking if laserstatus == low AFTER you have been low for three seconds and then made it high again.

Lines of code don't get all jumbled together and considered at the same time. Code runs line by line in the order things are written.

See if this does what you want, compiles but UNTESTED. BTW, are you sure you want to pulse the stepper at 50000 steps per second?:

  unsigned long timerStart;
  const int timerEnd = 6000, laserEnd = 3000;
  const int DIR = 2; //Direction pin of the driver
  const int PUL = 3; //Pulse pin of the driver
  const int laser = 4; //Laser pin
  int distance = 0; //the distance the motor has traveled since start
  const int steps = 25000; //the number of steps per revolution
  const int Speed = 10; //the time delay between pulses to the motor

void setup() {
  pinMode(PUL, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(laser, OUTPUT);
  digitalWrite(PUL, LOW);
  digitalWrite(DIR, LOW);
}

void loop() {
  while(millis() - timerStart < laserEnd)
  {
    digitalWrite(laser, LOW);
    digitalWrite(PUL, HIGH); //runs motor at set speed
    delayMicroseconds(Speed);
    digitalWrite(PUL, LOW);
    delayMicroseconds(Speed);
  }
  digitalWrite(laser, HIGH);
  if(millis() - timerStart > timerEnd)
    timerStart += timerEnd;

}

conmcmon:
I would like to flash my laser on and off and run my stepper motor while the laser is powered off.

Then you should never use delay() in your program.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

Also have a look at the second example in this Simple Stepper Code which shows how to step the motor without blocking.

...R
Stepper Motor Basics

Robin2:
Then you should never use delay() in your program.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

Also have a look at the second example in this Simple Stepper Code which shows how to step the motor without blocking.

...R
Stepper Motor Basics

Very useful info, Robin. Thanks for your help.

Delta_G:
Lines of code don't get all jumbled together and considered at the same time. Code runs line by line in the order things are written.

This is exactly what I was misunderstanding, thanks for the insight.