Created
October 7, 2015 09:44
-
-
Save anonymous/02d9511dda8c4a2a7227 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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