Hello,
I am new to Arduino and we have a Halloween Project where my Metro with the Motor Shield is going to be driving three separate motors to turn some skeleton heads back and forth so it looks like they are looking around. My goal was to make a 'smart' program where I can just use an array to tell the motor when to start, stop and what direction to move. The code below works when I have only one motor coded, but as soon as I add a second, the first motor sometimes won't spin more than a couple of seconds and after I add the third, it really starts screwing up and the motors seem to have a mind of their own. I have a one second delay at the end of the loop so that the head movement can be counted in seconds. Does anyone know if there is a better way to program this? Here is what the end result needs to be:
Configure Motor 1 to run forward on second 2, stop on second 7, move backwards on second 10, stop on second 15, move forwards on second 16 and stop on second 19.
Something similar for the other motors.
Any help you can provide will be greatly appreciated.
Here is my code below:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *mtrHead1 = AFMS.getMotor(1);
Adafruit_DCMotor *mtrHead2 = AFMS.getMotor(2);
Adafruit_DCMotor *mtrHead3 = AFMS.getMotor(3);
int timer = 1000; // Set to 1 second
int counter = 0; // Initialize counter
int cycles = 20; // Number of cycles (seconds) before starting over
int head1act[] = {2, 7, 10, 15, 16, 19}; // The second that an action will activate on Motor 1
int head1mv[] = {FORWARD, RELEASE, BACKWARD, RELEASE, FORWARD, RELEASE}; // The action the motor will take at said second
int head1spd[] = {255, 0, 255, 0, 255, 0}; // The speed the motor will do the action
int head2act[] = {3, 5, 8, 13, 14, 17}; // The second that an action will activate on Motor 2
int head2mv[] = {FORWARD, RELEASE, BACKWARD, RELEASE, FORWARD, RELEASE}; // The action the motor will take at said second
int head2spd[] = {255, 0, 255, 0, 255, 0}; // The speed the motor will do the action
int head3act[] = {1, 10, 12, 14, 16, 18}; // The second that an action will activate on Motor 3
int head3mv[] = {BACKWARD, RELEASE, FORWARD, RELEASE, FORWARD, RELEASE}; // The action the motor will take at said second
int head3spd[] = {255, 0, 255, 0, 255, 0}; // The speed the motor will do the action
void setup() {
AFMS.begin();
}
void loop() {
// loop from the lowest pin to the highest:
for (int counter = 0; counter < cycles; counter++){
for (int tmp = 0; tmp < sizeof(head1act); tmp++){ //check to see if the counter matches a number in motor 1 array
if (head1act[tmp] == counter) { //If the number matches, do the action below for motor 1
mtrHead1->setSpeed(head1spd[tmp]); //Set motor Speed
mtrHead1->run(head1mv[tmp]); //Run motor action
x = sizeof(head1act); //stop going through the array
}
}
for (int tmp = 0; tmp < sizeof(head2act); tmp++) {//check to see if the counter matches a number in motor 2 array
if (head2act[tmp] == counter) { If the number matches, do the action below for motor 2
mtrHead2->setSpeed(head2spd[tmp]); //Set motor Speed
mtrHead2->run(head2mv[tmp]); //Run motor action
x = sizeof(head2act); //stop going through the array
}
}
for (int tmp = 0; tmp < sizeof(head3act); tmp++){ //check to see if the counter matches a number in motor 3 array
if (head3act[tmp] == counter) { If the number matches, do the action below for motor 3
mtrHead3->setSpeed(head3spd[tmp]); //Set motor Speed
mtrHead3->run(head3mv[tmp]); //Run motor action
x = sizeof(head3act); //stop going through the array
}
}
delay(timer);
}
}