I had a gh-311rt ultrasonic sensor which did not have a datasheet. So i ran it with a ping))) program. You can easily try it with the cheapest 3 pin sensor . No need to go after ping)))
here is what you need.
1x Gh 311-rt sensor (if you have ping))) use it.If you have hc sr04 modulate the code) from here.
2x BO motors (75 rpm)
1x motor driver module. over here.(i used a motor driver if you want to reduce it use servomotors modified for 360)
1x Chassis set here (i scavenged one from one kit)
1x Arduino uno (it is awesome)
1x programming cable
wires
2x batteries (i suggest duracell enercell or one from radioshack)
croc clips
now begin
get it together (chassis) leave a wire on the motor drivers power pins positioned behind and here is the program.
//this is a code for controlling a simple robot using an arduino and a motor driver.
//created by robobot3112
const int loopPeriod = 20;
unsigned long loopdelay = 0;
const int sensor = 12; // ARDUINO OUTPUT PIN FOR SENSOR INPUT/OUTPUT
const int p1 = 8; // ARDUINO OUTPUT PIN FOR MOTOR INPUTS
const int p2 = 7; // ARDUINO OUTPUT PIN FOR MOTOR INPUTS
const int p3 = 4; // ARDUINO OUTPUT PIN FOR MOTOR INPUTS
const int p4 = 2; // ARDUINO OUTPUT PIN FOR MOTOR INPUTS int ultraDistance;
int ultraDuration;
void setup()
{
Serial.begin(9600);
pinMode(p1 , OUTPUT);
pinMode(p2 , OUTPUT);
pinMode(p3 , OUTPUT);
pinMode(p4 , OUTPUT); }
void loop()
{
if(millis() - loopdelay >= loopPeriod)
{
readUltrasonicSensors();
motorstate();
loopdelay = millis();
}
}
void readUltrasonicSensors()
{
pinMode(sensor , OUTPUT);
digitalWrite(sensor , LOW);
delay(2);
digitalWrite(sensor , HIGH);
delayMicroseconds(10);
digitalWrite(sensor , LOW);
pinMode(sensor , INPUT);
ultraDuration = pulseIn(sensor , HIGH);
ultraDistance = (ultraDuration/2)/29;
}
void motorstate() // MOTOR CONTROL FUNCTION
{
if((ultraDistance > 15)||(ultraDistance < 0))
{
digitalWrite(p3 , HIGH);
digitalWrite(p1 , HIGH);
digitalWrite(p2 , LOW);
digitalWrite(p4 ,LOW); }
else
{
digitalWrite(p1 , HIGH);
digitalWrite(p2 , LOW);
digitalWrite(p3 , HIGH);
digitalWrite(p4 , LOW);
}
}
To power this beast you require 2 9v batteries (1 for motor driver and other for the Arduino).