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


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:

// 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

  // 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

void loop() {
  // Move the motor X amount of steps
  // Pause

  // Move the motor X amount of steps the other way
  // Pause

Offline Offline
Shannon Member
Karma: 215
Posts: 12525
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: