Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Modified from the original adafruit source to suit the requirements - by Kumar – 9th Oct 2015 – Using Adafruit v2 Motorshield/Arduino UNO/Dagu Rover base
- #include <Servo.h> // Enables the Servo library
- #include <Wire.h>
- #include <Adafruit_MotorShield.h>
- #include "utility/Adafruit_MS_PWMServoDriver.h"
- //Using the same Library as **//http://www.instructables.com/id/Arduino-4wd-robot-with-ping-sensor-J-Bot/?ALLSTEPS**
- //Modified from the original adafruit source to suit the requirements - by Kumar – 9th Oct 2015 – Using Adafruit v2 Motorshield/Arduino UNO/Dagu Rover base
- //Conversion from 2WD to 4WD and 3 pin Ultrasonic sensor to 4 pin config
- // 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 *Frontleft = AFMS.getMotor(1);
- Adafruit_DCMotor *Frontright = AFMS.getMotor(2);
- Adafruit_DCMotor *Rearleft = AFMS.getMotor(3);
- Adafruit_DCMotor *Rearright = AFMS.getMotor(4);
- Servo PingServo;
- int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
- int pingPin = A0; // Ping sensor is connected to port A0
- int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
- long duration, inches, cm; // Define variables for Ping sensor
- void setup() {
- PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
- PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
- 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)
- Frontleft->setSpeed(150);
- Frontright->setSpeed(150);
- Rearleft->setSpeed(150);
- Rearright->setSpeed(150);
- Frontleft->run(FORWARD);
- Frontright->run(FORWARD);
- Rearleft->run(FORWARD);
- Rearright->run(FORWARD);
- // turn on motor
- Frontleft->run(RELEASE);
- Frontright->run(RELEASE);
- Rearleft->run(RELEASE);
- Rearright->run(RELEASE);
- }
- void loop() {
- uint8_t i;
- Serial.print("tick");
- Frontleft->run(FORWARD);
- Frontright->run(FORWARD);
- Rearleft->run(FORWARD);
- Rearright->run(FORWARD);
- for (i=0; i<255; i++) {
- Frontleft->setSpeed(i);
- Frontright->setSpeed(i);
- Rearleft->setSpeed(i);
- Rearright->setSpeed(i);
- delay(10);
- }
- for (i=255; i!=0; i--) {
- Frontleft->setSpeed(i);
- Frontright->setSpeed(i);
- Rearleft->setSpeed(i);
- Rearright->setSpeed(i);
- delay(10);
- }
- Serial.print("tock");
- Frontleft->run(BACKWARD);
- Frontright->run(BACKWARD);
- Rearleft->run(BACKWARD);
- Rearright->run(BACKWARD);
- for (i=0; i<255; i++) {
- Frontleft->setSpeed(i);
- Frontright->setSpeed(i);
- Rearleft->setSpeed(i);
- Rearright->setSpeed(i);
- delay(10);
- }
- for (i=255; i!=0; i--) {
- Frontleft->setSpeed(i);
- Frontright->setSpeed(i);
- Rearleft->setSpeed(i);
- Rearright->setSpeed(i);
- delay(10);
- }
- Serial.print("tech");
- Frontleft->run(RELEASE);
- Frontright->run(RELEASE);
- Rearleft->run(RELEASE);
- Rearright->run(RELEASE);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement