-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWR75.ino
More file actions
257 lines (223 loc) · 6.78 KB
/
WR75.ino
File metadata and controls
257 lines (223 loc) · 6.78 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
// display Display Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define display_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, display_RESET);
// Pin Definitions
#define TRIGGER_PIN 27
#define MAG_PIN 26
#define MODE_PIN 33
#define SHOT_PIN 25
#define BATTERY_PIN 14
#define LED_PIN 32
// NeoPixel Configuration
#define LED_COUNT 8
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Button Debouncing Variables
unsigned long lastDebounceTimeTrigger = 0;
unsigned long lastDebounceTimeMag = 0;
unsigned long lastDebounceTimeMode = 0;
const unsigned long debounceDelay = 50;
int lastTriggerState = HIGH;
int lastMagState = HIGH;
int lastModeState = HIGH;
// System State Variables
volatile bool shotPulseActive = false;
unsigned long shotStartTime = 0;
const unsigned long pulseWidth = 100; // Shot pulse width in ms
int shotCount = 0;
int currentMode = 1; // 1=Single, 2=Burst, 3=Auto
bool magPresent = false;
bool magWasRemoved = false;
int currentLED = 0;
unsigned long lastBatteryRead = 0;
const unsigned long batteryReadInterval = 5000; // Read battery every 5 sec
void setup() {
// Initialize pins
pinMode(TRIGGER_PIN, INPUT_PULLUP);
pinMode(MAG_PIN, INPUT_PULLUP);
pinMode(MODE_PIN, INPUT_PULLUP);
pinMode(SHOT_PIN, OUTPUT);
pinMode(BATTERY_PIN, INPUT);
digitalWrite(SHOT_PIN, LOW);
// Initialize display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (true); // Halt if display fails
}
display.clearDisplay();
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.fillRoundRect(31, 16, 60, 22, 3, 1);
display.drawRoundRect(32, 17, 58, 20, 3, 0);
display.setTextColor(BLACK);
display.setCursor(39, 20); // set position to display
display.println("WR75"); // set text
display.setTextColor(WHITE);
display.setCursor(38, 19); // set position to display
display.println("WR75"); // set text
// Initialize NeoPixel strip
strip.begin();
strip.show(); // Turn off all LEDs
// Initial display update
updateDisplay();
}
void loop() {
unsigned long currentMillis = millis();
// Debounce and handle trigger button
int triggerReading = digitalRead(TRIGGER_PIN);
if (triggerReading != lastTriggerState) {
lastDebounceTimeTrigger = currentMillis;
}
if ((currentMillis - lastDebounceTimeTrigger) > debounceDelay) {
if (triggerReading == LOW) { // Button pressed (active low)
handleTrigger();
}
}
lastTriggerState = triggerReading;
// Debounce and handle mag button
int magReading = digitalRead(MAG_PIN);
if (magReading != lastMagState) {
lastDebounceTimeMag = currentMillis;
}
if ((currentMillis - lastDebounceTimeMag) > debounceDelay) {
magPresent = (magReading == LOW); // Update mag presence
handleMagState();
}
lastMagState = magReading;
// Debounce and handle mode button
int modeReading = digitalRead(MODE_PIN);
if (modeReading != lastModeState) {
lastDebounceTimeMode = currentMillis;
}
if ((currentMillis - lastDebounceTimeMode) > debounceDelay) {
if (modeReading == LOW) { // Button pressed (active low)
delay(1000);
handleModeChange();
}
}
lastModeState = modeReading;
// Manage shot pulse timing
if (shotPulseActive && (currentMillis - shotStartTime >= pulseWidth)) {
digitalWrite(SHOT_PIN, LOW);
shotPulseActive = false;
}
// Handle full auto mode continuous firing
if (currentMode == 3 && digitalRead(TRIGGER_PIN) == LOW) {
fireShot();
}
// Read battery periodically
if (currentMillis - lastBatteryRead >= batteryReadInterval) {
lastBatteryRead = currentMillis;
updateDisplay();
}
}
void handleTrigger() {
switch (currentMode) {
case 1: // Single shot
fireShot();
delay(500);
break;
case 2: // Burst mode (5 shots)
for (int i = 0; i < 5; i++) {
fireShot();
delay(150); // Delay between shots in burst
}
break;
}
}
void fireShot() {
if (!shotPulseActive) {
digitalWrite(SHOT_PIN, HIGH);
shotPulseActive = true;
shotStartTime = millis();
shotCount++;
// updateLEDs();
runLightSequence();
updateDisplay();
}
}
void handleModeChange() {
currentMode = (currentMode % 3) + 1; // Cycle through 1,2,3
updateDisplay();
}
void handleMagState() {
if (!magPresent) {
magWasRemoved = true;
} else if (magWasRemoved) {
ESP.restart(); // Restart when mag is reinserted after removal
}
updateDisplay();
}
//void updateLEDs() {
// strip.clear(); // Turn off all LEDs
//
// Calculate color gradient (yellow to red)
// int r = map(currentLED, 0, LED_COUNT-1, 255, 255);
// int g = map(currentLED, 0, LED_COUNT-1, 255, 0);
// int b = 0;
//
// strip.setPixelColor(currentLED, strip.Color(r, g, b));
// strip.show();
// Move to next LED (wrap around)
// currentLED = (currentLED + 1) % LED_COUNT;
//}
void runLightSequence() {
for(int i = 0; i < LED_COUNT; i++) {
// Calculate color gradient (yellow to red)
int greenValue = map(i, 0, LED_COUNT-1, 150, 0);
uint32_t color = strip.Color(255, greenValue, 0);
// Light current LED
strip.setPixelColor(i, color);
strip.show();
delay(70); // LED on time
// Turn off current LED
strip.setPixelColor(i, 0);
strip.show();
delay(30); // Pause before next LED
}
}
float readBatteryPercentage() {
int raw = analogRead(BATTERY_PIN);
// Convert to voltage (assuming voltage divider 100k/100k)
float voltage = (raw / 4095.0) * 3.3 * 2;
// Map voltage to percentage (adjust for your battery)
voltage = constrain(voltage, 3.2, 4.2); // LiPo range
return map(voltage * 100, 320, 420, 0, 100);
}
void updateDisplay() {
// Dynamic content area (below divider line)
display.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-48, SSD1306_BLACK);
display.fillRect(0, 39, SCREEN_WIDTH, SCREEN_HEIGHT-39, SSD1306_BLACK);
display.setTextSize(1);
display.setCursor(0, 32);
// Display current mode
display.setCursor(5, 40);
display.print("Mode: ");
switch (currentMode) {
case 1: display.println("Single"); break;
case 2: display.println("Burst"); break;
case 3: display.println("Full Auto"); break;
}
// Display shot count
// display.drawRect(0, 0, 128, 16, SSD1306_BLACK);
display.setTextSize(2);
display.setCursor(12, 0);
display.print("Shots:");
display.println(shotCount);
display.setTextSize(1);
// Display battery percentage
display.setCursor(5, 48);
display.print("Battery: ");
display.print(readBatteryPercentage());
display.println("%");
// Draw mag indicator
display.drawCircle(114, 50, 7, SSD1306_WHITE);
if (magPresent) {
display.setCursor(112, 47);
display.print("M");
}
display.display();
}