Adafruit-MortorShield_sample

Dependencies:   Adafruit-MotorShield Adafruit-PWM-Servo-Driver mbed

Fork of Adafruit-PWM-Servo-Driver_sample by Shundo Kishi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Adafruit_MotorShield.h"
00003 #include "Adafruit_PWMServoDriver.h"
00004 
00005 Serial pc(USBTX,USBRX);
00006 
00007 // Create the motor shield object with the default I2C address
00008 //Adafruit_MotorShield AFMS = Adafruit_MotorShield(p9, p10, 0x60 << 1); 
00009 Adafruit_MotorShield AFMS = Adafruit_MotorShield(D14, D15, 0x60 << 1); 
00010 // Or, create it with a different I2C address (say for stacking)
00011 // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 
00012 
00013 // Select which 'port' M1, M2, M3 or M4. In this case, M1
00014 Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
00015 // You can also make another motor on port M2
00016 //Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
00017 
00018 void setup() {
00019   pc.baud(9600);           // set up Serial library at 9600 bps
00020   pc.printf("Adafruit Motorshield v2 - DC Motor test!\n\r");
00021 
00022   AFMS.begin();  // create with the default frequency 1.6KHz
00023   //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
00024   
00025   // Set the speed to start, from 0 (off) to 255 (max speed)
00026   myMotor->setSpeed(150);
00027   myMotor->run(FORWARD);
00028   // turn on motor
00029   myMotor->run(RELEASE);
00030 }
00031 
00032 void loop() {
00033   uint8_t i;
00034   
00035   pc.printf("tick\n\r");
00036 
00037   myMotor->run(FORWARD);
00038   for (i=0; i<255; i++) {
00039     myMotor->setSpeed(i);  
00040     wait_ms(10);
00041   }
00042   for (i=255; i!=0; i--) {
00043     myMotor->setSpeed(i);  
00044     wait_ms(10);
00045   }
00046   
00047   pc.printf("tock\n\r");
00048 
00049   myMotor->run(BACKWARD);
00050   for (i=0; i<255; i++) {
00051     myMotor->setSpeed(i);  
00052     wait_ms(10);
00053   }
00054   for (i=255; i!=0; i--) {
00055     myMotor->setSpeed(i);  
00056     wait_ms(10);
00057   }
00058 
00059   pc.printf("tech\n\r");
00060   myMotor->run(RELEASE);
00061   wait(1.0);
00062 }
00063 
00064 int main() {
00065     setup();
00066     while(1) {
00067         loop();
00068     }
00069 }