Offline
Full Member
Karma: 0
Posts: 123
|
 |
« on: February 25, 2013, 12:38:52 pm » |
I am trying to control a unipolar stepper with two buttons, one on either side of a salvaged typwriter cage. The sketch will run it one one direction but the button does not reverse the motor. Thanks for advise. #include <Stepper.h> Stepper s(20,8,9,10,11); int button1 = 1; int button2 = 2; int buttonState1 = 0; int buttonState2 = 0;
void setup() { pinMode(1,INPUT); //buttons pinMode(2,INPUT); pinMode(8,OUTPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT);
s.setSpeed(500); s.step(1500);//200 //delay(5000); }
void loop() { buttonState1 = digitalRead(button1); buttonState2 = digitalRead(button2);
if (buttonState1 == HIGH) { s.setSpeed(-500); s.step(1500);//200 //delay(1000); }
if (buttonState2 == HIGH) { s.setSpeed(500); s.step(-1500); delay(1000); }
}
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #1 on: February 25, 2013, 12:49:28 pm » |
pinMode(1,INPUT); //buttons pinMode(2,INPUT); No internal pullup resistors. So, you need external resistors. How are the "buttons" wired? Why not uses switches, instead? Indenting is a good thing.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #2 on: February 25, 2013, 01:37:30 pm » |
Forget the buttons for now.
Write a sketch that turns the motor in one direction for a period then reverses it for a period and repeats. You will then know that you can control the motor and can introduce the buttons. Your sketch is considerably more complicated than the example from the stepper library. Do you really need to set the pinMode of pins 8 to 11 for example ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #3 on: February 25, 2013, 03:21:57 pm » |
Yes, I have external, 10K pull down resistors. I edited the code a bit but it originally was running a back and forth pass at the proper speed. When I manually push the buttons, it will only advance the motor, not change direction.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #4 on: February 25, 2013, 03:58:41 pm » |
Your method of reversing the motor seems very complicated The example from the stepper library manages to do it like this void loop() { myStepper.step(stepsPerRevolution); delay(500); myStepper.step(-stepsPerRevolution); delay(500); } When you do this s.setSpeed(-500); s.step(1500); Is it by any chance equivalent to s.setSpeed(500); s.step(-1500); Negative speed and positive steps. Positive speed and negative steps
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #5 on: February 25, 2013, 04:56:09 pm » |
Yes, that seems simple enough but how do I plug in the buttons?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #6 on: February 25, 2013, 06:07:15 pm » |
The buttons and direction is moving correctly but this sketch will only move a limited number of steps rather than back and forth. A sonar sensor input will initiate and stop the motor. #include <Stepper.h>
int button1 = 1; int button2 = 2; int buttonState1 = 0; int buttonState2 = 0;
const int stepsPerRevolution = 100;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() { pinMode (button1,INPUT); pinMode (button2,INPUT); // set the speed at 60 rpm: myStepper.setSpeed(80); myStepper.step(stepsPerRevolution); //delay(500); } void loop() { buttonState1 = digitalRead (button1); buttonState2 = digitalRead (button2); if(buttonState2 == HIGH) { // step one revolution in one direction: myStepper.step(stepsPerRevolution); //delay(500); } if (buttonState1 == HIGH) { // step one revolution in the other direction: myStepper.step(-stepsPerRevolution); //delay(500); } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #7 on: February 25, 2013, 06:22:41 pm » |
A sonar sensor input will initiate and stop the motor. How? but this sketch will only move a limited number of steps rather than back and forth. That sketch would only go back and forth (whatever than means for a motor that rotates) if you alternated which switches you pressed. You still haven't said how the switches are wired. You need to state exactly what happens when you press ONE of the switches, and exactly what happens when the press the other switch (and when you press that switch - while the stepper is moving or after it completes a rotation). Please keep in mind that we are not trying to harass you. We can't see the hardware you have, or what it (or you) is/are doing.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #8 on: February 25, 2013, 07:12:23 pm » |
Sorry, my description of the problem is often another problem! Here is a photo of the setup http://www.flickr.com/photos/50454200@N06/8508893686/ You can see the blue button at the far left of the carriage. The stepper is mounted on the slide which presses the buttons on either end. I want the mechanism to slide back and forth, limited only by the buttons which send it in the opposite direction. Now it will only go as far as "myStepper.step(stepsPerRevolution);"
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #9 on: February 25, 2013, 07:20:34 pm » |
What you need to do is know which way you are stepping. On each pass through loop, see if the "Stop, you've hit the end" switch is pressed. If not, step ONCE. If so, change the direction.
That way, each pass through loop you read one of the switches (the one that the carriage is moving towards) and step or change direction without stepping.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #10 on: February 25, 2013, 07:53:24 pm » |
I understand what you are explaining but am at a near complete loss how to program the solution. How does the sketch determine the current direction?
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #11 on: February 26, 2013, 02:55:25 am » |
How does the sketch determine the current direction? Because you tell it. loop start moving right while rightSwitch not pressed move a step to the right end of while start moving left while leftSwitch not pressed move a step to the left end of while end of loop
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #12 on: February 26, 2013, 04:25:03 pm » |
I tried to adapt the example stepper sketch without success.
#include <Stepper.h>
const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution // for your motor int buttonOne = 1; int buttonTwo = 2; int buttonValOne = 0; int buttonValTwo = 0;
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int stepCount1 = 0; int stepCount2 = 0;// number of steps the motor has taken
void setup() { pinMode (buttonOne, INPUT); pinMode (buttonTwo, INPUT); }
void loop() { buttonValOne = (digitalRead, buttonOne); buttonValTwo = (digitalRead, buttonTwo); while (buttonValOne !=HIGH) { // step one step: myStepper.step(1); stepCount1++; delay(500); }
while (buttonValTwo !=HIGH) { // step one step: myStepper.step(1); stepCount1--; delay(500);
} }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #13 on: February 26, 2013, 05:59:54 pm » |
I can't see anything in your code that reverses the direction of the motor.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 123
|
 |
« Reply #14 on: February 27, 2013, 12:17:16 pm » |
Forgot to add the "-" --------myStepper.step(-1); If I change "if" to "while", the buttons don't respond but if continually pressed manually, they do operate as intended. Of course they should I want just a momentary switch to send in in reverse. Thanks for the help!
|
|
|
|
|
Logged
|
|
|
|
|
|