Use the sensor library
This commit is contained in:
parent
e30e0aebf1
commit
0e3064ac3d
50
car.ino
50
car.ino
@ -1,3 +1,5 @@
|
|||||||
|
#include <hcsr04.h>
|
||||||
|
|
||||||
/* Define your pins here */
|
/* Define your pins here */
|
||||||
/* Wheels */
|
/* Wheels */
|
||||||
const int left_wheel_1 = 0;
|
const int left_wheel_1 = 0;
|
||||||
@ -9,8 +11,7 @@ const int right_wheel_2 = 3;
|
|||||||
const int sensor_echo = 9;
|
const int sensor_echo = 9;
|
||||||
const int sensor_trigger = 10;
|
const int sensor_trigger = 10;
|
||||||
|
|
||||||
/* Debug serial output */
|
HCSR04 hcsr04(sensor_trigger, sensor_echo, 20, 4000);
|
||||||
bool debug = true;
|
|
||||||
|
|
||||||
/* Array bellow contains all the wheels */
|
/* Array bellow contains all the wheels */
|
||||||
int wheels[] = {left_wheel_1, left_wheel_2, right_wheel_1, right_wheel_2};
|
int wheels[] = {left_wheel_1, left_wheel_2, right_wheel_1, right_wheel_2};
|
||||||
@ -18,6 +19,8 @@ int wheels[] = {left_wheel_1, left_wheel_2, right_wheel_1, right_wheel_2};
|
|||||||
/* The counter */
|
/* The counter */
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
int mm;
|
||||||
|
|
||||||
/* Ultrasonic sensor variables */
|
/* Ultrasonic sensor variables */
|
||||||
long duration;
|
long duration;
|
||||||
int distance;
|
int distance;
|
||||||
@ -41,108 +44,75 @@ void setup() {
|
|||||||
|
|
||||||
/* Forward */
|
/* Forward */
|
||||||
void move_forward_wheel_left() {
|
void move_forward_wheel_left() {
|
||||||
if (debug) { debug_log("WH", "Move forward left wheel"); }
|
|
||||||
digitalWrite(left_wheel_1, HIGH);
|
digitalWrite(left_wheel_1, HIGH);
|
||||||
digitalWrite(left_wheel_2, LOW);
|
digitalWrite(left_wheel_2, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_forward_wheel_right() {
|
void move_forward_wheel_right() {
|
||||||
if (debug) { debug_log("WH", "Move forward right wheel"); }
|
|
||||||
digitalWrite(right_wheel_1, HIGH);
|
digitalWrite(right_wheel_1, HIGH);
|
||||||
digitalWrite(right_wheel_2, LOW);
|
digitalWrite(right_wheel_2, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_forward() {
|
void move_forward() {
|
||||||
if (debug) { debug_log("WH", "Move forward"); }
|
|
||||||
move_forward_wheel_left();
|
move_forward_wheel_left();
|
||||||
move_forward_wheel_right();
|
move_forward_wheel_right();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Backward */
|
/* Backward */
|
||||||
void move_backward_wheel_left() {
|
void move_backward_wheel_left() {
|
||||||
if (debug) { debug_log("WH", "Move backward left wheel"); }
|
|
||||||
digitalWrite(left_wheel_1, LOW);
|
digitalWrite(left_wheel_1, LOW);
|
||||||
digitalWrite(left_wheel_2, HIGH);
|
digitalWrite(left_wheel_2, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_backward_wheel_right() {
|
void move_backward_wheel_right() {
|
||||||
if (debug) { debug_log("WH", "Move backward right wheel"); }
|
|
||||||
digitalWrite(right_wheel_1, LOW);
|
digitalWrite(right_wheel_1, LOW);
|
||||||
digitalWrite(right_wheel_2, HIGH);
|
digitalWrite(right_wheel_2, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_backward() {
|
void move_backward() {
|
||||||
if (debug) { debug_log("WH", "Move backward"); }
|
|
||||||
move_backward_wheel_left();
|
move_backward_wheel_left();
|
||||||
move_backward_wheel_right();
|
move_backward_wheel_right();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Stops */
|
/* Stops */
|
||||||
void stop_all() {
|
void stop_all() {
|
||||||
if (debug) { debug_log("WH", "Stop the wheels"); }
|
|
||||||
for (i = 0; sizeof(wheels) - 1; i++) {
|
for (i = 0; sizeof(wheels) - 1; i++) {
|
||||||
digitalWrite(wheels[i], LOW);
|
digitalWrite(wheels[i], LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_left() {
|
void stop_left() {
|
||||||
if (debug) { debug_log("WH", "Stop left wheel"); }
|
|
||||||
digitalWrite(left_wheel_1, LOW);
|
digitalWrite(left_wheel_1, LOW);
|
||||||
digitalWrite(left_wheel_2, LOW);
|
digitalWrite(left_wheel_2, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_right() {
|
void stop_right() {
|
||||||
if (debug) { debug_log("WH", "Stop right wheel"); }
|
|
||||||
digitalWrite(right_wheel_1, LOW);
|
digitalWrite(right_wheel_1, LOW);
|
||||||
digitalWrite(right_wheel_2, LOW);
|
digitalWrite(right_wheel_2, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Turns */
|
/* Turns */
|
||||||
void turn_left(int delayMS) {
|
void turn_left(int delayMS) {
|
||||||
if (debug) { debug_log("WH", "Turn left"); }
|
|
||||||
stop_left();
|
stop_left();
|
||||||
move_forward_wheel_right();
|
move_forward_wheel_right();
|
||||||
delay(delayMS);
|
delay(delayMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void turn_right(int delayMS) {
|
void turn_right(int delayMS) {
|
||||||
if (debug) { debug_log("WH", "Turn right"); }
|
|
||||||
stop_right();
|
stop_right();
|
||||||
move_forward_wheel_left();
|
move_forward_wheel_left();
|
||||||
delay(delayMS);
|
delay(delayMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helpers */
|
|
||||||
void debug_log(char tag, char message) {
|
|
||||||
Serial.print("[");
|
|
||||||
Serial.print(tag);
|
|
||||||
Serial.print("] ");
|
|
||||||
Serial.println(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Ultrasonic sensor actions */
|
/* Ultrasonic sensor actions */
|
||||||
|
|
||||||
/* get_distance -- returns distance to an object in centimeters */
|
/* get_distance -- returns distance to an object in centimeters */
|
||||||
int get_distance() {
|
int get_distance() {
|
||||||
/* Clears the trigger */
|
mm = hcsr04.distanceInMillimeters();
|
||||||
if (debug) { debug_log("US", "Clear the trigger"); }
|
if (mm > 0) {
|
||||||
digitalWrite(sensor_trigger, LOW);
|
return mm / 10;
|
||||||
delayMicroseconds(2);
|
}
|
||||||
|
return 0;
|
||||||
/* Set the trigger on HIGH state for 10 microseconds */
|
|
||||||
if (debug) { debug_log("US", "Set trigger HIGH"); }
|
|
||||||
digitalWrite(sensor_trigger, HIGH);
|
|
||||||
delayMicroseconds(10);
|
|
||||||
digitalWrite(sensor_trigger, LOW);
|
|
||||||
|
|
||||||
/* Reads the echo, returns the sound wave travel in microseconds */
|
|
||||||
duration = pulseIn(sensor_echo, HIGH);
|
|
||||||
if (debug) { debug_log("US", "Get echo duration"); }
|
|
||||||
|
|
||||||
/* Calculating the distance */
|
|
||||||
distance = duration / 58;
|
|
||||||
if (debug) { debug_log("US", "Got distance!"); }
|
|
||||||
return distance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Main code */
|
/* Main code */
|
||||||
|
Loading…
Reference in New Issue
Block a user