Pages: [1]   Go Down
Author Topic: Random stepper motor movement using randomSeed() or random() help appreciated  (Read 144 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ]
« Last Edit: June 14, 2014, 06:14:39 am by MarkT » Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: