arduino_car/car.ino

160 lines
3.6 KiB
Arduino
Raw Normal View History

2018-11-21 14:09:32 +03:00
/* Define your pins here */
/* Wheels */
2018-11-21 12:29:53 +03:00
const int left_wheel_1 = 0;
const int left_wheel_2 = 1;
2018-11-21 11:53:17 +03:00
const int right_wheel_1 = 2;
const int right_wheel_2 = 3;
2018-11-21 14:09:32 +03:00
/* Ultrasonic sensor */
const int sensor_echo = 9;
const int sensor_trigger = 10;
/* Debug serial output */
bool debug = true;
/* Array bellow contains all the wheels */
int wheels[] = {left_wheel_1, left_wheel_2, right_wheel_1, right_wheel_2};
2018-11-21 12:29:53 +03:00
/* The counter */
int i;
2018-11-21 14:09:32 +03:00
/* Ultrasonic sensor variables */
long duration;
int distance;
2018-11-21 11:53:17 +03:00
void setup() {
/* Setup pins */
2018-11-21 14:09:32 +03:00
/* wheels */
for (i = 0; i < sizeof(wheels) - 1; i++) {
pinMode(wheels[i], OUTPUT);
2018-11-21 11:53:17 +03:00
}
2018-11-21 14:09:32 +03:00
/* Ultrasonic sensor */
pinMode(sensor_echo, INPUT);
pinMode(sensor_trigger, OUTPUT);
/* Starts serial communication */
Serial.begin(9600);
2018-11-21 11:53:17 +03:00
}
/* Wheel actions */
/* Forward */
void move_forward_wheel_left() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move forward left wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(left_wheel_1, HIGH);
digitalWrite(left_wheel_2, LOW);
}
void move_forward_wheel_right() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move forward right wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(right_wheel_1, HIGH);
digitalWrite(right_wheel_2, LOW);
}
void move_forward() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move forward"); }
2018-11-21 11:53:17 +03:00
move_forward_wheel_left();
move_forward_wheel_right();
}
/* Backward */
void move_backward_wheel_left() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move backward left wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(left_wheel_1, LOW);
digitalWrite(left_wheel_2, HIGH);
}
void move_backward_wheel_right() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move backward right wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(right_wheel_1, LOW);
digitalWrite(right_wheel_2, HIGH);
}
void move_backward() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Move backward"); }
2018-11-21 11:53:17 +03:00
move_backward_wheel_left();
move_backward_wheel_right();
}
/* Stops */
void stop_all() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Stop the wheels"); }
2018-11-21 14:09:32 +03:00
for (i = 0; sizeof(wheels) - 1; i++) {
digitalWrite(wheels[i], LOW);
2018-11-21 11:53:17 +03:00
}
}
void stop_left() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Stop left wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(left_wheel_1, LOW);
digitalWrite(left_wheel_2, LOW);
}
void stop_right() {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Stop right wheel"); }
2018-11-21 11:53:17 +03:00
digitalWrite(right_wheel_1, LOW);
digitalWrite(right_wheel_2, LOW);
}
2018-11-21 12:29:53 +03:00
/* Turns */
2018-11-21 11:53:17 +03:00
void turn_left(int delayMS) {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Turn left"); }
2018-11-21 11:53:17 +03:00
stop_left();
move_forward_wheel_right();
delay(delayMS);
}
void turn_right(int delayMS) {
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("WH", "Turn right"); }
2018-11-21 11:53:17 +03:00
stop_right();
move_forward_wheel_left();
delay(delayMS);
}
2018-11-21 14:09:32 +03:00
/* Helpers */
void debug_log(char tag, char message) {
Serial.print("[");
Serial.print(tag);
Serial.print("] ");
Serial.println(message);
}
/* Ultrasonic sensor actions */
/* get_distance -- returns distance to an object in centimeters */
int get_distance() {
/* Clears the trigger */
if (debug) { debug_log("US", "Clear the trigger"); }
digitalWrite(sensor_trigger, LOW);
delayMicroseconds(2);
/* 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);
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("US", "Get echo duration"); }
2018-11-21 14:09:32 +03:00
/* Calculating the distance */
distance = duration / 58;
2018-11-21 14:14:28 +03:00
if (debug) { debug_log("US", "Got distance!"); }
2018-11-21 14:09:32 +03:00
return distance;
}
2018-11-21 11:53:17 +03:00
/* Main code */
void loop() {
2018-11-21 14:09:32 +03:00
if (get_distance() > 25) {
move_forward();
delay(1000);
} else {
turn_left(500);
move_backward();
delay(1000);
turn_right(500);
}
2018-11-21 11:53:17 +03:00
}