Case 06: An Obstacle-avoidance Car
Contents
9. Case 06: An Obstacle-avoidance Car#
9.1. Purpose#
Make an obstacle-avoidance car with NezhaA Inventor’s Kit.
9.2. Purchse#
9.3. Materials Required#
9.4. Assembly Steps#
9.5. Hardware Connections#
Connect two motors to M1, M2 port and the ultrasonic sound sensor to J1 port on Nezha-A master box.
9.6. Programming#
9.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
9.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);
}
}
9.6.3. Result#
After powering on, the car moves forward and it turns autamatically if it detects any obstacles.