-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD_distance_ultrasonic_mv.ino
74 lines (64 loc) · 1.27 KB
/
LCD_distance_ultrasonic_mv.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <LiquidCrystal.h>
//lcd(RS, Enable, D4, D5, D6, D7);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int pinTrigger = 8;
int pinEcho = 9;
int buzzerPin = 10;
long Zeit;
int Abstand;
boolean AlarmOn;
void setup(){
AlarmOn = false;
lcd.begin(16,2);
lcd.clear();
pinMode(pinTrigger, OUTPUT);
pinMode(pinEcho, INPUT);
pinMode(buzzerPin,OUTPUT);
analogWrite(buzzerPin,255);
Serial.begin(9600);
}
void loop(){
analogWrite(buzzerPin, 255);
// get distance
digitalWrite(pinTrigger,LOW);
delay(5);
digitalWrite(pinTrigger,HIGH);
delay(10);
digitalWrite(pinTrigger,LOW);
Zeit = pulseIn(pinEcho,HIGH); // Zeit hin und zurück
Zeit = Zeit/2;
// Abstand = Zeit * Geschwindigkeit
Abstand = int((Zeit*0.0343)); // Schallgeschwindigkeit @20°C
//Serial.print("Distance (cm): ");
//Serial.println(distance);
// show distance on LCD
showDistance(Abstand);
if (Abstand < 16)
{
AlarmOn = true;
}
else
AlarmOn = false;
if(AlarmOn)
makeSound(50);
delay(1000);
}
void makeSound(int freqNum)
{
for(int i; i< 20; i++)
{
analogWrite(buzzerPin, 0);
delay(freqNum);
analogWrite(buzzerPin, 255);
delay(freqNum);
}
}
void showDistance( int dist)
{
lcd.clear();
lcd.print("* Abstand: *");
lcd.setCursor(6,1);
lcd.print(dist);
lcd.setCursor(9,1);
lcd.print("cm");
}