Parallax Ping))) and rgb led

Hi all, i have made a small alarm from rgb led. When you approach it turns red light on! otherwise it slightly blue)

i have used this sensor

http://parallax.com/sites/default/files/downloads/28015-PING-Detect-Distance.pdf

here you can check how it works

and here is the code:

const int pingPin = 7;
unsigned int duration, inches;
int Sensor = 0;
int value = 0;
int red = 6;

void setup() {
Serial.begin(9600);
}

void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2; // Convert to inches
Serial.println(inches); // Display result
delay(200); // Short delay

value = analogRead(pingPin); //read value from the sensor
delay (20);
analogWrite(red,inches);

}

 

kate

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s