Saturday, January 17, 2015

Motion Detector

I feel like I  am moving right along. Today I built a motion detector. The device uses the Ping))) Ultrasonic sensor, LEDs and of course the Arduino. The sensor sends out high frequency sound to detect if there is something in front of it. I plan to use this sensor as the "eyes" of my robot that will be able to detect and overcome obstacles. Again, the lights here represent what will be the motors of of the bot.

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