Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2015 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/02d9511dda8c4a2a7227 to your computer and use it in GitHub Desktop.
Save anonymous/02d9511dda8c4a2a7227 to your computer and use it in GitHub Desktop.
#include <Boards.h>
#include <Firmata.h>
#define trigPin 12
#define echoPin 13
int Buzzer = 8;
bool bJustBuzzed = false;
int lastBuzzed = 0;
int buzzTimeout = 30*1000;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1500);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(!bJustBuzzed) {
if ( (distance >= 200 || distance <= 100) ) {
Serial.println("no object detected");
digitalWrite(Buzzer, LOW);
}
else {
Serial.println("object detected");
tone(Buzzer, 1500); // play 400 Hz tone for 500 ms
delay(300);
noTone(Buzzer);
bJustBuzzed = true;
lastBuzzed = millis();
}
}
if(bJustBuzzed) {
if( (millis() - lastBuzzed) > buzzTimeout) {
bJustBuzzed = false;
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment