Tuesday, December 4, 2012

Proximity Sensor Controlled LED

Before today's meeting, we came up with the challenge (to a group of novices) to control a LED so that it turns on based on the distance of an object from a proximity sensor.

One verified technique:**

#include 

#define TRIGGER_PIN  11  
#define ECHO_PIN     12  
#define MAX_DISTANCE 200
int ledPin = 10;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  delay(50);
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  float objDist = uS / US_ROUNDTRIP_CM;
  Serial.print(objDist); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.println("cm");
  if(objDist > 0.1 && objDist <= 25){
   digitalWrite(ledPin, HIGH); 
  }
  else{
    digitalWrite(ledPin, LOW);
  }
}

**The html formatting to post the code removes the library name from the #include line. The library name should follow the #include command.

No comments:

Post a Comment