Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created November 4, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/86e7bb81c99bce8c4684 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/86e7bb81c99bce8c4684 to your computer and use it in GitHub Desktop.
arduino_bulb_ultrasonic_sensor.cc
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