Pages: [1] 2   Go Down
Author Topic: Sketch for Stepper motor??  (Read 1527 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 https://www.sparkfun.com/products/10267? 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...

Code:
#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);
}
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
https://www.sparkfun.com/products/10735

Just a word of warning...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for you help so far! :-) 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...

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);
 
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3477
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 :-) if you can't tell but am willing to learn

Code:
#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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 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;
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3477
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 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;

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
Code:
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.
Logged

Pages: [1] 2   Go Up
Jump to: