Introduction: ServYo Self Some Motion

Hello! This is how you make a motion following robot!!

Step 1: Get Materials

Required Materials:

• Breadboard

• Ultrasonic Sensor

• Servo

• Arduino

• wires (female to male and male to male)

Required Tools:

• Hot glue gun

• Xacto knife

• cardboard

•clay

Step 2: Build a Mount for Sensors

  1. get a piece of cardboard about 10cm length and 5cm in width
  2. bend middle of cardboard slightly
  3. on each half of the cardboard make 2 rectangular incisions that are 2cm in length and 1cm in width
  4. cut out a cardboard triangle that fits the bend base of the cardboard
  5. then glue the triangle to the rectangular

Step 3: Build

  • set up sensors in the newly made cardboard base
  • glue down to secure it
  • use no hardening clay to hold the servo
  • attached to the sensors in place

Step 4: Set Up Wires

follow the diagram upon for wire set up on the arduino and breadboard

  • we used pins 9 through 13

Step 5: Code!!!

Set up your code following this

/*************************************************************************************************
************************************************************************************************** Motion Follow Created by Calvin Kielas-Jensen

************************************************************************************************** *************************************************************************************************/

#include

Servo myservo;

const int Rin = 10, Lin = 12, Rout = 11, Lout = 13, serv = 9; //setting sensor pins and servo pin

// establish variables for duration // and the distance result in inches long Rduration, Lduration, Rinches, Linches;

int threshold = 10; //Sensor threshold in inches

int angle = 90; //Initial angle for the robot to turn to

boolean debug = true; //Serial communication for debuging. Set to true for serial communication.

void setup() { // initialize serial communication: if (debug) { Serial.begin(9600); } myservo.attach(9); //attach servo to pin 9 }

void loop() { //Most of the sensor code has been taken from David Mellis's PING sensor code //I modified it for a 4 pin sensor as oppsed to the 3 pin sensor // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(Rout, OUTPUT); digitalWrite(Rout, LOW); delayMicroseconds(2); digitalWrite(Rout, HIGH); delayMicroseconds(5); digitalWrite(Rout, LOW);

Rduration = pulseIn(Rin, HIGH); pinMode(Lout, OUTPUT); digitalWrite(Lout, LOW); delayMicroseconds(2); digitalWrite(Lout, HIGH); delayMicroseconds(5); digitalWrite(Lout, LOW);

Lduration = pulseIn(Lin, HIGH);

// convert the time into a distance Rinches = microsecondsToInches(Rduration); Linches = microsecondsToInches(Lduration); if (debug) { Serial.print("Left: "); Serial.print(Linches); Serial.println(" in"); Serial.print("Right: "); Serial.print(Rinches); Serial.println(" in"); } follow(); }

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-PI... return microseconds / 74 / 2; }

void follow() // this tells it where to turn the servo based on the ditance it detects { if (Linches <= threshold || Rinches <= threshold) { if (Linches + 2 < Rinches) { angle = angle - 2; } if (Rinches + 2 < Linches) { angle = angle + 2; } } if (angle > 160)//if the angle goes more than 160, then send it back to 160 to stop it from spinning around { angle = 160; } if (angle < 0) { angle = 0; } myservo.write(angle); }

Step 6: FINSHED!!!!

Now play and test to see if it works

If it doesn't work check all wire and code to see if there are any mistakes

credit:

MagicByCalvin--teaching it originally to us

Calvin Kielas-Jensen- creating the code