Example Code for Arduino-Autonomous Obstacle Avoidance

This article presents a detailed guide for implementing autonomous obstacle avoidance using Arduino and Infrared sensors, offering step-by-step instructions, example code, and essential setup tips.

Hardware Preparation

Software Preparation

Other Preparation Work

  1. Unscrew the screw and open the USB programming port.
  2. Turn on Vortex power switch, and plug in the Micro usb cable, it will install the driver automatically if you have installed Arduino IDE. If not, you can find it in the driver file in Arduino IDE folder-->Drivers folder.
  3. There is a switch close to the USB port, make sure the trigger is close to the USB side. This is MP3 switch, we'll teach you how to add new song in the following chapter.
  4. Open your Arduino IDE, select "Arduino UNO" and right "COM port" in Arduino IDE, now you can enjoy coding.

Sample Code

/***************************************************
 Vortex V1.0 (Small robots like bread)
 <https://www.dfrobot.com.cn/goods-1199.html>
 ***************************************************
 This example show how to use Infrared sensor to avoid obstacles.
 Created 2016-2-3
 By Andy zhou <[email protected]>
 version:V1.0
 ****************************************************/
/***********Notice and Trouble shooting***************
 1.Connection and Diagram can be found here
 <http://wiki.dfrobot.com.cn/index.php?title=(SKU:ROB0116)_Vortex%E5%8F%AF%E7%BC%96%E7%A8%8B%E6%9C%BA%E5%99%A8%E4%BA%BA#.E6.A0.B7.E4.BE.8B.E4.BB.A3.E7.A0.81>
 2.This code is tested on vortex V1.0.
 ****************************************************/
#define IR_IN  7//IR receiver pin
#define L_IR 8  //left ir transmitter pin
#define R_IR 12 //right ir transmitter pin
int count;
void leftSend38KHZ(void){//left ir transmitter sends 38kHZ pulse
  int i;
  for(i=0;i<24;i++){
    digitalWrite(L_IR,LOW);
    delayMicroseconds(8);
    digitalWrite(L_IR,HIGH);
    delayMicroseconds(8);
  }
}
void rightSend38KHZ(void){//right ir transmitter sends 38kHZ pulse
  int i;
  for(i=0;i<24;i++){
    digitalWrite(R_IR,LOW);
    delayMicroseconds(8);
    digitalWrite(R_IR,HIGH);
    delayMicroseconds(8);
  }
}
void pcint0Init(void){//init the interrupt
    PCICR |= 1 << PCIE2;
    PCMSK2 |= 1 << PCINT23;
}
ISR(PCINT2_vect){//motor encoder interrupt
  count++;
}
void obstacleAvoidance(void){
  char i;
  count=0;
  for(i=0;i<20;i++){  //left transmitter sends 20 pulses
    leftSend38KHZ();
    delayMicroseconds(600);
  }
  if(count>20){//if recieved a lot pulse , it means there's a obstacle
    Serial.println("Left");
    delay(100);
  }
  count=0;
  for(i=0;i<20;i++){//right transmitter sends 20 pulses
    rightSend38KHZ();
    delayMicroseconds(600);
  }
  if(count>20){//if recieved a lot pulse , it means there's a obstacle
    Serial.println("Right");
    delay(100);
  }
}
void setup(void){
  pinMode(L_IR,OUTPUT);//init the left transmitter pin
  pinMode(R_IR,OUTPUT);//init the right transmitter pin
  pinMode(IR_IN,INPUT);//init the ir receiver pin
  Serial.begin(9600);
  pcint0Init();
  sei();               //enable the interrupt
}
void loop(void){
  obstacleAvoidance();
}

Result

After you upload the sample code, you can open the serial port to check the result. Leave your hand in front of the vortex, it will feedback your hand position.

Was this article helpful?

TOP