My first Arduino based bot :)

part 2

int getrange()
{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
ultrasoundValue = (ultrasoundValue / 30); //rough conversion to inches
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
delay(100);
return ultrasoundValue;
}

void lookaround(int inmotion) {
int dist_left = 0;
int dist_right = 0;
int dist_center = 0;
while (count < 1900) {
servo1.write(25); //move eyes servo left
Servo::refresh();
count++;
//look left
}
//measure distance here, store in dist_left var

dist_left = getrange();

if (dist_left <=14) {
if (inmotion = 1){
driveit(500, 8); //rotate left 1
}
else motorstop();
makesounds();
}
count = 0;

if (inmotion =1) { //look center during sweep if inmotion
while (count < 1900) {
servo1.write(90); //move eyes servo center
Servo::refresh();
count++;
//look center
}
//measure distance here, store in dist_center var

dist_center = getrange();

if (dist_center <=14) {
if (inmotion = 1){
driveit(500, 6); //go back
}
else motorstop();
makesounds();
}
count = 0;
}

while (count < 1900) {
servo1.write(135); //move eyes servo right
Servo::refresh();
count++;
//look right
}
//measure distance here, store in dist_right var

dist_right = getrange();

if (dist_right <=14) {
if (inmotion = 1){
driveit(500, 7); //rotate right 1
}
else motorstop();
makesounds();
}

count = 0;
while (count < 1900) {
servo1.write(90);
Servo::refresh();
count++;
//look center
}
//measure distance here, store in dist_center var

dist_center = getrange();

if (dist_center <=14) {
if (inmotion = 1){
driveit(500, 6); //go back
}
else motorstop();
makesounds();
}

//analyze ranges
if (dist_left < dist_right) {
if (dist_left < dist_center) {
driveit(500, 8); //rotate left 1/8
}
}
if (dist_right < dist_left) {
if (dist_right < dist_center) {
driveit(500, 7); //rotate right 1/8
}
}
if (dist_center < dist_right) {
if (dist_center < dist_left) {
if (dist_left < dist_right) {
driveit(500, 8); //rotate left 1/2
}
else driveit (1000,7); //rotate right 1/2
}

count = 0;
}

void makesounds(){
count1 = 0;
count3 = 50;
for (count1 = 1; count1 <=45; count1++) {
if (count3 <=7) {
dir = false;
}
if (dir) count3 = count3 - 77;
else {
count3 = count3 + 77;
if (count3 >= 777) dir = true;
count1++;
}

digitalWrite(spkr,HIGH);
delayMicroseconds(count3);

digitalWrite(spkr,LOW);
delayMicroseconds(count3);

digitalWrite(spkr,HIGH);
delayMicroseconds(7777);

digitalWrite(spkr,LOW);
delayMicroseconds(count3);

digitalWrite(spkr,HIGH);
delayMicroseconds(77);

digitalWrite(spkr,LOW);
delayMicroseconds(count3);
}
}

[/quote]