dedi74132
Published © GPL3+

Line Follower Using Servo, RFID and Ultrasonic Sensor

Build a line follower robot using Arduino Uno with a continuous servo as a motor, and RFID to tag point and stop LF and ping sensor.

IntermediateShowcase (no instructions)7,342
Line Follower Using Servo, RFID and Ultrasonic Sensor

Things used in this project

Story

Read more

Code

ROBOT_LF

Arduino
main program of LF
// LCD
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

int pin1 = 3;
int pin2 = 5;
const int stop_position = 100; 
const int velocity_high = 85;
const int velocity_med = 55;
const int velocity_slow = 25;
const int buzzerPin = 8;

Servo servo_kiri;
Servo servo_kanan;

// FUNCTION
#define MAX 5
int top = -1;
int status;
int a = 10;
bool full = false;

// SENSOR
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int sensorPin3 = A2;
const int sensorPin4 = A3;
int interupt_status = 0;
int is_home = 0;
unsigned char data1,data2,data3,data4;
unsigned char sensor,batas,xsensor,xstatus;
unsigned char trackWarnaPutih;
unsigned char  trackMode = 0, batasEE = 100;
#define   lurus         0
#define   tikunganKiri  1
#define   tikunganKanan 2

void setup() {
  Serial.begin(9600);
  lcd_setup();
  ping_setup();  
  rfid_setup();
  motor_setup();
}

void loop() {
 // motor_maju(2);
  drawLCD();  
  if(is_home==0){
    ping_loop();
    if(interupt_status == 0){
        readSensor();
        rfid_scan();
    }else if(interupt_status == 1){
      motor_stop();
      buzzer();
      interupt_status = 0;
    }else{
      motor_stop();
      buzzer();
    }
  }else{
    motor_stop2;
  }    
}

LCD

Arduino
LCD CODE
String menuItems[] = {" Start", " (+)Jurusan", " (-)Jurusan"};
String menuJurusan[] = {"MESIN", "ELEKTRO", "INFORMATIKA", "INDUSTRI", "SIPIL", "ARSITEK", "BATAL"};
int ledPin = 13; // choose the pin for the LED
int inPin1 = 7;   // choose the input pin (for a pushbutton)
int inPin2 = 6;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void lcd_setup() {
  lcd.begin(16,2);
  lcd.clear();
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
}

void drawLCD() {
  if(digitalRead(inPin1)==HIGH) {
    digitalWrite(inPin1, LOW);
    lcd.clear();
    Serial.println("HOME");
    lcd.print("HOME");
    motor_stop();
    is_home = 1;
    motor_stop();
  }else if(digitalRead(inPin2)==HIGH) {
    digitalWrite(inPin1, LOW);
    lcd.clear();
    Serial.println("INTERUPT TO START");
    lcd.print("INTERUPT TO START");
    interupt_status == 1;
    is_home = 0;
    motor_stop();
  }
}

RFID

Arduino
RFID CODE, use MFRC522 LIBRARY
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String rfid_card[] = {"fa8d56f9", "9a775af9", "ca8a55f9", "424a7cd2", "ea748af9", "4a8229f9"};
String sekjur[] = {"MESIN", "ELEKTRO", "INFORMATIKA", "INDUSTRI", "SIPIL", "ARSITEK"};

/*
   Initialize.
*/
void rfid_setup() {
  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card
}

