Introduction: Arduino Wandering Robot (Improvised)
This is a tutorial on how to make an Arduino robot that will wonder around and avoid walls and other obstacles. It is a very easy thing to do ands is based for beginners and to teach them and get them into robotics, it is fun to do, I know my dog loves this, it barks and chases it around the house.
(Sorry this isn't a very good instructable, this is my first one and still getting a hang of it.)
Step 1: Recourses
Collect your Items:
Arduino Uno or Mega: http://www.freetronics.com.au/products/eleven#.VZ... // uno link
H-Bridge Shield: http://www.freetronics.com.au/products/eleven#.VZ...
Ultra Sonic Sensor: http://www.freetronics.com.au/products/eleven#.VZ...
Chassis: http://www.freetronics.com.au/products/eleven#.VZ...
Batteries: I suggest 3 9v Batteries
Tools:
(you don't really need tools but maybe...)
Wire strippers
Soldering Iron
Pliers
Step 2: Wiring
Start with the Arduino Uno;
Put the H-Bridge shield on top; (Making sure that the pins are all in the right spots)
Connect the sensor to the shield, put the "trig" to pin 10: put the "echo" to pin 9: and then Gnd to GND and Positive to 5V;
Connect 2 9v batteries to shield; (Do it in a series circuit)
Add the 2 dc motors to the inputs;
Step 3: Putting on the Chassis
It should look something like this once it is done. The sensor should be at the front of the chassis. the Arduino behind it and then the battery packs behind the Arduino.
(sorry my chassis hasn't arrived yet)
Step 4: Code
Here is my code;
Please respect it took me time to write this;
//Code By Jason McLaughlin
//2015
const int channel_a_enable = 6;
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;
const int channel_b_enable = 5;
const int channel_b_input_3 = 3;
const int channel_b_input_4 = 2;
#define trigPin 10
#define echoPin 9
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode( channel_a_enable, OUTPUT ); // Channel A enable
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2
pinMode( channel_b_enable, OUTPUT ); // Channel B enable
pinMode( channel_b_input_3, OUTPUT ); // Channel B input 3
pinMode( channel_b_input_4, OUTPUT ); // Channel B input 4
Serial.begin( 9600 );
Serial.println("Starting up"); }
void loop() {
//Ping/Ultra Sonic Sensor
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
delay(0);
//Forwards
if(distance>5) {Serial.println("forwards :)");
analogWrite( channel_a_enable, 255);
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);
analogWrite( channel_b_enable, 255);
digitalWrite( channel_b_input_3, HIGH);
digitalWrite( channel_b_input_4, LOW);
delay(50);
allInputsOff();}
else if(distance<5)
{ }
else {Serial.println("Channel A Idle");
analogWrite( channel_a_enable, 0);
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
delay(50);
allInputsOff();}
if(distance<5)
{Serial.println("Turing");
analogWrite( channel_a_enable, 255);
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, HIGH);
analogWrite( channel_b_enable, 255);
digitalWrite( channel_b_input_3, HIGH);
digitalWrite( channel_b_input_4, LOW);
delay(50);
allInputsOff();}
else if (distance>5) { }
else
{Serial.println("Channel A reverse Idle");
analogWrite( channel_a_enable, 0);
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);
delay(50);
allInputsOff();}
}
void allInputsOff()
{
digitalWrite( 4, LOW );
digitalWrite( 7, LOW );
digitalWrite( 6, LOW );
digitalWrite( 3, LOW );
digitalWrite( 2, LOW );
digitalWrite( 5, LOW );
}