-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1_arduino.ino
More file actions
138 lines (112 loc) · 3.45 KB
/
Copy pathtask1_arduino.ino
File metadata and controls
138 lines (112 loc) · 3.45 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Hand Gesture Detector - Two Sensors with Bluetooth HC-05
// Move RIGHT (left→right) = GREEN LED
// Move LEFT (right→left) = RED LED
// Stationary/Center = WHITE LED
#include <SoftwareSerial.h>
// Bluetooth HC-05 pins
#define BT_RX 2 // Connect to HC-05 TX
#define BT_TX 3 // Connect to HC-05 RX
SoftwareSerial bluetooth(BT_RX, BT_TX);
// Pin definitions
#define TRIG_L 4
#define ECHO_L 5
#define TRIG_R 11
#define ECHO_R 13
#define LAMP_RED 8
#define LAMP_WHITE 9
#define LAMP_GREEN 10
// Detection threshold (cm)
const int THRESHOLD = 30;
// State tracking
bool wasLeftActive = false;
bool wasRightActive = false;
String currentGesture = "NONE";
unsigned long gestureStartTime = 0;
const int GESTURE_HOLD_TIME = 500; // 500ms hold
void setup() {
pinMode(TRIG_L, OUTPUT);
pinMode(ECHO_L, INPUT);
pinMode(TRIG_R, OUTPUT);
pinMode(ECHO_R, INPUT);
pinMode(LAMP_RED, OUTPUT);
pinMode(LAMP_WHITE, OUTPUT);
pinMode(LAMP_GREEN, OUTPUT);
Serial.begin(9600);
bluetooth.begin(9600);
Serial.println("=== Two-Sensor Gesture Detector with Bluetooth ===");
Serial.println("Move hand LEFT→RIGHT for GREEN");
Serial.println("Move hand RIGHT→LEFT for RED");
Serial.println("Stationary = WHITE");
Serial.println("Bluetooth HC-05: RX=2, TX=3");
// Start with all LEDs off
digitalWrite(LAMP_RED, LOW);
digitalWrite(LAMP_WHITE, LOW);
digitalWrite(LAMP_GREEN, LOW);
}
void loop() {
int leftDist = getDistance(TRIG_L, ECHO_L);
int rightDist = getDistance(TRIG_R, ECHO_R);
bool leftActive = (leftDist > 0 && leftDist < THRESHOLD);
bool rightActive = (rightDist > 0 && rightDist < THRESHOLD);
// Detect RIGHT gesture
if (leftActive && !wasLeftActive && !rightActive) {
// Left sensor just activated
}
if (leftActive && rightActive && wasLeftActive && !wasRightActive) {
setGesture("RIGHT");
gestureStartTime = millis();
}
// Detect LEFT gesture
if (rightActive && !wasRightActive && !leftActive) {
// Right sensor just activated
}
if (rightActive && leftActive && wasRightActive && !wasLeftActive) {
setGesture("LEFT");
gestureStartTime = millis();
}
// Detect CENTER / stationary
if (leftActive && rightActive && millis() - gestureStartTime > GESTURE_HOLD_TIME) {
setGesture("CENTER");
}
// No hand detected
if (!leftActive && !rightActive && currentGesture != "NONE") {
currentGesture = "NONE";
turnOffAllLEDs();
bluetooth.println("NONE");
Serial.println("⚫ NO HAND [BT: NONE]");
}
wasLeftActive = leftActive;
wasRightActive = rightActive;
delay(50);
}
int getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
int distance = duration * 0.034 / 2;
return distance;
}
void setGesture(String gesture) {
if (gesture == currentGesture) return;
currentGesture = gesture;
turnOffAllLEDs();
bluetooth.println(gesture);
if (gesture == "LEFT") {
digitalWrite(LAMP_RED, HIGH);
Serial.println("🔴 LEFT MOTION [BT: LEFT]");
} else if (gesture == "RIGHT") {
digitalWrite(LAMP_GREEN, HIGH);
Serial.println("🟢 RIGHT MOTION [BT: RIGHT]");
} else if (gesture == "CENTER") {
digitalWrite(LAMP_WHITE, HIGH);
Serial.println("⚪ CENTER [BT: CENTER]");
}
}
void turnOffAllLEDs() {
digitalWrite(LAMP_RED, LOW);
digitalWrite(LAMP_WHITE, LOW);
digitalWrite(LAMP_GREEN, LOW);
}