This Tutorial is for anyone getting started with arduino. We will be building an obstacle avoiding robot that uses an ultrasonic HC SR-04 sensor to detect objects and 2 continuous (360 degree) servos wired to an external battery supply. This project uses an Arduino Uno without motor shield. The code is not mine, but not sure who to give credit to as I combined about 4 different sketches I found online.
Comment with any questions and sorry I said UMMM about 3000 times, I get nervous on camera...
CH340g driver for chinese arduino uno can be downloaded here:
http://www.wch.cn/download/CH341SER_MAC_ZIP.html
#include <Servo.h>
int trigPin = 2;
int echoPin = 4;
long duration, cm, inches;
Servo servoLeft;
Servo servoRight;
int forward = 180; // full speed forward
int backward = 0; // full speed backward
void setup() {
servoLeft.attach(10);
servoRight.attach(9);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
//sets both servos to full speed in the same direction
servoLeft.write(forward);
servoRight.write(backward);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(4);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// converts the time to a distance
cm = (duration / 2) / 29.1;
inches = (duration / 2) / 74;
delay(250);
//Sets object detection distance to 4.5
if (inches < 4.5) {
//stops both servos
servoLeft.write(90);
servoRight.write(90);
delay(1000);
//sets both servos to full speed in opposite direction
servoLeft.write(backward);
servoRight.write(forward);
delay(1000);
//sets only the left servo to rotate so the robot turns
servoLeft.write(forward);
delay(1500);
}//end if statement
}//ends loop
My name is Matthew, I am 26, and I attend the University of Pittsurgh. Currently I am a junior, going for a bachelors in Information Science. I am always looking for someone that would like to embark on a new project or startup. I work as a food server when I am not at school, but would love something a little more technical, if anyone is hiring.