Arduino code

const int PIRSensor = 8;// PIR sensor
int pingPin = 13;//ultrasonic sensor
int sensorValue = LOW;

void setup() {
pinMode(PIRSensor, INPUT);
Serial.begin(9600);
}

void loop(){
long duration, inches, cm;

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

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

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

sensorValue = digitalRead(PIRSensor);
delay(50);

if (sensorValue == HIGH) {
Serial.print(“1”);
Serial.print(“,”);
Serial.println(cm);

}
if (sensorValue == LOW) {

Serial.print(“0”);
Serial.print(“,”);
Serial.println(cm);

}

}
long microsecondsToInches(long microseconds)
{

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

return microseconds / 29 / 2;
}

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Advertisements
%d bloggers like this: