Tutorial robot AVOIDER, cara membuat robot avoider
Disini saya akan membagi ilmu dengan tutorial robot AVOIDER dan semoga bermanfaat bagi pembaca.
Alat dan Bahan yang di butuhkan:
1.1x Arduino Ini
2.1x Motor Driver L298N
4.4x Motor DC (untuk roda)
5.1x Sensor Ultrasonik
6.1x Bat +9v untuk Motor Driver
7.1x Bat +9v untuk Arduino
8.Kabel jumper secukupnya.
Rangkain:
Keterangan Konfigurasi Kabel:
1.Hubungkan pin Vcc Ultrasonic ke pin +5v Arduino.
2.Hubungkan PIN Echo Ultrasonic ke PIN 3 Arduino.
3.Hubungkan PIN Trigger Ultrasonic ke PIN 2 Arduino.
4.Hubungkan PIN ground Ultrasonic ke PIN ground Arduino.
5.Hubungkan PIN IN1 L298N ke PIN 7 Arduino.
6.Hubungkan PIN IN2 L298N ke PIN 6 Arduino.
7.Hubungkan PIN IN3 L298N ke PIN 5 Arduino.
8.Hubungkan PIN IN4 L298N ke PIN 4 Arduino.
9.Hubungkan +12v motor driver L298N ke PIN +9 BAT.
10.Hubungkan ground motor driver L298N ke BAT dan ke PIN ground Arduino.
Source code:
#include <NewPing.h>
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on ping censor(CENTER censor)
#define ECHO_PIN 3 // Arduino pin tied to echo pin on ping censor(CENTER censor)
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
unsigned int pingSpeed = 50;
unsigned long pingTimer; // Holds the next ping time.
int distance;
int distanceL;
int distanceR;
//MotorSetting; Using DFRobot L298 2A Motor Controller
int M1 = 4;
int E1 = 5;
int M2 = 7;
int E2 = 6;
int pwm_speed;
int speedVal = 255;
//char command = '\0';
void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(10, OUTPUT);
delay(250);
}
void loop() {
distance = sonar.ping_cm();
Serial.println(distance);
if (distance > 25){
Drive("Forward");
Serial.println("maju");
}
else if (distance < 25 && distance > 10)
{
Drive("TurnRight");
delay(250);
Drive("Stop");
distanceR = sonar.ping_cm();
Drive("TurnLeft");
delay (250);
Drive ("Stop");
distanceL = sonar.ping_cm();
if (distanceR > distanceL) {
Drive ("TurnRight");
Serial.println("kanan");
delay (250);
Drive ("Forward");
}
else {
Drive ("TurnLeft");
Serial.println("kiri");
delay (250);
Drive ("Forward");
}
}
else {
Drive ("Backward");
Serial.println("mundur");
delay (100);
Drive ("Stop");
}
}
//Mechatronic and Robotic//
//Electrical Engineering//
//Malikussaleh of University//
/////////////////Directions Setup/////////////////////////
void Drive(String Direction) {
if (Direction == "Backward") {
// Serial.println("Backward");
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
}
if (Direction == "Forward") {
// Serial.print("Forward");
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
}
if (Direction == "Stop") {
//Serial.print("Stop");
analogWrite(E1, 0);
analogWrite(E2, 0);
}
if (Direction == "TurnRight") {
//Serial.print("Turning Right");
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(M1, LOW);
digitalWrite(M2, HIGH);
}
if (Direction == "TurnLeft") {
// Serial.print("Turning Left");
analogWrite(E1, 255);
analogWrite(E2, 255);
digitalWrite(M1, HIGH);
digitalWrite(M2, LOW);
}
}
void test_speed() {
// constrain speed value to between 0-255
if (speedVal > 250) {
speedVal = 255;
Serial.println(" MAX ");
}
if (speedVal < 0) {
speedVal = 0;
Serial.println(" MIN ");
}
}
//--------------------------------------
/*
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
//return microseconds / 29 / 2;
return microseconds / 10; //basically 58
}*/
/////////////////////////////////////////////////////////////////////////
Terimakasih kepada pembaca setia dan semoga ilmu yang saya shere bermanfaat kepada pembaca. Terimakasih.