Jon Garich - Final Code

#include <Servo.h>

 

/*

ignore all of the commented out parts (I didn't end up using the 

commented parts but I wanted to keep them in there just in case 

I took it out for no reason)

*/

 

Servo steering;

//Servo pan;

 

//int steeringPos = 90;

//int panPos = 90;

const int pingPin = 7; //Ultrasound signal pin

//int ledPin = 13; //LED connected to digital pin 13 (just to check if its good)

//int switchPin = 0; //switch to set direction of the motor (possible input)

//const int motor1Pin1 = 3; //Motor 1 Pin 1

//const int motor1Pin2 = 4; //Motor 1 Pin 2

//int enablePin = 9; //pin to start the motor (possible input)

 

void setup() {

  Serial.begin(9600); //sets baud rate to 9600

  //pinMode(ledPin, OUTPUT); //sets the digital pin as output

  //set the motor pins to output (switch the poles to reverse direction)

  //pinMode(motor1Pin1, OUTPUT); 

  //pinMode(motor1Pin2, OUTPUT);

  steering.attach(9);

  //pinMode(enablePin, OUTPUT);

  //set the enablePin high so that the motor can turn on

  //digitalWrite(enablePin, HIGH);

  zeroSteering(90);

  //zeroPan(90);

}

 

void loop() {

   long inches;

   long duration;

   //startMotor(switchPin);

   pinMode(pingPin, OUTPUT); //switch signal pin to output

   sendPulse();  

   //listen for echo pulse

   pinMode(pingPin, INPUT); //switch signal pin to input

   duration = pulseIn(pingPin, HIGH); //append signal duration to duration

   inches = microsecondsToInches(duration);

   writeOut(inches);

   //do stuff if any value is passed by the echo pulse

   if (inches < 40) {

     steering.write(105);

     delay(50);

     steering.write(75);

     delay(50);

   }

   //delay of program

   delay(100);

}

 

long microsecondsToInches(long m) {

  // 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 m / 74 / 2;

}

 

void zeroSteering(int p) {

  steering.write(p);

}

 

/*void zeroPan(int p) {

  pan.write(p);

}

*/

 

/*void startMotor(int s) {

  //move the vehicle forward

  if (s == 1) {

    digitalWrite(motor1Pin1, LOW);

    digitalWrite(motor1Pin2, HIGH); 

  }

  //stop the vehicle

  else if (s == 0) {

    digitalWrite(motor1Pin1, LOW);

    digitalWrite(motor1Pin2, LOW);

  }

}

*/

 

void sendPulse() {

  //send low-high-low pulse to activate the trigger pulse of the sensor

   digitalWrite(pingPin, LOW); //send low pulse

   delayMicroseconds(2); //wait for 2 microseconds

   digitalWrite(pingPin, HIGH); //send high pulse

   delayMicroseconds(5); //wait for 5 microseconds

   digitalWrite(pingPin, LOW); //holdoff

}

 

void writeOut(int t) {

  Serial.print(t);

  Serial.print("in");

  Serial.println();

}

 

/*

void objectFound() {

  //do stuff when the object is found

  steering.write(105);

  delay(50);

  steering.write(75);

  delay(50);

}

*/