Offline
Newbie
Karma: 0
Posts: 39
|
 |
« on: February 15, 2013, 07:18:58 pm » |
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... #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
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #1 on: February 15, 2013, 08:06:09 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #2 on: February 15, 2013, 08:10:01 pm » |
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
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #3 on: February 15, 2013, 10:02:22 pm » |
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
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #4 on: February 16, 2013, 07:57:24 am » |
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/10735Just a word of warning...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #5 on: February 16, 2013, 12:01:29 pm » |
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... #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
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #6 on: February 16, 2013, 12:15:30 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #7 on: February 16, 2013, 12:34:37 pm » |
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 #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
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #8 on: February 16, 2013, 12:42:51 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #9 on: February 16, 2013, 01:32:07 pm » |
hey I got it!!! :-) 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; }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 48
Posts: 1408
May all of your blinks be without delay
|
 |
« Reply #10 on: February 16, 2013, 01:39:18 pm » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #11 on: February 17, 2013, 04:17:19 pm » |
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;
}
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #12 on: February 17, 2013, 04:25:35 pm » |
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
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #13 on: February 17, 2013, 04:35:51 pm » |
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
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #14 on: February 17, 2013, 04:58:20 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|