I am in need of urgent help. i have bought an adafruit motor shield and with corresponding adafruit stepper motor. i have had this working really nicely but have just found out after installing it in my art exhibit it doesn't have enough torque to turn the rod of a venetian blind round. So my thinking is to purchase another motor so i have one at either end and that should hopefully have enough torque to turn the rod in the blind.
With the motor on order i am currently trying to create the code in order to run 2 x stepper motors, from one adafruit shield and i will continue to use a single 12 v power supply to power them both ( i believe that i can do this, is this correct??). Both stepper motors are to work at the same time with one going forward and the other going backward, which when connected to either end of the rod due to orientation they will both be going the same direction and in turn make the rod turn.
My big issue is that my coding experience is very limited. i have read as much info as i can find and realise i have to set the motors up in one step mode in order for them to run at the same time. then using the adafruit multi stepper example it says to wrap them in a accelstepper wrapper but when it comes to the actual code that controls everything i am unsure on what code to write. please see what i have done already below. i want both motors to come on, do the same number of steps, one motor forward, the other backward, then wait ( i have put in a randomiser delay pattern for the purposes of the exhibit) and then the motors to turn back the opposite way they just turned, then another randomised delay.
Any info would be great, i am feel i am close but cannot complete the code.
Please help, many thank yous in advance.
Kind regards
tom
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <AccelStepper.h>
#include <AFMotor.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);
unsigned long randomTimeOn; // here's a variable to store our blinds open time value
unsigned long randomTimeOff; // here's a variable to store our blinds closed time value
void forwardstep1() {
motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
motor1.onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {
motor2.onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
motor2.onestep(BACKWARD, DOUBLE);
}
// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1forward(forwardstep1);
AccelStepper stepper1backward(backwardstep1);
AccelStepper stepper2forward(forwardstep2);
AcellStepper stepper2backward(backwardstep2);
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
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
randomSeed(analogRead(0)); // start the random call in a unspecified point in the random sequence
stepper1forward.setMaxSpeed(50);
stepper1forward.setAcceleration(100.0);
stepper1forward.moveTo(155);
stepper1backward.setMaxSpeed(50);
stepper1backward.setAcceleration(100.0);
stepper1backward.moveTo(155);
stepper2forward.setMaxSpeed(50);
stepper2forward.setAcceleration(100.0);
stepper2forward.setmoveTo(155);
stepper2backward.setMaxSpeed(50);
stepper2backward.setAcceleration(100.0);
stepper2backward.setmoveTo(155);
myMotor->setSpeed(50); // 50 rpm
myMotor->release(); // stop rotation and turn off holding torque.
}
void loop() {
randomTimeOn = random(0, 4000); // this then randomly picks a number in the range specified
Serial.println(randomTimeOn); // what number 'randomTime' is holding
Serial.println("Double coil steps");
stepper1forward.run();
stepper2backward.run();
myMotor->release();
delay(randomTimeOn);
randomTimeOff = random(0, 4000); // chose a differnet number within the range specified
stepper1backward.run();
stepper2forward.run();
myMotor->release();
delay(randomTimeOff);
}