Seems like posting large code chunks here is cool? As opposed to using another service?
Oh, I'm a noob if my post count didn't make that clear. So please go easy on me. Just trying to learn.
Different movement tasks are broken up into their own functions. THis is one of the bajillion hacks of the SainSmart robot code that is out there. If you've seen it, you will notice the similarity.
As far as the "messy" part. Notice the distance checks in the turning functions (body_rturn/body_lturn), as well as nodanger/backup functions.
In general I haven't figured out how best to perform movement AND check distance while it is moving. I started creating ClearanceDecision functions, but haven't found them to be too helpful in checking more often.
FYI - I have a ultrasonic sensor on the front and back of the robot now. Just added, code may need adjustment for that.
Thanks!
#include <Servo.h>
const int EchoPin = 2; // Ultrasonic 1 signal input
const int TrigPin = 3; // Ultrasonic 1 signal output
const int RearEchoPin = 13; // Ultrasonic rear signal input
const int RearTrigPin = 12; // Ultrasonic rear signal output
const int leftmotorpin1 = 9; //signal output of DC geared motor
const int leftmotorpin2 = 10;
const int rightmotorpin1 = 5;
const int rightmotorpin2 = 6;
int currDist = 49; // distance
int currRearDist = 50; // reaer distance
int randomTurnTime = 0; // Time to spend in the random turn
void setup() {
Serial.begin(9600); // Serial begin texting
pinMode(EchoPin, INPUT);
pinMode(RearEchoPin, INPUT);
// for (int pinindex = 3; pinindex < 8; pinindex++) {
for (int pinindex = 3; pinindex < 13; pinindex++) {
pinMode(pinindex, OUTPUT); // set pins 3 to 13 as outputs
}
}
void loop() {
currDist = MeasuringDistance(); //measure front distance
currRearDist = MeasuringRearDistance(); //measure rear distance
if(currDist > 50) {
nodanger();}
else if(currDist <= 50){
totalhalt();
Serial.println("Look out!");
backup();
randTurn();
}
else {
//whichway();
randTurn();
}
}
//measure distance, unit:cm
long MeasuringDistance() {
long duration;
Serial.println("MeasuringDistance");
Serial.print("Current Distance: ");
Serial.println(currDist);
//pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
//pinMode(EchoPin, INPUT);
duration = pulseIn(EchoPin, HIGH);
return duration / 29 / 2;
}
//measure distance, unit:cm
long MeasuringRearDistance() {
long durationrear;
Serial.println("MeasuringRearDistance");
Serial.print("Current Rear Distance: ");
Serial.println(currRearDist);
//pinMode(TrigPin, OUTPUT);
digitalWrite(RearTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(RearTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(RearTrigPin, LOW);
//pinMode(EchoPin, INPUT);
durationrear = pulseIn(RearEchoPin, HIGH);
return durationrear / 29 / 2;
}
// forward
void nodanger() {
Serial.println("Rolling...");
// digitalWrite(leftmotorpin1, LOW);
// digitalWrite(leftmotorpin2, HIGH);
// digitalWrite(rightmotorpin1, LOW);
// digitalWrite(rightmotorpin2, HIGH);
analogWrite(leftmotorpin1, 0);
analogWrite(leftmotorpin2, 180);
analogWrite(rightmotorpin1, 0);
analogWrite(rightmotorpin2, 180);
delay(100);
}
//backward
void backup() {
boolean allclear = false;
Serial.println("backup");
currRearDist = MeasuringRearDistance(); //measure rear distance
if(currDist <= 50){
totalhalt();
Serial.println("Look out!");
allclear = false;
}
else {
allclear = true;
}
if (allclear) {
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, LOW);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, LOW);
delay(700);
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, HIGH);
delay(200);
}
}
void totalhalt() {
Serial.println("Total halt!");
digitalWrite(leftmotorpin1, HIGH);
digitalWrite(leftmotorpin2, HIGH);
digitalWrite(rightmotorpin1, HIGH);
digitalWrite(rightmotorpin2, HIGH);
delay(200);
}
//turn left
void body_lturn() {
int turntime;
turntime = randomTurnTime;
Serial.print("randomTurnTime: ");
Serial.println(randomTurnTime);
for (int looper = 0; looper < (turntime/1000); looper++) {
if (ClearanceBehindDecision()) {
Serial.println("Turning left while going backward: ");
analogWrite(leftmotorpin1, 200/3);
analogWrite(leftmotorpin2, 0);
analogWrite(rightmotorpin1, 200);
analogWrite(rightmotorpin2, 0);
//delay(turntime/3);
delay(1500);
totalhalt();
}
if (ClearanceAheadDecision()) {
Serial.println("Turning left while going forward: ");
analogWrite(leftmotorpin1, 0);
analogWrite(leftmotorpin2, 200);
analogWrite(rightmotorpin1, 0);
analogWrite(rightmotorpin2, 200/3);
//delay(turntime/3);
delay(1500);
totalhalt();
delay(turntime/4);
}
if(currDist > 50) {
looper = turntime;
}
}
}
//turn right
void body_rturn() {
int turntime;
Serial.print("randomTurnTime: ");
Serial.println(randomTurnTime);
turntime = randomTurnTime;
for (int looper = 0; looper < (turntime/1000); looper++) {
if (ClearanceBehindDecision()) {
Serial.println("Turning right while going backward: ");
analogWrite(leftmotorpin1, 200);
analogWrite(leftmotorpin2, 0);
analogWrite(rightmotorpin1, 200/3);
analogWrite(rightmotorpin2, 0);
//delay(turntime/3);
delay(1500);
totalhalt();
}
if (ClearanceAheadDecision()) {
Serial.println("Turning right while going forward: ");
analogWrite(leftmotorpin1, 0);
analogWrite(leftmotorpin2, 200/3);
analogWrite(rightmotorpin1, 0);
analogWrite(rightmotorpin2, 200);
//delay(turntime/3);
delay(1500);
totalhalt();
delay(turntime/4);
}
if(currDist > 50) {
looper = turntime;
}
}
}
void randTurn(){
Serial.println("randTurn");
long randNumber;
randomSeed(analogRead(0));
randNumber = random(2, 10);
Serial.print("randNumber= ");
Serial.println(randNumber);
randomTurnTime = (int(randNumber/2)+1) * 1000;
if(randNumber > 6) {
body_rturn();
}
else
{
body_lturn();
}
}
boolean ClearanceAheadDecision(){
boolean booallclear = false;
currDist = MeasuringDistance(); //measure front distance
if(currDist > 50) {
nodanger();
booallclear = true;
}
else {
totalhalt();
booallclear = false;
Serial.println("Look out ahead!");
}
return(booallclear);
}
boolean ClearanceBehindDecision(){
boolean booallclear = false;
currRearDist = MeasuringRearDistance(); //measure rear distance
if(currRearDist > 40) {
nodanger();
booallclear = true;
}
else {
totalhalt();
booallclear = false;
Serial.println("Look out behind you!");
}
return(booallclear);
}