8. Case 06: An Obstacle-avoidance Car#

8.1. Purpose#


Make an obstacle-avoidance car with NezhaA Inventor’s Kit.../../_images/neza-a-case-06-01.png

8.2. Purchse#


NezhaA Inventor’s Kit

8.3. Materials Required#


../../_images/neza-a-case-06-02.png

8.4. Assembly Steps#


../../_images/neza-a-step-06-01.png ../../_images/neza-a-step-06-02.png ../../_images/neza-a-step-06-03.png ../../_images/neza-a-step-06-04.png ../../_images/neza-a-step-06-05.png ../../_images/neza-a-step-06-06.png ../../_images/neza-a-step-06-07.png ../../_images/neza-a-step-06-08.png ../../_images/neza-a-step-06-09.png ../../_images/neza-a-step-06-10.png ../../_images/neza-a-step-06-11.png ../../_images/neza-a-step-06-12.png ../../_images/neza-a-step-06-13.png ../../_images/neza-a-step-06-14.png ../../_images/neza-a-step-06-15.png ../../_images/neza-a-step-06-16.png ../../_images/neza-a-step-06-17.png ../../_images/neza-a-step-06-18.png ../../_images/neza-a-step-06-19.png ../../_images/neza-a-step-06-20.png ../../_images/neza-a-step-06-21.png ../../_images/neza-a-step-06-22.png ../../_images/neza-a-step-06-23.png ../../_images/neza-a-step-06-24.png ../../_images/neza-a-step-06-25.png ../../_images/neza-a-step-06-26.png ../../_images/neza-a-step-06-27.png ../../_images/neza-a-step-06-28.png ../../_images/neza-a-step-06-29.png ../../_images/neza-a-step-06-30.png ../../_images/neza-a-step-06-31.png ../../_images/neza-a-step-06-32.png ../../_images/neza-a-step-06-33.png ../../_images/neza-a-step-06-34.png ../../_images/neza-a-step-06-35.png ../../_images/neza-a-step-06-36.png ../../_images/neza-a-step-06-37.png ../../_images/neza-a-step-06-38.png ../../_images/neza-a-step-06-39.png ../../_images/neza-a-step-06-40.png

8.5. Hardware Connections#


Connect two motors to M1, M2 port and the ultrasonic sound sensor to J1 port on Nezha-A master box.

../../_images/neza-a-case-06-03.png

8.6. Programming#


8.6.1. Prepare the programming#

Steps for preparation please refer to: Arduino 3 in 1 Breakout Board

Import the libraries and the subsidiary libraries of Nezha-A master box and then import the libraries of the ultrasonic sound sensor: PlanetXUltrasonic-main.zip Download and import the self-defined library connections for Nezha-A master box: RJPins-main.zip

8.6.2. Sample Projects:#

// Language ArduinoC
#include <PlanetXUltrasonic.h>
#include <NezhaA.h>
#include <RJPins.h>

PlanetXUltrasonic ultrasonicJ1(J1);    //Create an instance of PlanetXUltrasonic category
NezhaA nezhaa;    //Create an instance of NezhaA category
int distance;    //Create a variable of int distance

void setup() {
  nezhaa.begin();    //Initiliaze the buzzer, motor, servo and light
}

void loop() {
  distance = (ultrasonicJ1.getDistance());    //Set the detected value from ultrasonic sound sensor as the variable distance 
  if (distance > 3 && distance < 20) {    //Judge if the variable is over 3 and below 20
    nezhaa.setMotorSpeed(M1, 15);    // Set the speed of the motor connecting to M1 at 15%
    nezhaa.setMotorSpeed(M2, -15);    //Set the speed of the motor connecting to M2 at -15%
    delay((1) * 1000);    //Pause 1000ms
  } else {
    nezhaa.setMotorSpeed(M1, 20);
    nezhaa.setMotorSpeed(M2, 20);
  }
}

8.6.3. Result#

After powering on, the car moves forward and it turns autamatically if it detects any obstacles.