Go Down

Topic: stepper motor control (Read 711 times) previous topic - next topic

Joes

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 100

Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int 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

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 100

Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int 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

thanks for that but when button if high the stepper funs continuosly in the one direction

sdturner

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

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 13
int button = 2;      // button pin

int count = 0;       // Our blink counter

// The setup() method runs once, when the sketch starts

void 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 power

void 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 am Last Edit: Sep 12, 2012, 12:10 am by sdturner Reason: 1
Doh!  :smiley-sad-blue:

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