Skip to content

Instantly share code, notes, and snippets.

@a0a7
Created December 9, 2025 19:45
Show Gist options
  • Select an option

  • Save a0a7/36ed8599ee996948662a493d9a41ed4f to your computer and use it in GitHub Desktop.

Select an option

Save a0a7/36ed8599ee996948662a493d9a41ed4f to your computer and use it in GitHub Desktop.
#include "Particle.h"
SYSTEM_MODE(AUTOMATIC);
// chambers on D0-D5
const pin_t CHAMBER_PINS[6] = { D0, D1, D2, D3, D4, D5 };
const char* CHAMBER_NAMES[6] = { "C1", "C2", "C3", "C4", "C5", "C6" };
const pin_t TRIGGER_PIN = D6;
const char* TRIGGER_NAME = "TRIGGER";
// joystick on A0/A1
const pin_t JOYSTICK_X_PIN = A0;
const pin_t JOYSTICK_Y_PIN = A1;
const uint32_t JOYSTICK_REPORT_MS = 50;
uint32_t lastJoystickReport = 0;
const uint32_t EDGE_GUARD_MS = 15; // debounce time
struct Btn {
pin_t pin;
const char* name;
int prevReading;
bool isPressed;
uint32_t lastEdgeMs;
};
Btn buttons[6];
Btn triggerBtn;
size_t BTN_COUNT = sizeof(buttons) / sizeof(buttons[0]);
void setup() {
for (int i = 0; i < 6; i++) {
buttons[i] = { CHAMBER_PINS[i], CHAMBER_NAMES[i], LOW, false, 0 };
}
triggerBtn = { TRIGGER_PIN, TRIGGER_NAME, LOW, false, 0 };
for (size_t i = 0; i < BTN_COUNT; i++) {
pinMode(buttons[i].pin, INPUT_PULLDOWN);
}
pinMode(TRIGGER_PIN, INPUT_PULLDOWN);
pinMode(JOYSTICK_X_PIN, INPUT);
pinMode(JOYSTICK_Y_PIN, INPUT);
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");
}
inline void reportJoystick(int xVal, int yVal) {
if (!Serial.isConnected()) return;
Serial.printlnf("JOYSTICK_X:%d,Y:%d", xVal, yVal);
}
void loop() {
uint32_t now = millis();
// joystick every 50ms
if (now - lastJoystickReport >= JOYSTICK_REPORT_MS) {
int xValue = analogRead(JOYSTICK_X_PIN);
int yValue = analogRead(JOYSTICK_Y_PIN);
reportJoystick(xValue, yValue);
lastJoystickReport = now;
}
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;
if (allowEdge && cur == HIGH && b.prevReading == LOW) {
b.lastEdgeMs = now;
if (!b.isPressed) {
b.isPressed = true;
report(b.name, true);
}
}
if (allowEdge && cur == LOW && b.prevReading == HIGH) {
b.lastEdgeMs = now;
if (b.isPressed) {
b.isPressed = false;
report(b.name, false);
}
}
b.prevReading = cur;
}
// trigger button
{
auto& b = triggerBtn;
int cur = digitalRead(b.pin);
bool allowEdge = (now - b.lastEdgeMs) >= EDGE_GUARD_MS;
if (allowEdge && cur == HIGH && b.prevReading == LOW) {
b.lastEdgeMs = now;
if (!b.isPressed) {
b.isPressed = true;
report(b.name, true);
}
}
if (allowEdge && cur == LOW && b.prevReading == HIGH) {
b.lastEdgeMs = now;
if (b.isPressed) {
b.isPressed = false;
report(b.name, false);
}
}
b.prevReading = cur;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment