Go Down

Topic: Random stepper motor movement using randomSeed() or random() help appreciated (Read 212 times) previous topic - next topic

timlong

Hello

How can I apply a random rotation to and fro for a stepper motor please? I have adapted the sketch below, and could add a series of turns up and down but would prefer it to be random. I am planning to get the motor to play a guitar chaotically...

Any advice welcome, thanks

I am using this motor, PF35T - 49L4:
http://www.adafruit.com/blog/2009/06/03/small-stepper-motors-and-micro-servos-new-in-the-store/

http://forum.arduino.cc/index.php?topic=181956.0

Quote
// Include the Stepper Library
#include <Stepper.h>

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 48;

// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);

void setup() {
  // Set the RPM of the motor
  myStepper.setSpeed(30);

  // Turn on pulse width modulation
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);

  // Log some shit
  Serial.begin(9600);
}

void loop() {
  // Move the motor X amount of steps
  myStepper.step(STEPS);
  Serial.println(STEPS);
  // Pause
  delay(2000);

  // Move the motor X amount of steps the other way
  myStepper.step(-STEPS);
  Serial.println(-STEPS);
  // Pause
  delay(2000);
}

MarkT

You probably need to compute a random absolute position within a fixed range, then
move to that - otherwise you have no bounds on the movement (which is currently a
random walk)

[ BTW the AccelStepper library has an absolute moveTo() method which might
be more suitable ]
[ I won't respond to messages, use the forum please ]

Go Up