I'm driving a stepper motor using an adafruit motor shield. I have it set up so that it rotates 6 rev per minute. I'd like to be able to add that every 60 s the revolution randomly change from 5 to 7 revolution per minute. I can figure out what the delay will be by trail and error, delay now is 1084.
int stepCount;
unsigned long previousMillis;
#include <Wire.h>
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(2052, 1);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(1000); // 10 rpm
}
void loop() {
unsigned long currentMillis = millis();
//Serial.println("Single coil steps");
myMotor->onestep(FORWARD, SINGLE);
delayMicroseconds(1084);
stepCount++;
if(stepCount == 2052)
{
unsigned long lapTime = currentMillis - previousMillis;
Serial.println(lapTime);
previousMillis = currentMillis;
stepCount = 0;
}
}