Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Sonar/Sonar.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#define trigPin 12
#define echoPin 13
#define beepPin 10
#define scalingfactor 50

int inches = 0;
unsigned long duration = 0;
unsigned long maxtime = 1000;
unsigned long i = 1;
unsigned long time2 = 0;
unsigned long time1 = 0;

void setup(){
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(beepPin, OUTPUT);
}

void logdistance(){
time1 = millis();
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = duration/148;
time2 = millis();
maxtime = scalingfactor*inches;
}

void delayandcheckdistance(){
i=1;
while(1){
if (i % 60 == 0){
logdistance();
}
delay(1);
++i;
if (i >= maxtime){
break;
}
}
}

void beep(){
digitalWrite(beepPin, HIGH);
delay(90);
logdistance();
if(time2-time1 < 30){
delay(60-(time2-time1));
}
digitalWrite(beepPin, LOW);
}

void loop(){
beep();
delayandcheckdistance();
}

1 change: 1 addition & 0 deletions Sonar/liink.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://clone.tinkernut.com/portfolio/make-sonar-device/#lightbox[9247]/0/