/*
   Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

void rfid_scan() {

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()){
    readSensor();
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()){
    readSensor();
  }
  
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);
  for (int x = 0; x < 6; x++) {
    if(read_rfid == "8a4098f9"){
        lcd.clear();
        lcd.print("HOME");    
        is_home = 1;
        motor_stop2();
        buzzer();
    }else{      
      if (read_rfid == rfid_card[x]) {
        buzzer();
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Surat Terkirim");
        lcd.setCursor(0, 1);
        lcd.print("SEKJUR ");
        lcd.print(sekjur[x]);
        interupt_status = 1;
      } 
    }

  }
}

SENSOR

Arduino
IR Sensor i use modeul 4 sensor IR
void motor_setup() {
  servo_kiri.attach(pin1);
  servo_kanan.attach(pin2);
}

void buzzer(){
  tone(buzzerPin, 3000);
  delay(500);  
  noTone(buzzerPin);
}

void motor_maju(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  
  servo_kiri.write(stop_position - speed -5);
  servo_kanan.write(stop_position + speed -10);
}

void motor_mundur(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  
  servo_kiri.write(stop_position + speed -5);
  servo_kanan.write(stop_position - speed -10);  
}

void belok_kanan(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  
  servo_kiri.write(stop_position - speed -5);
  servo_kanan.write(stop_position - speed -10);
}

void belok_kanan_patah(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  servo_kiri.write(stop_position - speed -5);
  servo_kanan.write(stop_position -10);
}

void belok_kiri(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  
  servo_kiri.write(stop_position + speed -5);
  servo_kanan.write(stop_position + speed -10);  
}

void belok_kiri_patah(int speed) {
  if(speed==1) {
     speed = velocity_high;
  } else if(speed==2) {
     speed = velocity_med;
  } else {
     speed = velocity_slow;
  }
  
  servo_kiri.write(stop_position -5);
  servo_kanan.write(stop_position + speed -10);  
}

void motor_stop() {
  servo_kiri.write(stop_position - 5);
  servo_kanan.write(stop_position - 10);
}

void motor_stop2() {
  servo_kiri.write(stop_position - 5);
  servo_kanan.write(stop_position - 5);
}

void readSensor() {
  xsensor = sensor;
  sensor = 0;
  trackWarnaPutih = trackMode;
  batas = batasEE;

  data1 = analogRead(sensorPin1);
  data2 = analogRead(sensorPin2);
  data3 = analogRead(sensorPin3);
  data4 = analogRead(sensorPin4); 
  
  if (data1>batas) sensor |= 0x01;
  if (data2>batas) sensor |= 0x02;
  if (data3>batas) sensor |= 0x04;
  if (data4>batas) sensor |= 0x08;

  lcd.setCursor(0,1);
  
   if (trackWarnaPutih) {
     sensor = sensor ^ 0x0F;      
   } 
   if(sensor == 0x01) {    
    // hasil pembacaan sensor "0001" -> track berada di ujung kanan sensor      
      belok_kanan(1);
      Serial.print("0001");
      lcd.print("Pengiriman Surat");
   }
   else if (sensor == 0x03) {    
    // hasil pembacaan sensor "0011" -> track berada di ujung kanan sensor      
      belok_kanan(1);
      Serial.print("0011");
      lcd.print("Pengiriman Surat");
   }
   else if (sensor == 0x06) {
      // hasil pembacaan sensor "0110" -> track tepat berada di tengah   
      motor_maju(3);
      Serial.print("0110");
      lcd.print("Pengiriman Surat");
   }
   else if (sensor == 0x07) {
      // hasil pembacaan sensor "0111" -> track tepat berada di tengah   
      belok_kanan(1);
      Serial.print("0111");
      lcd.print("Pembacaan Sensor");
   }
   else if (sensor == 0x08) {
      // hasil pembacaan sensor "1000" -> track berada di ujung kiri sensor
      belok_kiri(1);
      Serial.print("1000");
      lcd.print("Pengiriman Surat");
   }
   else if (sensor == 0x0C) {
      // hasil pembacaan sensor "1100" -> track berada di ujung kiri sensor
      belok_kiri(1);
      Serial.print("1100");
      lcd.print("Pengiriman Surat");
   }
   else if (sensor == 0x0E) {
      // hasil pembacaan sensor "1110" -> track berada di ujung kiri sensor
      belok_kiri(1);
      Serial.print("1110");
      lcd.print("Pengiriman Surat");
   }
   else if ((sensor == 4001) || (sensor == 2001) || (sensor == 2100)|| (sensor == 5110)) {
      belok_kanan(1);
   }else{
      motor_maju(3);
   }
}

ULTRASONIC

Arduino
Ultrasonic / Ping Sensor
#define trigPin 4
#define echoPin 2

void ping_setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void ping_loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance > 5 || distance <= 0){
    noTone(buzzerPin);
  }
  else {
    Serial.println("object detected");
    motor_stop();
    tone(buzzerPin, 5000); // play 400 Hz tone for 500 ms
    delay(10);
  }
}

Credits

dedi74132
1 project • 5 followers
Contact

Comments

Please log in or sign up to comment.