Files
ROFLS_Arena/ROFLS_Arena_Camera_Controller/ROFLS_Arena_Camera_Controller.ino
T

117 lines
2.5 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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;
// ESPNOW 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("ESPNOW 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;
}
// Nonblocking 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;
}
}