Go Down

Topic: stepper motor control (Read 1 time)previous topic - next topic

Joes

Sep 10, 2012, 10:19 pm
hi there i have made this code up,i would like to add another function, when a switch pin goes high the stepper will return to its 0 position and when the pin goes low the stepper will return the where the pot is set to. how do we do this?

Code: [Select]
` #include <Stepper.h>// change this to the number of steps on your motor#define STEPS 100Stepper stepper(STEPS, 8, 9, 10, 11);// the previous reading from the analog inputint previous = 0;void setup(){  // set the speed of the motor to 30 RPMs  stepper.setSpeed(150);}void loop(){  // get the sensor value  int potVal = analogRead(0);  int stpPos = map(potVal, 0, 1023, 0, 100);  stepper.step(stpPos - previous);  // remember the previous value of the sensor  previous = stpPos;}`

sdturner

#1
Sep 11, 2012, 01:41 am
You want an if-else statement. If the input is high, undo the steps you've made, else if the input is low, go to the pot position:

Code: [Select]
`#include <Stepper.h>// change this to the number of steps on your motor#define STEPS 100Stepper stepper(STEPS, 8, 9, 10, 11);// the previous reading from the analog inputint previous = 0;void setup(){  // set the speed of the motor to 30 RPMs  stepper.setSpeed(150);  pinMode(1, INPUT);            // This is the input pin}void loop(){  // get the sensor value  int potVal = analogRead(0);  int stpPos = map(potVal, 0, 1023, 0, 100);    // I added this if-else statement  if (digitalRead(1) == HIGH) {    stepper.step(-previous);  }  else {    stepper.step(stpPos - previous);  }    // remember the previous value of the sensor  previous = stpPos;}`

Joes

#2
Sep 11, 2012, 10:01 pm
thanks for that but when button if high the stepper funs continuosly in the one direction

sdturner

#3
Sep 11, 2012, 10:50 pm
I'm not sure about that. I've not used the stepper library so there could be some subtle issue I don't know about.

Does it work if you put constants in place of the variables?

Code: [Select]
`if (digitalRead(1) == HIGH) {    stepper.step(-25);  }  else {    stepper.step(25);  }`

Joes

#4
Sep 11, 2012, 11:09 pm
no just runs one way and when button go's high then go's the other way
but thinking about it, the code is runing over and over so it will keep doing it?
so to shop this i have to do somthing like this?

Code: [Select]
`int ledPin =  13;    // LED connected to digital pin 13int button = 2;      // button pinint count = 0;       // Our blink counter// The setup() method runs once, when the sketch startsvoid setup()   {                  // initialize the digital pin as an output:  pinMode(ledPin, OUTPUT);   pinMode(button, INPUT);  }// the loop() method runs over and over again,// as long as the Arduino has powervoid loop()                    {  if (digitalRead(button)== HIGH)  // buttin when high  {  if (count < 1) {    digitalWrite(ledPin, HIGH);   // set the LED on    delay(1000);                  // wait for a second    digitalWrite(ledPin, LOW);    // set the LED off    delay(1000);  // wait for a second        count++;  // add one (1) to our count  }  }  else {    if(count == 1) count =0;       // resetting the count   } } `

sdturner

#5
Sep 12, 2012, 12:04 amLast Edit: Sep 12, 2012, 12:10 am by sdturner Reason: 1
Doh!

or ...

Code: [Select]
`if (digitalRead(1) == HIGH) {    stepper.step(-previous);    // reset the previous value to zero    previous = 0;  }  else {    stepper.step(stpPos - previous);    // remember the previous value of the sensor    previous = stpPos;  }`

Go Up