#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C addressAdafruit_MotorShield AFMS = Adafruit_MotorShield();// Or, create it with a different I2C address (say for stacking)// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1Adafruit_DCMotor *myMotor = AFMS.getMotor(1);Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);// You can also make another motor on port M2//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
void setup() { Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(7, INPUT_PULLUP); pinMode(8, INPUT_PULLUP); pinMode(A0, INPUT);
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
if (!AFMS.begin()) { // create with the default frequency 1.6KHz //if (!AFMS.begin(1000)) { // OR with a different frequency, say 1KHz Serial.println("Could not find Motor Shield. Check wiring."); while (1) ; } Serial.println("Motor Shield found.");
/**************************/ // Set the speed to start, from 0 (off) to 255 (max speed) //Can change the code to run the motor faster or slower myMotor->setSpeed(50); /*************************/ myMotor->run(FORWARD); // turn on motor myMotor->run(RELEASE);
myMotor2->setSpeed(50); /*************************/ myMotor2->run(FORWARD); // turn on motor myMotor2->run(RELEASE);
myMotor3->setSpeed(50); /*************************/ myMotor3->run(FORWARD); // turn on motor myMotor3->run(RELEASE);
myMotor4->setSpeed(50); /*************************/ myMotor4->run(FORWARD); // turn on motor myMotor4->run(RELEASE);}
void loop() { uint8_t i;
int forward = digitalRead(7); int backward = digitalRead(8);
/******************/
myMotor->setSpeed(50); myMotor2->setSpeed(50); myMotor3->setSpeed(50); myMotor4->setSpeed(50);
if (forward == LOW) { return Stage1(); delay(150000); return Stage2();
} else { myMotor->run(RELEASE); myMotor2->run(RELEASE); myMotor3->run(RELEASE); myMotor4->run(RELEASE); }}
void Stage1() { myMotor->run(RELEASE); myMotor->run(FORWARD); delay(1000); myMotor2->run(RELEASE); myMotor2->run(FORWARD); delay(1000); myMotor3->run(RELEASE); myMotor3->run(FORWARD); delay(1000); myMotor4->run(RELEASE); myMotor4->run(FORWARD);}
void Stage2() { myMotor->setSpeed(150); myMotor->run(RELEASE); myMotor->run(FORWARD); delay(100); myMotor2->setSpeed(40); myMotor2->run(RELEASE); myMotor2->run(BACKWARD); delay(10000); myMotor3->setSpeed(250); myMotor3->run(RELEASE); myMotor3->run(FORWARD); delay(1500); myMotor4->setSpeed(50); myMotor4->run(RELEASE); myMotor4->run(BACKWARD);}