EgoBot

1

So this thing has an ego problem (or maybe its just shy). It looks away each time you get real close to it.

Code:

#define trigPin 10
#define echoPin 11
#include <Servo.h>

Servo myservo;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(9);

randomSeed(analogRead(0));
}

void loop() {
int duration, distance,pos=0,i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(” cm”);
if(distance<15)
{
myservo.write(random(0,180));
//Serial.print(distance);
//Serial.println(” cm”);
}
delay(100);
}

Leave a Reply

Your email address will not be published.