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://forum.arduino.cc/index.php?topic=181956.0
// 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);
}