I am trying to run two stepper motors at the same time. I need the two motors to turn back and forth at different random intervals at the same time. The code as it is now the motor in port 1 goes back and forth first then stops then the port motor runs.
Something like -
motor 1 and motor2 two move forward at random interval 1 and random interval 2 respectively
motor 1 and motor2 two move backward at random interval 1 and random interval 2 respectively
Is there a way to write the code so that each motor is controlled independently? This would be ideal.
Thanks!
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor1->setSpeed(20); // rpm
myMotor2->setSpeed(20); // rpm
}
void loop() {
myMotor1->step(random(30,50), FORWARD, DOUBLE);
myMotor1->step(random(30,50), BACKWARD, DOUBLE);
myMotor2->step(random(30,50), FORWARD, DOUBLE);
myMotor2->step(random(30,50), BACKWARD, DOUBLE);
}