Jumat, 01 Juli 2016

How To Build An Obstacle Avoiding Robot

How To Build An Obstacle Avoiding Robot

Alat dan Bahan yang dibutuhkan :

1x Arduino Uno

1x Ultra Sonic Sensor

1x Motor Driver L298N

4x Motor DC (Sebagai Roda)

1x Battery +9V for Motor L298N

1x Battery +9V for Arduino UNO

Kabel jumper secukupnya

Langkah-langkahnya :

1.      Susun rangkaian seperti gambar berikut :


Keterangan Konfigurasi Kabel :

·         Hubungkan Pin Vcc Ultrasonic Sensor ke Pin +5V Arduino

·         Hubungkan Pin Echo Ultrasonic Sensor ke Pin 3 Arduino

·         Hubungkan Pin Trigger Ultrasonic Sensor ke Pin 2 Arduino

·         Hubungkan Pin Ground Ultrasonic Sensor ke Pin Ground Arduino

·         Hubungkan Pin IN1 L298N ke Pin 7 Arduino

·         Hubungkan Pin IN2 L298N ke Pin 6 Arduino

·         Hubungkan Pin IN3 L298N ke Pin 5 Arduino

·         Hubungkan Pin IN4 L298N ke Pin 4 Arduino

·         Solder Pin Motor DC (Contoh lihat pada Gambar diatas)

·         Hubungkan +12V Motor Driver L298N ke +9V Battery

·         Hubungkan Ground Motor Driver L298N ke Battery dan Ke Pin Ground Arduino

1.      Jalankan program Arduino-nya.

2.      Klik menu "Tools -> Board -> Arduino Uno"

3.      Klik menu "Tools -> Port -> ( Pilih Port arduino yang terdeteksi di komputer anda )

4.      Lalu masukan Sketch dibawah ini.

#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
}*/
/////////////////////////////////////////////////////////////////////////

Tidak ada komentar:

Posting Komentar

PERANCANGAN ALAT PENDETEKSI ASAP ROKOK BERBASIS ARDUINO UNO DENGAN OUTPUT SUARA DAN MATI LAMPU SECARA OTOMATIS

BAB I PENDAHULUAN 1.1 Latar Belakang          Dalam perkembangan dunia teknologi semakin maju maka diperlukan kesadaran kita untuk...