-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino.ino
More file actions
67 lines (56 loc) · 1.72 KB
/
arduino.ino
File metadata and controls
67 lines (56 loc) · 1.72 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const int IR_SENSOR_PIN = 23; // Connect IR Out to GPIO 23
const int LED_PIN = 2; // Built-in LED
const int BUZZER_PIN = 18; // Connect IR Out to GPIO 18
int counter = 0;
bool objectDetected = false;
void setup() {
// Higher baud rate for responsive web communication
Serial.begin(115200);
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Flash LED and Beep to signal start
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
int sensorState = digitalRead(IR_SENSOR_PIN);
// IR sensors usually go LOW when an object is detected
if (sensorState == LOW && !objectDetected) {
counter++;
objectDetected = true;
// Flash built-in LED
digitalWrite(LED_PIN, HIGH);
// Beep
digitalWrite(BUZZER_PIN, HIGH);
// Send data to the HTML dashboard in a simple format
Serial.print("COUNT:");
Serial.println(counter);
// Small debounce delay
delay(100);
}
// Reset detection state when object moves away (sensor goes HIGH)
if (sensorState == HIGH && objectDetected) {
objectDetected = false;
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("STATUS:IDLE");
}
// Check for commands FROM the dashboard (e.g., Reset)
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
command.trim();
if (command == "RESET") {
counter = 0;
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("COUNT:0");
}
}
}