Sketch for Stepper motor??

Ok I am working on a project and this is what I would like to do.

When pir sensor senses motion this stepper motor https://www.sparkfun.com/products/9238? with this motor driver EasyDriver - Stepper Motor Driver - ROB-12779 - SparkFun Electronics? would turn 90 degrees and stay there until the sensor goes low(stops sensing motion).

My question is... how do I write a simple sketch to accomplish this?

Here is my sketch that I want to insert the new stepper sketch into...

#define DIR_PIN 2
#define STEP_PIN 3

const int sensorPin = 4;
const int ledPin = 13;

void setup() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
} 

void loop(){ 
  
  if(digitalRead(sensorPin) == HIGH) {
    digitalWrite(ledPin, HIGH);
  }

if(digitalRead(sensorPin) == LOW) {
  digitalWrite(ledPin, LOW);
}
}

Have you looked at the example sketches for stepper motors? Moving a stepper motor to a specified position is not exactly a novel requirement and it would be pretty bizarre if nobody had done it before, don't you think?

yes I have looked at those sketches but all they do is either go by one step at a time or keep rotating back and forth. And that is not what I want. I want to be able to tell the motor to turn to position x and stay there all day if need be...not keep moving.

So use that as a starting point. You now know how to make the motor move by a step. Next you want to make it move by the right number of steps, and then stop. Think about how you'd do that in code.

I have two of those same stepper motors, and two of those same stepper motor drivers. Mine have that horrid burnt electronics smell that you never really want to smell. They are far too small for those motors.

I now have two of these, although I have yet to use them.

Just a word of warning...

Thanks for you help so far! :slight_smile: I have written a simple code to control the stepper and have it stop at 90, 180, 270, and 360 degrees then rotate back to the original starting position. It delays 1 second at each spot but I want it to stay there for as long as the pir stays high. If I take out the " delay(1000);" then it just keeps spinning until the pir goes low. Does this make sense? I will be glad to clarify more!

Here is the code...

#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int pos = 50;
int pos2 = 50;
int pos3 = 50;
int pos4 = 50;
int pos5 = -200;

void setup() {
  
  myStepper.setSpeed(60);
  
}

void loop() {
  
  myStepper.step(pos);
  delay(1000);
  myStepper.step(pos2);
  delay(1000);
  myStepper.step(pos3);
  delay(1000);
  myStepper.step(pos4);
  delay(1000);
  myStepper.step(pos5);
  delay(1000);
  
}

Your stepper motor is doing what it is told, although there are much neater ways of doing it.

The original sketch that you posted turned on an LED when the sensor value went HIGH, now you need to merge the 2 ideas together. You know how to read the sensor and act on it. You know how to make the motor move. You are nearly there.

here is my latest sketch.... i'm getting hung up on my delay function or do I need to use a variable to store the value of the "pirpin"?? I am fairly new to this :slight_smile: if you can't tell but am willing to learn

#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int ledpin = 13;
int pirpin = 4;

int pos = 50;
int pos2 = -50;

void setup() {
  
  myStepper.setSpeed(60); // sets speed of stepper
  
  pinMode(ledpin, OUTPUT);
  pinMode(pirpin, INPUT);
  
}

void loop() {
  
  if (digitalRead(pirpin) == HIGH) {
    digitalWrite(ledpin, HIGH);
    myStepper.step(pos);
  }
  
  if(digitalRead(pirpin) == LOW) {
    digitalWrite(ledpin, LOW);
    myStepper.step(pos2);
  }
}

i'm getting hung up on my delay function

Right now - forget that delay() exists. You NEVER need to use it. There are alternatives that are far better.

do I need to use a variable to store the value of the "pirpin"?

Yes. Two actually. One to save the current state and one to save the previous state.

Then, you do stuff only when the current state is not the same as the previous state. What is does (step forward or step backward) depends on the current state.

hey I got it!!! :slight_smile: thanks for ya'lls help!!! here is the code i came up with...

#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int ledpin = 13;
int pirpin = 4;
int pirstate = 0; // variable to store current pir state
int lastpirstate = 0; // variable to store last pir state
int pos = 50;
int pos2 = -50;

void setup() {
  
  myStepper.setSpeed(60); // sets speed of stepper
  
  pinMode(ledpin, OUTPUT);
  pinMode(pirpin, INPUT);
  
}

void loop() {
  
  pirstate = digitalRead(pirpin);
  
  if(pirstate != lastpirstate) {
    if(pirstate == HIGH) {
    digitalWrite(ledpin, HIGH);
    myStepper.step(pos);
  } else {
    digitalWrite(ledpin, LOW);
    myStepper.step(pos2);
  }
}
lastpirstate = pirstate;
}

Well done.
As Paul said you never need to use delay(), not that you are at the moment. The alternative, which does not block other code running whilst the delay() times out uses a technique that checks frequently whether the required amount of time has passed since an action started and, if not, carries on. Have a look at the BlinkWithoutDelay example to see how it is done.

Ok thanks for your help and now that I have got that figured out I would like to add a few more pir sensors. Here is what I want to accomplish... When pir#1 goes high(senses motion) the stepper will turn to that direction and stop until it goes low then return to original position. Ok so far the sketch I have works perfectly for that. Now I add pir#2... when pir#1 is high and pir#2 goes high then it rotates to far. If pir#1 is low and pir#2 goes high then the stepper turn to the right position and works fine. Does this make sense?? Here is the code...

