added functionality for a camera controller
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_now.h>
|
||||
|
||||
const int camPin = 16; // Any GPIO pin
|
||||
const int pressTime = 250; // 0.25s HIGH pulse
|
||||
const int gapTime = 250; // Gap between pulses for double press
|
||||
|
||||
void doSinglePress() {
|
||||
digitalWrite(camPin, HIGH);
|
||||
delay(pressTime);
|
||||
digitalWrite(camPin, LOW);
|
||||
}
|
||||
|
||||
void doDoublePress() {
|
||||
doSinglePress();
|
||||
delay(gapTime);
|
||||
doSinglePress();
|
||||
}
|
||||
|
||||
// Must match the sender struct exactly
|
||||
typedef struct struct_message_send {
|
||||
int boardID;
|
||||
bool buttonSTART;
|
||||
bool buttonSTARTforced;
|
||||
bool buttonPAUSE;
|
||||
bool buttonPIT;
|
||||
bool buttonPIThold;
|
||||
bool buttonRESET;
|
||||
bool buttonREDTEAM;
|
||||
bool buttonREDTEAMtapout;
|
||||
bool buttonBLUETEAM;
|
||||
bool buttonBLUETEAMtapout;
|
||||
} struct_message_send;
|
||||
|
||||
struct_message_send incoming;
|
||||
|
||||
// Recording state variable
|
||||
bool RECORDING = false;
|
||||
bool lastRECORDING = false; // for change detection
|
||||
|
||||
// LED blink timing
|
||||
unsigned long lastBlink = 0;
|
||||
bool ledState = false;
|
||||
|
||||
// ESP‑NOW receive callback
|
||||
void onDataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
|
||||
if (len != sizeof(incoming)) return;
|
||||
|
||||
memcpy(&incoming, data, sizeof(incoming));
|
||||
|
||||
// Only accept packets from boardID 0 (referee remote)
|
||||
if (incoming.boardID != 0) return;
|
||||
|
||||
// START or STARTforced → RECORDING = true
|
||||
if (incoming.buttonSTART || incoming.buttonSTARTforced) {
|
||||
RECORDING = true;
|
||||
}
|
||||
|
||||
// RESET → RECORDING = false
|
||||
if (incoming.buttonRESET) {
|
||||
RECORDING = false;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(camPin, OUTPUT);
|
||||
digitalWrite(camPin, LOW); // Idle state
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(200);
|
||||
|
||||
Serial.println("Extra Receiver Ready");
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setSleep(false);
|
||||
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
Serial.println("ESP‑NOW init failed!");
|
||||
return;
|
||||
}
|
||||
|
||||
esp_now_register_recv_cb(onDataRecv);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Detect changes in RECORDING state
|
||||
if (RECORDING != lastRECORDING) {
|
||||
|
||||
// Debug print
|
||||
Serial.print("RECORDING state changed: ");
|
||||
Serial.println(RECORDING ? "TRUE" : "FALSE");
|
||||
|
||||
// Trigger camera once on state change
|
||||
doSinglePress();
|
||||
|
||||
lastRECORDING = RECORDING;
|
||||
}
|
||||
|
||||
// Non‑blocking LED blink when RECORDING = true
|
||||
if (RECORDING) {
|
||||
unsigned long now = millis();
|
||||
if (now - lastBlink >= 1000) { // blink every 1s
|
||||
lastBlink = now;
|
||||
ledState = !ledState;
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
}
|
||||
} else {
|
||||
// Ensure LED is off when not recording
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
ledState = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user