Files
hopper_traps/trailer_remote/trailer_remote.ino
Thaddeus Hughes 9bc81a9ebf init
2025-08-24 07:39:06 -05:00

104 lines
2.5 KiB
C++

#include <RH_ASK.h> // RadioHead library
#include <SPI.h> // Required for RadioHead, though not used directly
// Initialize the RF transmitter (default pin is 12, change if needed)
RH_ASK rf_driver(4000, 10, 10); // 2000 bps, DATA pin on 10
//#define REPEAT_INTERVAL 100
#define NUM_BUTTONS 5
#define DEBOUNCE_DELAY 100
const uint8_t BUTTON_PINS[NUM_BUTTONS] = {2, 3, 4, 5, 6};
bool lastButtonStates[NUM_BUTTONS] = {HIGH, HIGH, HIGH, HIGH, HIGH};
bool currentButtonStates[NUM_BUTTONS] = {HIGH, HIGH, HIGH, HIGH, HIGH};
bool buttonFallingEdges[NUM_BUTTONS] = {false, false, false, false, false};
uint32_t lastDebounceTimes[NUM_BUTTONS] = {0, 0, 0, 0, 0};
uint32_t lastRepeatTimes[NUM_BUTTONS] = {0, 0, 0, 0, 0};
void checkButtons() {
for (uint8_t i = 0; i < NUM_BUTTONS; i++) {
bool reading = digitalRead(BUTTON_PINS[i]);
if (reading != lastButtonStates[i]) {
lastDebounceTimes[i] = millis();
}
if ((millis() - lastDebounceTimes[i]) > DEBOUNCE_DELAY) {
if (reading != currentButtonStates[i]) {
currentButtonStates[i] = reading;
if (currentButtonStates[i] == LOW) {
buttonFallingEdges[i] = true;
}
}
}
lastButtonStates[i] = reading;
if (buttonFallingEdges[i]) {
buttonFallingEdges[i] = false;
lastRepeatTimes[i] = millis();
}
/*if (currentButtonStates[i] == LOW && (millis() - lastRepeatTimes[i]) >= REPEAT_INTERVAL) {
lastRepeatTimes[i] = millis();
onButtonPush(i);
}*/
}
}
void sendCommand(char cmd, int8_t ch) {
uint8_t msg[8];
msg[0] = 'h';
msg[1] = 'e';
msg[2] = 'c';
msg[3] = 'b';
msg[4] = cmd;
msg[5] = 48+ch;
msg[6] = msg[4]+msg[5];
msg[7] = '\0';
Serial.print("SEND: ");
Serial.print((char)msg[0]);
Serial.print((char)msg[1]);
Serial.println();
rf_driver.send((uint8_t *) msg, 7);
rf_driver.waitPacketSent();
}
void setup() {
//Serial.begin(115200);
if (!rf_driver.init()) {
//Serial.println("RF Transmitter initialization failed!");
}
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(2, INPUT);
}
void loop() {
checkButtons();
char cmd = currentButtonStates[0] ? 'J':'R';
//Serial.println(cmd);
if (!currentButtonStates[1])
sendCommand(cmd, 1);
if (!currentButtonStates[2])
sendCommand(cmd, 2);
if (!currentButtonStates[3])
sendCommand(cmd, 3);
if (!currentButtonStates[4])
sendCommand(cmd, 4);
//delay(20); // Wait 1 second before sending again
//Serial.println("loop");
}