-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_code.cpp
More file actions
49 lines (42 loc) · 1.11 KB
/
project_code.cpp
File metadata and controls
49 lines (42 loc) · 1.11 KB
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
// initializing pins
const int trigPin = 13;
const int buzzerPin = 8;
const int echoPin = 12;
const int waterSensorPin = 7;
const int mosfetGatePin = 3;
const int motorOnTime = 2000;
// initalize variables
int distanceCm, waterState;
unsigned long time;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(waterSensorPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(mosfetGatePin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time = pulseIn(echoPin, HIGH);
// calculate distance
distanceCm = time * 0.034 / 2;
// Water detection sensor code
waterState = digitalRead(waterSensorPin);
if (waterState == HIGH && digitalRead(buzzerPin) == HIGH) {
digitalWrite(mosfetGatePin, HIGH);
delay(motorOnTime);
digitalWrite(mosfetGatePin, LOW);
}
// Ultrasonic sensor code
if (distanceCm < 25) {
digitalWrite(buzzerPin, HIGH); // Buzzer ON
} else {
digitalWrite(5, LOW); // Buzzer OFF
}
}