Created
November 4, 2015 09:18
-
-
Save AbhishekGhosh/86e7bb81c99bce8c4684 to your computer and use it in GitHub Desktop.
arduino_bulb_ultrasonic_sensor.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int pingPin = 13; | |
int inPin = 12; | |
int bulbZone = 5; | |
int bulbLed = 3, redLed = 2; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
long duration, cm; | |
pinMode(pingPin, OUTPUT); | |
pinMode(bulbLed, OUTPUT); | |
pinMode(redLed, OUTPUT); | |
digitalWrite(pingPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(pingPin, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(pingPin, LOW); | |
pinMode(inPin, INPUT); | |
duration = pulseIn(inPin, HIGH); | |
cm = msCms(duration); | |
Serial.print(cm); | |
Serial.print("cm"); | |
Serial.println(); | |
if (cm > bulbZone) | |
{ | |
digitalWrite(bulbLed, HIGH); | |
digitalWrite(redLed, LOW); | |
} | |
else | |
{ | |
digitalWrite(redLed, HIGH); | |
digitalWrite(bulbLed, LOW); | |
} | |
delay(100); | |
} | |
long msCms(long microseconds) | |
{ | |
return microseconds / 29 / 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment