Created
December 7, 2025 20:45
-
-
Save a0a7/7775e2b7291b446277b32a912c1e724f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Particle.h" | |
| // Sends messages over USB Serial: e.g., "TRIGGER_DOWN", "C3_UP", "W_DOWN" | |
| SYSTEM_MODE(AUTOMATIC); | |
| // Pin layout (adjust if needed) | |
| // Movement: W A S D on D2-D5 | |
| const pin_t MOVE_PINS[4] = { D2, D3, D4, D5 }; | |
| const char* MOVE_NAMES[4] = { "W", "A", "S", "D" }; | |
| // Trigger on D6 | |
| const pin_t TRIGGER_PIN = D6; | |
| const char* TRIGGER_NAME = "TRIGGER"; | |
| // Chambers C1–C6 on D7, D8, D9, A0, A1, A2 | |
| const pin_t CHAMBER_PINS[6] = { D7, D8, D9, A0, A1, A2 }; | |
| const char* CHAMBER_NAMES[6] = { "C1", "C2", "C3", "C4", "C5", "C6" }; | |
| // Debounce guard time (ms) | |
| const uint32_t EDGE_GUARD_MS = 15; | |
| // State tracking | |
| struct Btn { | |
| pin_t pin; | |
| const char* name; | |
| int prevReading; // LOW/HIGH | |
| bool isPressed; // logical pressed state | |
| uint32_t lastEdgeMs; // last edge timestamp | |
| }; | |
| Btn buttons[4 + 1 + 6]; // Move + Trigger + Chambers | |
| size_t BTN_COUNT = sizeof(buttons) / sizeof(buttons[0]); | |
| void setup() { | |
| // Populate button array | |
| size_t idx = 0; | |
| for (int i = 0; i < 4; i++, idx++) { | |
| buttons[idx] = { MOVE_PINS[i], MOVE_NAMES[i], LOW, false, 0 }; | |
| } | |
| buttons[idx++] = { TRIGGER_PIN, TRIGGER_NAME, LOW, false, 0 }; | |
| for (int i = 0; i < 6; i++, idx++) { | |
| buttons[idx] = { CHAMBER_PINS[i], CHAMBER_NAMES[i], LOW, false, 0 }; | |
| } | |
| // Configure pins | |
| for (size_t i = 0; i < BTN_COUNT; i++) { | |
| pinMode(buttons[i].pin, INPUT_PULLDOWN); // pressed = HIGH | |
| } | |
| Serial.begin(115200); | |
| waitFor(Serial.isConnected, 3000); | |
| } | |
| inline void report(const char* name, bool pressed) { | |
| if (!Serial.isConnected()) return; | |
| Serial.printlnf("%s_%s", name, pressed ? "DOWN" : "UP"); | |
| } | |
| void loop() { | |
| uint32_t now = millis(); | |
| for (size_t i = 0; i < BTN_COUNT; i++) { | |
| auto& b = buttons[i]; | |
| int cur = digitalRead(b.pin); | |
| bool allowEdge = (now - b.lastEdgeMs) >= EDGE_GUARD_MS; | |
| // Rising: LOW -> HIGH | |
| if (allowEdge && cur == HIGH && b.prevReading == LOW) { | |
| b.lastEdgeMs = now; | |
| if (!b.isPressed) { | |
| b.isPressed = true; | |
| report(b.name, true); // DOWN | |
| } | |
| } | |
| // Falling: HIGH -> LOW | |
| if (allowEdge && cur == LOW && b.prevReading == HIGH) { | |
| b.lastEdgeMs = now; | |
| if (b.isPressed) { | |
| b.isPressed = false; | |
| report(b.name, false); // UP | |
| } | |
| } | |
| b.prevReading = cur; | |
| } | |
| // delay(5); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment