Advertisement
Hadlock

adafruit motor controller sketch

Sep 12th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. /*
  2. This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
  3. It won't work with v1.x motor shields! Only for the v2's with built in PWM
  4. control
  5.  
  6. For use with the Adafruit Motor Shield v2
  7. ---->   http://www.adafruit.com/products/1438
  8. */
  9.  
  10. #include <Wire.h>
  11. #include <Adafruit_MotorShield.h>
  12. #include "utility/Adafruit_PWMServoDriver.h"
  13.  
  14. // Create the motor shield object with the default I2C address
  15. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  16. // Or, create it with a different I2C address (say for stacking)
  17. // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
  18.  
  19. // Select which 'port' M1, M2, M3 or M4. In this case, M1
  20. Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
  21. // You can also make another motor on port M2
  22. //Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
  23.  
  24. void setup() {
  25.   Serial.begin(9600);           // set up Serial library at 9600 bps
  26.   Serial.println("Adafruit Motorshield v2 - DC Motor test!");
  27.  
  28.   AFMS.begin();  // create with the default frequency 1.6KHz
  29.   //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  30.  
  31.   // Set the speed to start, from 0 (off) to 255 (max speed)
  32.   myMotor->setSpeed(150);
  33.   myMotor->run(FORWARD);
  34.   // turn on motor
  35.   myMotor->run(RELEASE);
  36. }
  37.  
  38. void loop() {
  39.   uint8_t i;
  40.  
  41.   Serial.print("tick");
  42.  
  43.   myMotor->run(FORWARD);
  44.   for (i=0; i<255; i++) {
  45.     myMotor->setSpeed(i);  
  46.     delay(10);
  47.   }
  48.   for (i=255; i!=0; i--) {
  49.     myMotor->setSpeed(i);  
  50.     delay(10);
  51.   }
  52.  
  53.   Serial.print("tock");
  54.  
  55.   myMotor->run(BACKWARD);
  56.   for (i=0; i<255; i++) {
  57.     myMotor->setSpeed(i);  
  58.     delay(10);
  59.   }
  60.   for (i=255; i!=0; i--) {
  61.     myMotor->setSpeed(i);  
  62.     delay(10);
  63.   }
  64.  
  65.   Serial.print("tech");
  66.   myMotor->run(RELEASE);
  67.   delay(1000);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement