Saturday, May 16, 2015

PIR Motion Sensor

I have finally given in to the fact that somewhere along the way i bruno out my ultrasonido sensor. le sigh. Another example of "just because the light is on - doesn't mean anybody is home."

I switches over to a PIR Motion Sensor because that is all that was left at our shock in Costa Rica... I also atole this guys wiring and coding structure and cam up with what I h ave been trying to do for months within an hour....


If anyone out there cares... could you take a look at my ultrasonic/servo code and tell me if it could have worked??

#include <Servo.h>  
Servo neckservo;                                

int centerPos = 90;
int left = 180;
int right = 0;

int pingPin = 7;
long int duration, distanceInches;
long distanceFront=0;
int lookAround=20;

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

long distanceCm(){
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

distanceInches = microsecondsToInches(duration);
return microsecondsToCentimeters(duration);
}

void center()
{
neckServo.write(centerPos);
}

void scanRoom()
{
neckServo.write(left);
delay(700);
neckServo.write(centerPos);
delay(700);
neckServo.write(right);
delay(700);
neckServo.write(centerPos);
delay(700);
}

void setup()
{
neckServo.attach(2);
}

void loop()
{
distanceFront=distanceCm();
if (distanceFront > 1){
if (distanceFront<lookAround){
for(int i=0; i<=8; i++) {
scanRoom();
delay(700);
}
for(int i=0; i<=10; i++) {
scanRoom();
delay(700);
}
} else {
center();
delay(700);
}
}
}





No comments:

Post a Comment