This article shows you how to build a basic obstacle avoiding robot using NanoPlayBoard.
Inventory
- NanoPlayBoard.
- 9v battery.
- 2 Continuous rotation servos.
- Ultrasonic sensor HC-SR04.
- BQ Printbot Renacuajo chassis.
Sketch
Upload this sketch on the NanoPlayBoard:
#include <NanoPlayBoard.h>
#define MAX_DISTANCE_FROM_OBSTACLE 5
NanoPlayBoard board;
void setup() {
}
void loop() {
int distance = board.ultrasound.pingCm();
if (distance < MAX_DISTANCE_FROM_OBSTACLE && distance !=0) {
board.servos.stop();
board.servos.goBackward(1000);
board.servos.goRight(1000);
} else {
board.servos.goForward();
}
}
The source code of this sketch is available on GitHub.