Last active
December 28, 2015 23:19
-
-
Save dannystaple/7578133 to your computer and use it in GitHub Desktop.
Wall avoiding code - used at http://orionrobots.co.uk/blog/explorer_wall_avoider_kit.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
class SR04 { | |
public: | |
int trigPin; | |
int echoPin; | |
void setup() { | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
} | |
void trigger() { | |
// the sr04 is triggered by a HIGH pulse of 10 or more microseconds. | |
// Short low pulse, clean high, then low. | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(11); | |
digitalWrite(trigPin, LOW); | |
} | |
long microsecondsToCentimeters(long microseconds) | |
{ | |
// The speed of sound is 340 m/s or 29 microseconds per centimeter. | |
// The ping travels out and back, so to find the distance of the | |
// object we take half of the distance travelled. | |
return microseconds / 58; // (use 148 for inches) | |
} | |
long readDist() { | |
trigger(); | |
// The echo pin is used to read the signal from the hc-sr04. A HIGH | |
// pulse whose duration is the time (in microseconds) from the sending | |
// of the ping to the reception of its echo off of an object. | |
long duration = pulseIn(echoPin, HIGH); | |
// convert the time into a distance | |
int cm = microsecondsToCentimeters(duration); | |
return cm; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
setup motors | |
setup sensors | |
start moving forward | |
} | |
void loop() { | |
read sensors | |
while either below threshold: | |
if left sensor is closer: | |
turn right a bit | |
else while right sensor is closer: | |
turn left bit | |
keep going forward | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sensor.readDist() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
motors.forward(); | |
motors.turnLeft(); | |
motors.turnRight(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TurtleMotors.h" | |
#include "DistanceSensor.h" | |
//Define the devices | |
Motor motor_left = {3, 4, 5}; | |
Motor motor_right = {10, 8, 9}; | |
TurtleMotors motors = {motor_left, motor_right, 255, 100}; | |
SR04 sensor_left = {11, 12}; | |
SR04 sensor_right = {6, 7}; | |
void setup() { | |
motors.setup(); | |
sensor_left.setup(); | |
sensor_right.setup(); | |
motors.forward(0); | |
} | |
//Threshold to avoid at | |
#define SENSOR_AVOID_DIST 30 | |
void loop() { | |
int l = sensor_left.readDist(); | |
int r = sensor_right.readDist(); | |
while (l < SENSOR_AVOID_DIST || r < SENSOR_AVOID_DIST) { | |
//if left is closer than right | |
if (l < r) { | |
motors.turnRight(); | |
} else { | |
motors.turnLeft(); | |
} | |
l = sensor_left.readDist(); | |
r = sensor_right.readDist(); | |
} | |
motors.forward(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment