kmkumar

Dagu Rover Code

Apr 8th, 2016
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Modified from the original adafruit source to suit the requirements - by Kumar – 9th Oct 2015 – Using Adafruit v2 Motorshield/Arduino UNO/Dagu Rover base
  2.  
  3. #include <Servo.h> // Enables the Servo library
  4. #include <Wire.h>
  5. #include <Adafruit_MotorShield.h>
  6. #include "utility/Adafruit_MS_PWMServoDriver.h"
  7.  
  8. //Using the same Library as **//http://www.instructables.com/id/Arduino-4wd-robot-with-ping-sensor-J-Bot/?ALLSTEPS**
  9. //Modified from the original adafruit source to suit the requirements - by Kumar – 9th Oct 2015 – Using Adafruit v2 Motorshield/Arduino UNO/Dagu Rover base
  10. //Conversion from 2WD to 4WD and 3 pin Ultrasonic sensor to 4 pin config
  11.  
  12. // Create the motor shield object with the default I2C address
  13. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  14. // Or, create it with a different I2C address (say for stacking)
  15. // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
  16.  
  17. // Select which 'port' M1, M2, M3 or M4. In this case, M1
  18. Adafruit_DCMotor *Frontleft = AFMS.getMotor(1);
  19. Adafruit_DCMotor *Frontright = AFMS.getMotor(2);
  20. Adafruit_DCMotor *Rearleft = AFMS.getMotor(3);
  21. Adafruit_DCMotor *Rearright = AFMS.getMotor(4);
  22.  
  23. Servo PingServo;
  24.  
  25. int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
  26.  
  27. int pingPin = A0; // Ping sensor is connected to port A0
  28.  
  29. int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
  30. long duration, inches, cm; // Define variables for Ping sensor
  31.  
  32.  
  33. void setup() {
  34. PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
  35. PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
  36.  
  37. AFMS.begin(); // create with the default frequency 1.6KHz
  38. //AFMS.begin(1000); // OR with a different frequency, say 1KHz
  39.  
  40. // Set the speed to start, from 0 (off) to 255 (max speed)
  41. Frontleft->setSpeed(150);
  42. Frontright->setSpeed(150);
  43. Rearleft->setSpeed(150);
  44. Rearright->setSpeed(150);
  45. Frontleft->run(FORWARD);
  46. Frontright->run(FORWARD);
  47. Rearleft->run(FORWARD);
  48. Rearright->run(FORWARD);
  49. // turn on motor
  50. Frontleft->run(RELEASE);
  51. Frontright->run(RELEASE);
  52. Rearleft->run(RELEASE);
  53. Rearright->run(RELEASE);
  54. }
  55.  
  56. void loop() {
  57. uint8_t i;
  58.  
  59. Serial.print("tick");
  60. Frontleft->run(FORWARD);
  61. Frontright->run(FORWARD);
  62. Rearleft->run(FORWARD);
  63. Rearright->run(FORWARD);
  64.  
  65. for (i=0; i<255; i++) {
  66. Frontleft->setSpeed(i);
  67. Frontright->setSpeed(i);
  68. Rearleft->setSpeed(i);
  69. Rearright->setSpeed(i);
  70. delay(10);
  71. }
  72. for (i=255; i!=0; i--) {
  73. Frontleft->setSpeed(i);
  74. Frontright->setSpeed(i);
  75. Rearleft->setSpeed(i);
  76. Rearright->setSpeed(i);
  77. delay(10);
  78. }
  79.  
  80. Serial.print("tock");
  81.  
  82. Frontleft->run(BACKWARD);
  83. Frontright->run(BACKWARD);
  84. Rearleft->run(BACKWARD);
  85. Rearright->run(BACKWARD);
  86.  
  87. for (i=0; i<255; i++) {
  88. Frontleft->setSpeed(i);
  89. Frontright->setSpeed(i);
  90. Rearleft->setSpeed(i);
  91. Rearright->setSpeed(i);
  92. delay(10);
  93. }
  94. for (i=255; i!=0; i--) {
  95. Frontleft->setSpeed(i);
  96. Frontright->setSpeed(i);
  97. Rearleft->setSpeed(i);
  98. Rearright->setSpeed(i);
  99. delay(10);
  100. }
  101.  
  102. Serial.print("tech");
  103. Frontleft->run(RELEASE);
  104. Frontright->run(RELEASE);
  105. Rearleft->run(RELEASE);
  106. Rearright->run(RELEASE);
  107. delay(1000);
  108. }
RAW Paste Data