Basic 4WD Robot obstacle avoiding concept

A last, thanks to this book I learned how to use capacitators to avoid motor interferences (I’m a total electronic newbie) and I rebuilt my 4WD robot from scratch.

For this test concept I used:

Arduino Uno

4WD robot

Arduino Motor Shield (good tutorial here)

HC-SR04 Ulrasonic sensor (good tutorial here)

To get the code you can mix the codes from both tutorials to do whatever you wish, its pretty easy.

4WD robot front view with HC-SR04 ultrasonic ranging module

4WD robot front view with HC-SR04 ultrasonic ranging module

4WD robot side view with arduino motor shield

4WD robot side view with arduino motor shield

 


NOTE: I’ve used a custom Ultrasonic library (link here) (download here or on their github site)

The robot code:

#include 

#define trigPin 2
#define echoPin 4

Ultrasonic ultrasonic(trigPin,echoPin); // (Trig PIN,Echo PIN)


void setup() {
  Serial.begin (9600);
  
  // put your setup code here, to run once:
  // Motor setup
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT); //Initiates Brake Channel B pin
  // HC-SR04 distance sensor setup
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}

void loop() {
  delay(100);
  if (isNearObject()) {
    //Serial.println("Stoped");
    stopRobot();
    turn();
    stopRobot();
  } else {
    //Serial.println("Running"); 
    goAhead();
  }
}

boolean isNearObject() {
  int distance;
  distance=ultrasonic.Ranging(CM);
  Serial.print(distance);
  Serial.println(" cm");
  if (distance>8) { return false; } else { return true; }
}

void goAhead() {
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 128);   //Spins the motor on Channel A at half speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 128);   //Spins the motor on Channel B at full speed
}

void stopRobot() {
  digitalWrite(9, HIGH); // engage break Channel A
  digitalWrite(8, HIGH); // engage break Channel B
}

void turn() {
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 128);   //Spins the motor on Channel A at half speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 128);   //Spins the motor on Channel B at full speed
  delay(1800);             // turn 1.8 second
}

 

Posted in Arduino, Concept, DIY, Electronics, In progress, Programming, Robots, Videos Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*