First of all, hi to all.
By way of introduction;
I had dreamed of building a robot for years and years. 18 months ago on the 9th of June 2010 I found out about Arduino and fell in love. I had not done electronics or computer programming before. The most I'd done was 3D modelling on the computer (and that's just a professional hobby). This is not the sum-total of a year's worth of dedicated work. I am 32, I have a full-time job (working swing shift), I'm dating a wonderful and understanding lady (for over a year now), I spend time with my kids and I do all the other things that people generally do in life.
Here is my robot that Im making, and started making using an Arduino Duemilanove:
and
I then downsized the brain to an Arduino Pro Mini (3.3V, 8 MHz) w/ ATmega328:
I also mounted the batteries on top.
To give you some idea of what this is, is it's a four-legged, autonomous robot.
Each leg has three servos, giving them 3 degrees of freedom.
It has one sensor, an EZ0 Ultrasonic sensor on the front for obstacle avoidance.
The programming is set up with several components:
- Stand
- StandtoForward
- Forward
- ForwardtoStand
- RotateLeft
Because I only have one sensor, once I turn it on, it starts in Forward mode.
It then walks forward until it meets an object.
It then stands, then rotates left, approximately 90 degrees.
Then it either continues walking, or if there is an object in the way it rotates left again.
There is not much code in this robot. It's just repetitive because of the way I have set the robots movements. I have posted the main part of the code, but I haven't entered all the movements because they are just repetitions of servo.write(x), but I've included the Stand component.
Please note that I am in the process of reducing the code (currently sitting at around 500 lines). I intend on putting all the components into arrays to keep it tidy.
#include <Servo.h>
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
Servo myservo8;
Servo myservo9;
Servo myservo10;
Servo myservo11;
Servo myservo12;
Servo myservo13;
int pause = 300;
int sensorValue;
int f=0;
int rl=0;
void setup()
{
pinMode(0, OUTPUT);
myservo2.attach(2); //tip blue
myservo3.attach(3);
myservo4.attach(4);
myservo5.attach(5);
myservo6.attach(6);
myservo7.attach(7); //tip red
myservo8.attach(8); //tip green
myservo9.attach(9);
myservo10.attach(10);
myservo11.attach(11);
myservo12.attach(12);
myservo13.attach(13); //tip yellow
}
void loop()
{
sensorValue = analogRead(A0);
Serial.print(sensorValue);
Serial.println();
if (sensorValue > 20) //if far from object
{
if (f==1){
doForward();
}
else
if (f==0){
doStandtoForward();
f=1;
}
}
else
{
if(f==1) {
doForwardtoStand();
f=0;
}
else
if (f==0)
{
doStand();
delay(pause);
doRotateLeft();
doRotateLeft();
}
}
} //void end
void doStand()
{
myservo2.write(77); //tip blue
myservo3.write(57);
myservo4.write(90);
myservo5.write(90);
myservo6.write(123);
myservo7.write(103); //tip red
myservo8.write(77); //tip green
myservo9.write(57);
myservo10.write(90);
myservo11.write(90);
myservo12.write(123);
myservo13.write(103); //tip yellow
}