Skip to content

Instantly share code, notes, and snippets.

@mdobson
Created April 21, 2014 20:28
Show Gist options
  • Save mdobson/11155497 to your computer and use it in GitHub Desktop.
Save mdobson/11155497 to your computer and use it in GitHub Desktop.
How many motors could I possibly control?
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.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);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *firstMotor = AFMS.getMotor(1);
Adafruit_DCMotor *secondMotor = AFMS.getMotor(2);
Adafruit_DCMotor *thirdMotor = AFMS.getMotor(3);
Adafruit_DCMotor *fourthMotor = 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
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
firstMotor->setSpeed(150);
firstMotor->run(FORWARD);
// turn on motor
firstMotor->run(RELEASE);
secondMotor->setSpeed(150);
secondMotor->run(FORWARD);
// turn on motor
secondMotor->run(RELEASE);
thirdMotor->setSpeed(150);
thirdMotor->run(FORWARD);
// on motor
thirdMotor->run(RELEASE);
fourthMotor->setSpeed(150);
fourthMotor->run(FORWARD);
// turn on motor
fourthMotor->run(RELEASE);
}
void loop() {
uint8_t i;
Serial.print("tick");
firstMotor->run(FORWARD);
for (i=0; i<255; i++) {
firstMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
firstMotor->setSpeed(i);
delay(10);
}
Serial.print("tock");
firstMotor->run(BACKWARD);
for (i=0; i<255; i++) {
firstMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
firstMotor->setSpeed(i);
delay(10);
}
Serial.print("tech");
firstMotor->run(RELEASE);
secondMotor->run(FORWARD);
for (i=0; i<255; i++) {
secondMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
secondMotor->setSpeed(i);
delay(10);
}
Serial.print("tock");
secondMotor->run(BACKWARD);
for (i=0; i<255; i++) {
secondMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
secondMotor->setSpeed(i);
delay(10);
}
Serial.print("tech");
secondMotor->run(RELEASE);
thirdMotor->run(FORWARD);
for (i=0; i<255; i++) {
thirdMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
thirdMotor->setSpeed(i);
delay(10);
}
Serial.print("tock");
thirdMotor->run(BACKWARD);
for (i=0; i<255; i++) {
thirdMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
thirdMotor->setSpeed(i);
delay(10);
}
Serial.print("tech");
thirdMotor->run(RELEASE);
fourthMotor->run(FORWARD);
for (i=0; i<255; i++) {
fourthMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
fourthMotor->setSpeed(i);
delay(10);
}
Serial.print("tock");
fourthMotor->run(BACKWARD);
for (i=0; i<255; i++) {
fourthMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
fourthMotor->setSpeed(i);
delay(10);
}
Serial.print("tech");
fourthMotor->run(RELEASE);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment