Bahan-bahan
Arduino Uno R3 + USB Cable
Motor Control Shield
HC-SR04 dan Bracket
Motor DC + Gearbox & Roda
2x 14500 3.7V Li-Ion Battery dan 2xAA Battery Box
2WD Smart Car Chassis dan Spacer
Male to Male Jumper Cable
Male to Female Jumper Cable
Pin Out
ARDUINO
/ MOTOR CONTROL SHIELD
|
|
A4
|
ECHO HC-SR04
|
A5
|
TRIGGER HC-SR04
|
M3
|
MOTOR / RODA KIRI
|
M4
|
MOTOR / RODA KANAN
|
EXT_PWR
|
BATTERY (Jumper PWR_JMP)
|
#include <AFMotor.h> // import AdaFruit Motor Shield Library
#define trigPin A5 // define the pins of Ultrasonic HC-SR04 pins
#define echoPin A4
AF_DCMotor motor3(3, MOTOR34_64KHZ); // set up motors
AF_DCMotor motor4(4, MOTOR34_8KHZ);
void setup()
{
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT); // set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT); // set the echo pin to input (recieve sound waves)
motor3.setSpeed(200); //set the speed of the motors, between 0-255
motor4.setSpeed (200);
}
void loop()
{
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(30); // delays are required for a succesfull sensor operation
digitalWrite(trigPin, HIGH);
delayMicroseconds(38); // this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1; // convert the distance to centimeters
if (distance < 25) /*if there's an obstacle 25 centimers, ahead, do the following: */
{
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!"); // print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
motor3.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor4.run (BACKWARD);
}
else
{
Serial.println ("No obstacle detected, going forward");
delay (10);
motor3.run(FORWARD); //if there's no obstacle ahead, Go Forward!
motor4.run(FORWARD);
}
}
Galeri
Tidak ada komentar:
Posting Komentar