Go Down

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



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


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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131