Case 08: A Big Beetle
Contents
11. Case 08: A Big Beetle#
11.1. Purpose#
Make a big beetle with NezhaA Inventor’s Kit.
11.2. Purchse#
11.3. Materials Required#
11.4. Assembly Steps#
11.5. Hardware Connections#
Connect two motors to M1 and M2 port, the ultrasonic sound sensor to J1 port and the line-tracking sensor on J2 port on Nezha-A master box.
11.6. Programming#
11.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 line-tracking sensor: PlanetXTracking-main.zip
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
11.6.2. Sample Code:#
// Language ArduinoC
#include <NezhaA.h>
#include <RJPins.h>
#include <PlanetXTracking.h>
#include <PlanetXUltrasonic.h>
PlanetXTracking trackingJ2(J2); //Create an instance of PlanetXTracking category
NezhaA nezhaa; //Create an instance of Nezha category
PlanetXUltrasonic ultrasonicJ1(J1); // Create an instance of PlanetXUltrasonic 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, 0); //Set the speed of the motor connecting to M1 at 0%
nezhaa.setMotorSpeed(M2, 0); //Set the speed of the motor connecting to M2 at 0%
}
else {
if (trackingJ2.isTracked(Right)) {
nezhaa.setMotorSpeed(M1, 0); //Set the speed of the motor connecting to M1 at 0%
nezhaa.setMotorSpeed(M2, 20); //Set the speed of the motor connecting to M2 at 20%
}
if (trackingJ2.isTracked(Left)) {
nezhaa.setMotorSpeed(M1, 20); //Set the speed of the motor connecting to M1 at 20%
nezhaa.setMotorSpeed(M2, 0); //Set the speed of the motor connecting to M2 at 0%
}
}
}
11.6.3. Result#
After powering on, the beetle walks along with the line and it stops while it detects the obstacles.