AI-Duino: Artificial Intelligence Robot with Arduino
Lets Be Friends
  • AI-Duino Overview
  • Aiduino Forum
  • AI-Duino Research Lab
    • HC-SR-4 Ultrasonic Sensor Module
    • Related Projects
  • AI-Duino Blog
  • AI-Duino Aims
  • AI-Duino Media
  • About AI-Duino
    • Contact Us
    • Donate

Obstacle Avoiding Arduino Powered Robot 

3/1/2016

0 Comments

 
This is just a quick entry to outline the latest development with the AI-Duino project. Having installed the ultrasonic sensor, I wrote a very messy sketch to make it obstacle avoiding. 

As I did not have a spare stepper motor and driver I chose to mount the ultrasonic sensor in a fixed position. Due to this, I made the AI-Duino turn completely to assess the space to its left and right (once it had encountered an obstacle). 

I have included the sketch for anyone who would like to use it. 

All comments, feedback and suggestions are welcomed.

obstacle avoiding robot sketch

N.B. This sketch utilises the ultrasonic sensor library discussed here. Also, please ignore my messy and unhelpful comments within the sketch. 

#include <Ultrasonic.h>

/*-----( Import needed libraries )-----*/
#include "Ultrasonic.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIG_PIN  12
#define  ECHO_PIN  13
/*-----( Declare objects )-----*/
Ultrasonic OurModule(TRIG_PIN, ECHO_PIN);
/*-----( Declare Variables )-----*/

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  [email protected]");
             
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(10, OUTPUT); 
  pinMode(11, OUTPUT); 
}
void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Serial.print(OurModule.Ranging(CM));
  Serial.print("cm   ");
  delay(100);  //Let echos from room dissipate
  Serial.print(OurModule.Ranging(INC));
  Serial.println("inches");
  int right = 0;
  int left = 0;
  delay(500);
  

  if((OurModule.Ranging(CM)) > 30) //if clear
    {
      Serial.print ("More than 30 CM");
      Serial.print (" - Clear to Drive Forward");
     digitalWrite(10, HIGH);   // set the LED on ... forward
     digitalWrite(6, HIGH);   // set the LED on ... forward
     //delay(1000);              // wait for a second
     //digitalWrite(10, LOW);    // set the LED off
     //digitalWrite(6, LOW);    // set the LED off
     //delay(10);              // wait for a second
      Serial.println (" - Drove Forward 1000");
      delay(100);
    }
  if((OurModule.Ranging(CM)) < 30) //if obstical is in the way
    {
      Serial.print ("Less than 30 CM");
      Serial.print (" - Obstical Detected");
      Serial.print (" - Searching Surroundings");
      digitalWrite(10, LOW);    // set the motors off
      digitalWrite(6, LOW);    // set the motors off
      digitalWrite(6, HIGH);   // set the LED on ... turn left
      digitalWrite(11, HIGH);   // set the LED on ... turn left
      delay(600);              // wait for 6/10 second
      digitalWrite(6, LOW);    // set the LED off
      digitalWrite(11, LOW);    // set the LED off
      delay(100);              // wait for a second
      Serial.print (" - Turned Left 90 Degrees ");
      left = (OurModule.Ranging(CM)); //take distance reading - left
      Serial.print(left);
      
      //digitalWrite(6, LOW);
      //digitalWrite(11, LOW);
      digitalWrite(5, HIGH); //turn right 180 degrees
      digitalWrite(10, HIGH);
      delay(1200);
      digitalWrite(5, LOW); // stop
      digitalWrite(10, LOW);
      Serial.print (" - Turned Right 180 Degrees ");
      right = (OurModule.Ranging(CM)); //take distance reading - right
      Serial.println(right);
      digitalWrite(6, HIGH);  // return to centre
      digitalWrite(11, HIGH); 
      delay(600);              // wait for 6/10 second
      digitalWrite(6, LOW);    //stop
      digitalWrite(11, LOW);
      delay(10);
      
      int dif = (left + 1000) - right; //working out if left or right has more space - less than 1000 means right has more room
      Serial.println (dif);
      
      if(dif>1000) // more room on the left
      {
        digitalWrite(6, HIGH); //turn left 
        digitalWrite(11, HIGH);
        delay(600);               //180 degrees
        digitalWrite(6, LOW); //stop
        digitalWrite(11, LOW);
        Serial.println(" turning 90 left - more space left");  
      }
      
      if(dif<1000) // more room on the right
      {
        digitalWrite(5, HIGH); //turn right 
        digitalWrite(10, HIGH);
        delay(600);               //180 degrees
        digitalWrite(5, LOW); //stop 
        digitalWrite(10, LOW);
        Serial.println(" turning 90 left - more space right");  
      }
    }
//}
//void loop() {
}
0 Comments

    Steven Gartland

    Creator and Developer of Ai-Duino

    Categories

    All
    Motor Controller
    Obstacle Avoiding Robot
    The Plan

    RSS Feed

    Donate to AI-Duino
Powered by Create your own unique website with customizable templates.