# My first robot

Here is my first Robot! THanks for the help I've gotten on this forum!

Though my cassis is just built from scrap, I love the look! And you gotta dig my wheels...pill bottle caps (very large ones) that I was using in my garage to store nails. They make great wheels in a pinch, though they are having trouble overcoming the traction of the rubber lego wheel in the back when pivoting.

Now you need to get a cat and then post a video of the results.

LOL...that does sound fun, though it's programmed for object avoidance right now. I haven't figured out how to do motion following yet...I'd eventually like to.

Right now the robot will pivot clockwise within 24" of any object. So if the better route of travel is the left, it will never make it.

I figure if I have the head scan side to side at about 90 degrees, I can somehow program it to head in the direction with most available distance. I don't have the logic for the code quite sorted out in my head yet so if anyone has any advice, I welcome it!

Here's my current code if that is helpful to anyone else:

``````const int rightPIN=5;
const int leftPIN=3;
const int pingPin = 4;
const int avoidinch = 22;

int leftforward=42;  //was 290
int rightforward=170;
int leftbackward=80;
int rightbackward=80;

void setup()
{
Serial.begin (9600);
pinMode (leftPIN,OUTPUT);
pinMode (rightPIN,OUTPUT);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): 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.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);

if (inches>avoidinch)
{ _forward();    }
else
{ _clockwise();  }
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).  This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

void _forward()
{
analogWrite (leftPIN,leftforward);
analogWrite (rightPIN,rightforward);
}

void _clockwise() //confirmed
{
analogWrite (leftPIN,leftforward);
analogWrite (rightPIN,rightbackward);
}
``````

I don't have the logic for the code quite sorted out in my head yet so if anyone has any advice, I welcome it!

The Ping sensor gives (indirectly) the distance to the nearest object. Turn the head 90 degrees, and take a reading.(distanceR). Then, rotate the head back to front, and then rotate 90 degrees the other way, and take another reading (distanceL).

Turn left or right based on whether distanceR or distanceL is greater.

Of course, you not limited to 90 degree head turns or robot turns. You could read 45 degrees on either side of straight ahead, and turn 45 degrees.

Dang...that is so marvelously simple I feel silly for not coming up with it on my own!

I don't have the logic for the code quite sorted out in my head yet so if anyone has any advice, I welcome it!

The Ping sensor gives (indirectly) the distance to the nearest object. Turn the head 90 degrees, and take a reading.(distanceR). Then, rotate the head back to front, and then rotate 90 degrees the other way, and take another reading (distanceL).

Turn left or right based on whether distanceR or distanceL is greater.

Of course, you not limited to 90 degree head turns or robot turns. You could read 45 degrees on either side of straight ahead, and turn 45 degrees.

^Double post m8?

Motion following would be cool, but you'll need to have 2 sensors (to sense if the object went left or right)... even then you will have little sensitivity and a lot of noise filtering to deal with... BUT IT CAN BE DONE! I DONT WANT TO DISCOURAGE, JUST A FRIENDLY WARNING ON WHAT TO LOOK OUT FOR!

Cheers!

Wow, awesome project!

/me

Now you need to get a cat and then post a video of the results.

;D