Here is a photo:
And here is a short video of the motion detector functioning:
And finally, a quick shout out to Make: Arduino Bots and Gadgets where I retrieved the base of the code.
/* Ping))) Sensor
*/
const int pingPin = 2;
const int motorPin = 9;
long int duration, distanceInches, distanceCm;
int limitCm = 60;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
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);
distanceCm = microsecondsToCentimeters(duration);
checkLimit();
delay(100);
}
void checkLimit()
{
if (distanceCm < limitCm){
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 /2;
}
No comments:
Post a Comment