-
Notifications
You must be signed in to change notification settings - Fork 0
/
CM1.ino
41 lines (36 loc) · 809 Bytes
/
CM1.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
#define TRIGPIN 2
#define ECHOPIN 3
#define LED1 12
#define LED2 13
const int speaker=2;
long ping() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
return pulseIn(ECHOPIN, HIGH)/58;
}
void setup() {
Serial.begin(9600);
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
long cm = ping();
Serial.println(cm);
if (cm <= 100) {
digitalWrite(LED1, HIGH);
delay(cm*1.5 + 10);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(cm*1.5 + 10);
digitalWrite(LED2, LOW);
tone(2,500);
noTone(2);
delay(1000);
}
delay(100);
}