do I need to use interrupts to accomplish this?

thanks

#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int ledpin = 13;
int pirpin = 4;
int pirstate = 0; // variable to store current pir state
int lastpirstate = 0; // variable to store last pir state
int pirpin2 = 7;
int pirstate2 = 0;
int lastpirstate2 = 0;
int LED = 12;

void setup() {
  
  myStepper.setSpeed(60); // sets speed of stepper
  
  pinMode(ledpin, OUTPUT);
  pinMode(pirpin, INPUT);
  pinMode(pirpin2, INPUT);
  pinMode(LED, OUTPUT);
  
}

void loop() {
  
  pirstate = digitalRead(pirpin);
  
  if(pirstate != lastpirstate) {
    if(pirstate == HIGH) {
    digitalWrite(ledpin, HIGH);
    myStepper.step(50);
  } else {
    digitalWrite(ledpin, LOW);
    myStepper.step(-50);
  }
}
lastpirstate = pirstate;

  pirstate2 = digitalRead(pirpin2);
  
  if(pirstate2 != lastpirstate2) {
    if(pirstate2 == HIGH) {
      digitalWrite(ledpin, HIGH);
      myStepper.step(-100);
    } else {
      digitalWrite(ledpin, LOW);
      myStepper.step(100);
    }
  }
lastpirstate2 = pirstate2;

}

Now I add pir#2... when pir#1 is high and pir#2 goes high then it rotates to far. If pir#1 is low and pir#2 goes high then the stepper turn to the right position and works fine. Does this make sense??

No, it doesn't. Steppers step. Describing what you want in terms of what the device is capable of makes a lot more sense.

Like so:
When PIR one sense motion, I want the stepper to step 90 times, clockwise. Then, when PIR one senses no motion, I want the stepper to step 90 times, counterclockwise.

Now, it you describe, in the same way, what you want when PIR two senses motion, and what you want when PIR two senses no motion, we could help.

Ok sorry for being unclear....

when pir1 senses motion i want stepper to step 50 steps counter clockwise
when pir1 is low(no motion) and pir2 senses motion i want stepper to step 100 steps counter clockwise
when pir1 is senses motion (already at the 50 step counter clockwise position) and then pir2 senses motion I want stepper to step 50 steps counter clockwise (to reach the position it supposed to be at when just pir2 senses motion.)

does that make more sense?

thanks

does that make more sense?

Not really. There are several possibilities. PIR1 is HIGH and PIR2 is LOW. PIR1 is LOW and PIR2 is LOW. PIR1 is LOW and PIR2 is HIGH. And, PIR1 is HIGH and PIR2 is HIGH.

If you use a series of if statements, like so:

int oneState = digitalRead(onePin);
int twoState = digitalRead(twoPin);

if(oneState == LOW && twoState == LOW)
{
  // Do something
}
else if(oneState == HIGH && twoState == LOW)
{
  // Do something else
}
else if(oneState == LOW && twoState == HIGH)
{
  // Do something else
}
else //oneState == HIGH && twoState == HIGH)
{
   // Do something else
}

That covers all the possibilities. All you need to do is fill in what to do in each state.

Thanks PaulS when PIR1 is HIGH and PIR2 is HIGH is when i get confused on my programming... I will try your sketch!! thanks!

Ok I have tried that sketch but I can't figure it out... I need help trying to figure out how to keep track of how many steps the stepper takes from when it is powered up and if I can get that then I should be able to get the rest... any ideas on how to do this? Here is my code....

#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int ledpin = 13;
int pirpin = 4;
int pirstate = 0; // variable to store current pir state
int lastpirstate = 0; // variable to store last pir state
int pirpin2 = 7;
int pirstate2 = 0;
int lastpirstate2 = 0;
int bothpirstate = 0;
int lastbothpirstate = 0;


void setup() {
  
  myStepper.setSpeed(60); // sets speed of stepper
  
  pinMode(ledpin, OUTPUT);
  pinMode(pirpin, INPUT);
  pinMode(pirpin2, INPUT);
}

void loop() {
  
  int pirpinstate = digitalRead(pirpin);
  int pirpin2state = digitalRead(pirpin2);
  const int pos1 = 50;
  const int pos2 = 100;
  
  
 pirstate = digitalRead(pirpin);
  
  if(pirstate != lastpirstate) {
    if(pirstate == HIGH) {
    digitalWrite(ledpin, HIGH);
    myStepper.step(pos1);
  } else {
    digitalWrite(ledpin, LOW);
    myStepper.step(-pos1);
  }
}
lastpirstate = pirstate;

  pirstate2 = digitalRead(pirpin2);
  
  if(pirstate2 != lastpirstate2) {
    if(pirstate2 == HIGH) {
      digitalWrite(ledpin, HIGH);
      myStepper.step(pos2);
    } else {
      digitalWrite(ledpin, LOW);
      myStepper.step(-pos2);
    }
  }
lastpirstate2 = pirstate2;
}

dewyoder18:
I need help trying to figure out how to keep track of how many steps the stepper takes from when it is powered up and if I can get that then I should be able to get the rest... any ideas on how to do this?

Your program is what is telling the stepper to move using the myStepper.step method. The method takes an argument, so if after each move you add the number of the argument to a variable that is initialised to zero you will know how many steps it has taken won't you ?