part 1
#include <Servo.h>
Servo servo1;
#define spkr 9 // speaker + pin9 - digital
#define ultraSoundSignal 7 // Ultrasound signal pin - digital
#define ledPin 13 // LED connected to digital pin 13
#define motor1a 5 // motor1 pin5 - digital
#define motor1b 4 // motor1 pin4 - digital
#define motor2a 2 // motor2 pin2 - digital
#define motor2b 3 // motor2 pin3 - digital
#define eyeleds 8
//int count = 50;
int count = 0;
int count1 = 0;
//int count2 = 0;
int count3 = 50;
int count4 = 0;
boolean dir = true;
int servoval = 95;
int moveservo;
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int obded = 0;
int usv1 = 0;void setup()
{
servo1.attach(14); //analog pin 0 - eyes/sonar device
pinMode (ledPin, OUTPUT); // Sets the digital pin as output
pinMode (1, OUTPUT);
pinMode (motor1a, OUTPUT);
pinMode (motor2a, OUTPUT);
pinMode (motor1b, OUTPUT);
pinMode (motor2b, OUTPUT);
pinMode (spkr, OUTPUT);
pinMode (eyeleds, OUTPUT);
}void loop()
{
timecount = 0;
val = 0;
digitalWrite(eyeleds, HIGH); //turn on eyeleds while in main loop
usv1 = getrange();
if (usv1 <=14) {
motorstop();
makesounds();
lookaround(0);
count4 = 0;
} //lookaround if somethings close
if (usv1 > 8) {
driveit(500, 9);
} //go exploring
count4++;
if (count4 >=10) {
count4 = 0;
lookaround(1);
} //no motorstop called to make realtime course changes?
}void driveit (int cval, int dir) {
switch (dir) {case 1: //left motor forward
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
delay (cval);
motorstop();
break;case 2: //left motor backward
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
delay (cval);
motorstop();
break;case 3: //right motor forward
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay (cval);
motorstop();
break;case 4: //right motor backward
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
delay (cval);
motorstop();
break;case 5: //both left and right forward - forward
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay (cval);
motorstop();
break;case 6: //both left and right backward - back
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
delay (cval);
motorstop();
break;case 7: //left forward right backward - rotate right
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
delay (cval);
motorstop();
break;case 8: //left backward right forward - rotate left
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay (cval);
motorstop();
break;case 9: //set forward with no time, wait for sonar to brake
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);default:
break;
}
}void motorstop () { //reset all outputs as well as put the motors in "brake" mode
Which technically invalidates
//all of the "low" state declarations in the driveit()routines
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
}