-
Notifications
You must be signed in to change notification settings - Fork 7
/
3_sensor_sipaa.ino
57 lines (44 loc) · 1.11 KB
/
3_sensor_sipaa.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
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const int trigPin = 5;
const int echoPin = 4;
#define SOUND_SPEED 0.033
long duration;
float distanceCm;
const int collision=14;
void setup() {
Serial.begin(115200);
Serial.println("BEGIN!");
dht.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(collision,INPUT);
}
void loop(){
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("\n");
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * SOUND_SPEED/2;
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
int collisionData=digitalRead(collision);
if(collisionData==0){
Serial.println("no collision");
}
else if(collisionData==1){
Serial.println("collision");
}
delay(1000);
}