I am relatively new to Arduino although I know some basic Javascript and electronics. My school purchased 9 arduino starter kits and the teachers weren't sure what they wanted to do with them, so I 'borrowed' one and assembled a boebot using two parallax continuous servos. The boebot kit was from 2006 in case you were wondering. I only used the frame, servos, and wheels from said kit. I am using the Arduino Uno (Branded as Elegoo Uno R3, the kit was from Elegoo) to control everything. My only sensor is a HC-SR04 ultrasonic sensor, and I have one indication LED. The objective of the robot is to move through a straight hallway and avoid obstacles such as boxes. The code I wrote is rough and could be improved upon, but it is functional, my only issue is consistency (the sonic sensor doesn't sense some objects, the who thing drift left or right, etc.).
The method I am using to navigate is when the robot finds an obstacle, it will turn right and go as far as it can, then turn left. Then as long as there is an object in front of it, it will move left and check if there's an object in front of it again. Once there isn't an object in front, it will go forward. The robot goes through the different modes by using the 'direct' variable. There are some remnants of things I tried but gave up on, but didn't delete. They are just idle variables.
If anyone has any suggestions on improving either the consistency or code, please feel free to comment.
// Servo Setup
#include <Servo.h>
Servo SerL;
Servo SerR;
// Pins
const int trig = 6;
const int echo = 7;
const int led = 13;
const int sr = 8;
const int sl = 9;
const int turnDelay = 750;
// variables
int direct = 0;
boolean fre = true;
int distance;
int calcDist() {
long duration, distance;
digitalWrite(trig, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trig, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;
return distance;
}
void Move() {
SerL.writeMicroseconds(2000);
SerR.writeMicroseconds(1000);
delay(1500);
SerR.writeMicroseconds(1500);
SerL.writeMicroseconds(1500);
}
void turnR() {
SerL.writeMicroseconds(2000);
SerR.writeMicroseconds(2000);
Serial.print("Ran Turn Right");
Serial.print("\n");
digitalWrite(led, HIGH);
delay(turnDelay);
digitalWrite(led, LOW);
SerL.writeMicroseconds(1500);
SerR.writeMicroseconds(1500);
}
void turnL() {
SerL.writeMicroseconds(1000);
SerR.writeMicroseconds(1000);
digitalWrite(led, HIGH);
Serial.print("Ran Turn Left");
Serial.print("\n");
delay(turnDelay);
digitalWrite(led, LOW);
SerL.writeMicroseconds(1500);
SerR.writeMicroseconds(1500);
}
void setup() {
pinMode(sl, OUTPUT);
pinMode(sr, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(led, OUTPUT);
SerL.attach(sl);
SerR.attach(sr);
Serial.begin(9600);
Serial.print("Ran Setup");
Serial.print("\n");
}
void loop() {
//Distance
Serial.print("\n");
distance = calcDist();
if(distance < 20 && distance > 0) {
if(direct == 0) {
direct = 1;
turnR();
distance = calcDist();
if(distance > 20) {
SerL.writeMicroseconds(2000);
SerR.writeMicroseconds(1000);
calcDist();
}
}
else if(distance < 20 && direct == 1) {
turnL();
distance = calcDist();
direct = 2;
}
else if(distance < 20 && direct == 2) {
turnL();
Move();
turnR();
delay(50);
}
}
else if(direct != 1) {
direct = 0;
fre = true;
Serial.print("Going");
Serial.print("\n");
SerL.writeMicroseconds(2000);
SerR.writeMicroseconds(1000);
}
// Serial Print
distance = calcDist();
Serial.print("\n");
Serial.print(direct);
Serial.print("\n");
Serial.print(distance);
Serial.print("\n\n");
delay(500);
}