summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQrius <[email protected]>2025-07-05 12:08:54 +0200
committerQrius <[email protected]>2025-07-05 12:08:54 +0200
commita20776e5f89422b63ef6e2e6d7641acc001cc5f5 (patch)
treefbb6d789007ce2c991b99d9cfd96aa033cc33b0c
parentc3e9693cf977d4451a39c4462307b1aeda50f806 (diff)
downloadcar_aux_buttons-a20776e5f89422b63ef6e2e6d7641acc001cc5f5.tar.gz
car_aux_buttons-a20776e5f89422b63ef6e2e6d7641acc001cc5f5.zip
Make high beam function more sensibly, hold to manually turn onHEADmaster
-rw-r--r--car_aux_buttons.ino73
1 files changed, 46 insertions, 27 deletions
diff --git a/car_aux_buttons.ino b/car_aux_buttons.ino
index 877a188..09eaaa0 100644
--- a/car_aux_buttons.ino
+++ b/car_aux_buttons.ino
@@ -18,12 +18,17 @@
#define DEBOUNCE_DELAY_BUTTON 50L
#define DEBOUNCE_DELAY_SIGNAL 500L
+#define DEBOUNCE_DELAY_SIGNAL_HB 50L
#define LONG_PRESS_DURATION 1000L
#define PSENSOR_AUTO_ENABLE_ADDRESS 2
#define RLIGHTS_AUTO_ENABLE_ADDRESS 3
#define FLIGHTS_AUTO_ENABLE_ADDRESS 4
+#define B_LOW 0
+#define B_SHORT 1
+#define B_LONG 2
+
enum State {
OFF_AUTO,
ON_AUTO,
@@ -44,6 +49,8 @@ void fsm_tick_timer(
enum State* state,
unsigned int sense,
unsigned int button,
+ unsigned int TriggerManual,
+ unsigned int TriggerAuto,
unsigned long* timer,
unsigned long timeout,
unsigned int autoEnableAddress
@@ -56,6 +63,20 @@ void fsm_tick_timer(
if ((millis() - (*timer)) > timeout)
timer_expired = 1;
+ if (*state != ON_MANUAL && button == TriggerAuto) {
+ if (autoEnable) {
+ EEPROM.write(autoEnableAddress, 0);
+ autoEnable = 0;
+ tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 80);
+ delay(150);
+ tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 80);
+ } else {
+ EEPROM.write(autoEnableAddress, 1);
+ autoEnable = 1;
+ tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 300);
+ }
+ }
+
switch (*state) {
case OFF_AUTO:
digitalWrite(outputPin, LOW);
@@ -65,7 +86,7 @@ void fsm_tick_timer(
break;
}
- if (button) {
+ if (button == TriggerManual) {
*state = ON_MANUAL;
break;
}
@@ -74,7 +95,7 @@ void fsm_tick_timer(
case ON_AUTO:
digitalWrite(outputPin, HIGH);
- if (button) {
+ if (button == TriggerManual) {
*state = OFF_MANUAL;
*timer = millis();
break;
@@ -93,7 +114,13 @@ void fsm_tick_timer(
case ON_MANUAL:
digitalWrite(outputPin, HIGH);
- if (button) {
+ if (button == TriggerManual) {
+ *state = OFF_AUTO;
+ break;
+ }
+ if (TriggerManual == B_LONG && button == B_SHORT) {
+ // In this case, we want to override the input options,
+ // As we don't want to require long presses to disable outputs
*state = OFF_AUTO;
break;
}
@@ -107,7 +134,7 @@ void fsm_tick_timer(
break;
}
- if (button) {
+ if (button == TriggerManual) {
*state = ON_AUTO;
break;
}
@@ -127,7 +154,7 @@ unsigned int debouncelp(
) {
int reading = digitalRead(pin);
- unsigned int btnPressed = 0;
+ unsigned int btnPressed = B_LOW;
if (reading != (*lastState)) {
(*lastDebounceTime) = millis();
@@ -145,7 +172,7 @@ unsigned int debouncelp(
if (!(*longPressTriggered)) {
unsigned long pressDuration = millis() - (*buttonPressTime);
if (pressDuration < LONG_PRESS_DURATION) {
- btnPressed = 1;
+ btnPressed = B_SHORT;
}
}
}
@@ -154,16 +181,7 @@ unsigned int debouncelp(
if ((*state) == LOW && !(*longPressTriggered)) {
if ((millis() - (*buttonPressTime)) >= LONG_PRESS_DURATION) {
*longPressTriggered = true;
- unsigned int autoEnable = EEPROM.read(autoEnableAddress);
- if (autoEnable) {
- EEPROM.write(autoEnableAddress, 0);
- tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 80);
- delay(150);
- tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 80);
- } else {
- EEPROM.write(autoEnableAddress, 1);
- tone(PIN_OUTPUT_SPEAKER, BEEP_FREQ, 300);
- }
+ btnPressed = B_LONG;
}
}
}
@@ -174,7 +192,8 @@ unsigned int debouncesig(
unsigned int pin,
bool* state,
bool* lastState,
- unsigned long* lastDebounceTime
+ unsigned long* lastDebounceTime,
+ unsigned long debounce_delay
) {
int reading = digitalRead(pin);
if (reading != (*lastState)) {
@@ -185,7 +204,7 @@ unsigned int debouncesig(
if (reading != (*lastState))
(*lastDebounceTime) = millis();
- if ((millis() - (*lastDebounceTime)) > DEBOUNCE_DELAY_SIGNAL)
+ if ((millis() - (*lastDebounceTime)) > debounce_delay)
if (reading != *state)
*state = reading;
@@ -208,11 +227,11 @@ void setup() {
pinMode(PIN_OUTPUT_FLIGHTS, OUTPUT);
}
-#define DEBOUNCE_SIGNAL(Name, Pin) \
+#define DEBOUNCE_SIGNAL(Name, Pin, Delay) \
static bool Name##State = LOW; \
static bool Name##LastState = LOW; \
static unsigned long Name##LastDebounceTime = 0; \
- unsigned int Name##Enabled = debouncesig(Pin, &Name##State, &Name##LastState, &Name##LastDebounceTime);
+ unsigned int Name##Enabled = debouncesig(Pin, &Name##State, &Name##LastState, &Name##LastDebounceTime, Delay);
#define DEBOUNCE_BUTTON(Name, Pin, AutoEnable) \
static bool Name##State = HIGH; \
static bool Name##LastState = HIGH; \
@@ -220,15 +239,15 @@ void setup() {
static unsigned long Name##ButtonPressTime = 0; \
static bool Name##LongPressTriggered = false; \
unsigned int Name##Pressed = debouncelp(Pin, &Name##State, &Name##LastState, &Name##LastDebounceTime, &Name##ButtonPressTime, &Name##LongPressTriggered, AutoEnable);
-#define FSM(LName, UName, Signal, Timeout) \
+#define FSM(LName, UName, Signal, TriggerAuto, TriggerManual, Timeout) \
DEBOUNCE_BUTTON(LName, PIN_INPUT_BTN_##UName, UName##_AUTO_ENABLE_ADDRESS) \
- fsm_tick_timer(PIN_OUTPUT_##UName, &STATE_##UName, Signal, LName##Pressed, &UName##_TIMEOUT_TIMER, Timeout, UName##_AUTO_ENABLE_ADDRESS);
+ fsm_tick_timer(PIN_OUTPUT_##UName, &STATE_##UName, Signal, LName##Pressed, TriggerManual, TriggerAuto, &UName##_TIMEOUT_TIMER, Timeout, UName##_AUTO_ENABLE_ADDRESS);
void loop() {
- DEBOUNCE_SIGNAL(reverseSignal, PIN_INPUT_REVERSE)
- DEBOUNCE_SIGNAL(highBeamSignal, PIN_INPUT_HIGH_BEAM)
+ DEBOUNCE_SIGNAL(reverseSignal, PIN_INPUT_REVERSE, DEBOUNCE_DELAY_SIGNAL)
+ DEBOUNCE_SIGNAL(highBeamSignal, PIN_INPUT_HIGH_BEAM, DEBOUNCE_DELAY_SIGNAL_HB)
- FSM(psensor, PSENSOR, reverseSignalEnabled, TIMEOUT_AUTO_OFF)
- FSM(rlights, RLIGHTS, reverseSignalEnabled, TIMEOUT_AUTO_OFF)
- FSM(Flights, FLIGHTS, highBeamSignalEnabled, TIMEOUT_NONE)
+ FSM(psensor, PSENSOR, reverseSignalEnabled, B_LONG, B_SHORT, TIMEOUT_AUTO_OFF)
+ FSM(rlights, RLIGHTS, reverseSignalEnabled, B_LONG, B_SHORT, TIMEOUT_AUTO_OFF)
+ FSM(Flights, FLIGHTS, highBeamSignalEnabled, B_SHORT, B_LONG, TIMEOUT_NONE)
}