diff options
Diffstat (limited to 'car_aux_buttons.ino')
-rw-r--r-- | car_aux_buttons.ino | 59 |
1 files changed, 46 insertions, 13 deletions
diff --git a/car_aux_buttons.ino b/car_aux_buttons.ino index 407686d..877a188 100644 --- a/car_aux_buttons.ino +++ b/car_aux_buttons.ino @@ -11,12 +11,13 @@ #define PIN_INPUT_BTN_RLIGHTS 3 #define PIN_INPUT_BTN_FLIGHTS 4 -#define TIMEOUT_AUTO_OFF 15000 +#define TIMEOUT_AUTO_OFF 3000 #define TIMEOUT_NONE 0 #define BEEP_FREQ 1000 -#define DEBOUNCE_DELAY 50L +#define DEBOUNCE_DELAY_BUTTON 50L +#define DEBOUNCE_DELAY_SIGNAL 500L #define LONG_PRESS_DURATION 1000L #define PSENSOR_AUTO_ENABLE_ADDRESS 2 @@ -48,7 +49,7 @@ void fsm_tick_timer( unsigned int autoEnableAddress ) { unsigned int autoEnable = EEPROM.read(autoEnableAddress); - if ((digitalRead(sense) == HIGH) && autoEnable) + if (sense && autoEnable) *timer = millis(); unsigned int timer_expired = 0; @@ -59,7 +60,7 @@ void fsm_tick_timer( case OFF_AUTO: digitalWrite(outputPin, LOW); - if ((digitalRead(sense) == HIGH) && autoEnable) { + if (sense && autoEnable) { *state = ON_AUTO; break; } @@ -79,7 +80,7 @@ void fsm_tick_timer( break; } - if ((digitalRead(sense) == HIGH) && autoEnable) { + if (sense && autoEnable) { break; } @@ -101,8 +102,7 @@ void fsm_tick_timer( case OFF_MANUAL: digitalWrite(outputPin, LOW); - if (digitalRead(sense) == LOW) { - // One improvement here is to wait until the timer expires before going to auto + if (!sense && timer_expired) { *state = OFF_AUTO; break; } @@ -134,7 +134,7 @@ unsigned int debouncelp( (*lastState) = reading; } - if ((millis() - (*lastDebounceTime)) > DEBOUNCE_DELAY) { + if ((millis() - (*lastDebounceTime)) > DEBOUNCE_DELAY_BUTTON) { if (reading != (*state)) { (*state) = reading; @@ -170,6 +170,32 @@ unsigned int debouncelp( return btnPressed; } +unsigned int debouncesig( + unsigned int pin, + bool* state, + bool* lastState, + unsigned long* lastDebounceTime +) { + int reading = digitalRead(pin); + if (reading != (*lastState)) { + (*lastDebounceTime) = millis(); + (*lastState) = reading; + } + + if (reading != (*lastState)) + (*lastDebounceTime) = millis(); + + if ((millis() - (*lastDebounceTime)) > DEBOUNCE_DELAY_SIGNAL) + if (reading != *state) + *state = reading; + + *lastState = reading; + + if ((*state) == HIGH) + return 1; + return 0; +} + void setup() { pinMode(PIN_INPUT_REVERSE, INPUT); pinMode(PIN_INPUT_HIGH_BEAM, INPUT); @@ -180,10 +206,13 @@ void setup() { pinMode(PIN_OUTPUT_PSENSOR, OUTPUT); pinMode(PIN_OUTPUT_RLIGHTS, OUTPUT); pinMode(PIN_OUTPUT_FLIGHTS, OUTPUT); - //Serial.begin(9600); } -void loop() { +#define DEBOUNCE_SIGNAL(Name, Pin) \ + 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); #define DEBOUNCE_BUTTON(Name, Pin, AutoEnable) \ static bool Name##State = HIGH; \ static bool Name##LastState = HIGH; \ @@ -195,7 +224,11 @@ void loop() { 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(psensor, PSENSOR, PIN_INPUT_REVERSE, TIMEOUT_AUTO_OFF) - FSM(rlights, RLIGHTS, PIN_INPUT_REVERSE, TIMEOUT_AUTO_OFF) - FSM(Flights, FLIGHTS, PIN_INPUT_HIGH_BEAM, TIMEOUT_NONE) +void loop() { + DEBOUNCE_SIGNAL(reverseSignal, PIN_INPUT_REVERSE) + DEBOUNCE_SIGNAL(highBeamSignal, PIN_INPUT_HIGH_BEAM) + + FSM(psensor, PSENSOR, reverseSignalEnabled, TIMEOUT_AUTO_OFF) + FSM(rlights, RLIGHTS, reverseSignalEnabled, TIMEOUT_AUTO_OFF) + FSM(Flights, FLIGHTS, highBeamSignalEnabled, TIMEOUT_NONE) } |