Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 5 December 2018
- reddit e-bike lights controller version 1.0 (headlights, blinkers and brake light)
- reddit: Dougcim53
- Board: Arduino uno/nano
- No external / non-standard libraries used
- */
- // Troubleshooting message switches ########################################
- bool print_function_messages = false; // set to false to turn off all the function serial messages. This one will send a lot of messages if set to true.
- // Printing serial messages is nice when building and testing, but they cause time delays you may not want in the final version.
- // This variable is a switch that allows or prevents all of the function serial messages from printing.
- bool print_blinker_messages = true; // This is the same thing, but just for the blinker on/off changes.
- bool print_headlight_1_messages = true; // And these are for the other outputs.
- bool print_headlight_2_messages = true;
- bool print_brake_light_messages = true;
- bool print_joystick_leftRight_messages = true; // Might as well do the control inputs also, since the whole sketch is still pretty small.
- bool print_joystick_upDown_messages = true;
- bool print_blinker_cancel_button_messages = true;
- bool print_brake_input_messages = true;
- // I/O pin setup ####################################################
- int joystick_leftRight_axis = A0; // analog input for joystick
- int joystick_upDown_axis = A1; // analog input for joystick
- int brake_trigger_pin = 3; // digital input, active = high
- int blinker_cancel_pin = 4; // digital input, active = high
- int headlight_1_pin = 9; // digital output pin
- int headlight_2_pin = 8; // digital output pin
- int blinker_left_pin = 12; // digital output pin
- int blinker_right_pin = 10; // digital output pin
- int brake_light_pin = 11; // digital output pin
- // Control timer setup ###################################################
- // To prevent accidental activation, the buttons/joystick must be pressed for at least two consecutive cycles of whatever their time intervals are.
- int headlight_1_activation_time = 500; // 500 time value is in milliseconds. This is the time to press the joystick UP to turn on headlight #1.
- int headlight_2_activation_time = 750; // 1000 time value is in milliseconds. This is the time to press the joystick UP to turn on headlight #2.
- // Note that the headlight #2 value must be greater than #1.
- int headlight_turn_off_time = 1000; // Milliseconds value. This is the time to pull the joystick down to turn off the headlights/running lights.
- int blinker_activation_interval = 500; // 500 time value is milliseconds. This is how long to hold the joystick left or right to turn that blinker direction on.
- int blinker_on_time = 300; // 300 time value is milliseconds. This is the time that the blinker LED will be 'on'.
- int blinker_off_time = 150; // 150 time value is in milliseconds. This is the time that the blinker LED will be 'off'.
- int blinker_cancel_button_time_interval = 250; // This is the time to hold the blinker cancel button to turn off the blinkers.
- int brake_light_low_value = 128; // This is a PWM value. This is the value of the brake light, when NOT active, but when either of the headlights are on.
- int brake_light_high_value = 255; // This is a PWM value. This is the active value of the brake light, if the leadlights are on or not.
- int brake_input_check_interval = 100; // This is the time interval to check the brake input pin.
- // Joystick threshold values ###################################################
- // These assume that the analog pin value is used directly (0 - 1023)
- int joystick_up_active_value = 800;
- int joystick_down_active_value = 200;
- // You may need to flip these values around, depending on how the joystick is wired.
- // Also the functions reading the joystick values may need the comparator signs changed.
- int joystick_left_active_value = 200;
- int joystick_right_active_value = 800;
- // You may need to flip these values around, depending on how the joystick is wired.
- // Also the functions reading the joystick values may need the comparator signs changed.
- // Other assorted variables ###############################################
- int blinker_left_state = 0; // 0 = off, 1 = on
- int blinker_left_LED_state = 0; // This represents if the blinker pin is off or on.
- int blinker_left_trigger_count = 0; // This must reach 2 for the blinker to turn on.
- int blinker_right_state = 0; // 0 = off, 1 = on
- int blinker_right_LED_state = 0; // This represents if the blinker pin is off or on.
- int blinker_right_trigger_count = 0; // This must reach 2 for the blinker to turn on.
- int headlight_1_state = 0; // 0 = off, 1 = on
- int headlight_1_trigger_count = 0;
- int headlight_2_state = 0; // 0 = off, 1 = on
- int headlight_2_trigger_count = 0;
- int headlights_off_trigger_count = 0;
- int blinkers_off_trigger_count = 0;
- int brake_light_state = 0; // 0 = off, 1 = on
- int brake_light_mode = 1; // 1 = low, 2 = high
- unsigned long brake_control_begin_time = 0; // These are used to time the control checks for the brake input button.
- unsigned long brake_control_current_time = 0;
- unsigned long blinker_cancel_control_begin_time = 0; // These are used to time the control checks for the blinker cancel button.
- unsigned long blinker_cancel_control_current_time = 0;
- unsigned long headlight_1_control_begin_time = 0; // These are used to time the control that turns headlight 1 on and off.
- unsigned long headlight_1_control_current_time = 0;
- unsigned long headlight_2_control_begin_time = 0; // These are used to time the control that turns headlight 2 on and off.
- unsigned long headlight_2_control_current_time = 0;
- unsigned long blinker_control_begin_time = 0; // These are used to time the control that turns the blinkers on and off.
- unsigned long blinker_control_current_time = 0;
- unsigned long blinker_blink_begin_time = 0; // These are used to time the intervals that the blinkers turn on and off, when activated.
- unsigned long blinker_blink_current_time = 0;
- unsigned long blinker_interval_time = 0;
- // ##################################################################################
- void setup() {
- Serial.begin(9600);
- //pinMode(joystick_leftRight_axis, INPUT);
- //pinMode(joystick_upDown_axis, INPUT);
- // With the Arduino IDE and boards, you don't need to set the pinMode for pins that are used for analogRead.
- // IIRC it is because the pins are switched to input during runtime by default, since analogRead can only be performed on an input pin anyway.
- // There is info about the matter online, if you care to read futher.
- // One example of many ----> http://forum.arduino.cc/index.php?topic=281728.0
- pinMode(blinker_right_pin, OUTPUT);
- pinMode(blinker_left_pin, OUTPUT);
- pinMode(brake_trigger_pin, INPUT);
- pinMode(brake_light_pin, OUTPUT);
- pinMode(headlight_2_pin, OUTPUT);
- pinMode(headlight_1_pin, OUTPUT);
- if (print_function_messages == true) {
- Serial.println("exiting setup()");
- // If you are using serial messages anyway,
- // it is good to have a serial message when leaving the setup() function.
- }
- } // end of setup() function
- void loop() {
- check_joystick_leftRight_value();
- check_headlight_1_control_value();
- check_headlight_2_control_value();
- check_brake_input_value();
- check_blinker_cancel_button_value();
- check_blinker_state();
- } // end of main loop
- void check_joystick_leftRight_value() {
- if (print_function_messages == true) {
- Serial.println("check_joystick_leftRight_value() called");
- }
- // This function checks the joystick left-right value and turns the same-direction blinker on if it has been held for 2 cycles.
- blinker_control_current_time = millis();
- if (blinker_control_current_time > blinker_control_begin_time) {
- if (blinker_control_current_time >= (blinker_control_begin_time + blinker_activation_interval)) {
- int leftRightValue = analogRead(joystick_leftRight_axis);
- // Below turns on the left blinker.
- if (leftRightValue <= joystick_left_active_value) {
- // NOTE: if you flip the joystick values, then you may need to flip the comparator signs above as well.
- // This means the joystick is pushed left.
- if (print_joystick_leftRight_messages == true) {
- Serial.print("left joystick detected: value = ");
- Serial.println(leftRightValue);
- }
- if (blinker_left_state == 0) {
- switch (blinker_left_trigger_count) {
- case 0:
- blinker_left_trigger_count = 1; // increment this trigger count to 1
- blinker_right_trigger_count = 0; // reset the other blinker trigger count.
- break;
- case 1:
- // if the trigger count reaches 2, the left trigger count is reset to zero and the left blinker is turned on here.
- blinker_left_trigger_count = 0;
- start_left_blinker();
- break;
- }
- }
- }
- // Below turns on the right blinker.
- if (leftRightValue >= joystick_right_active_value) {
- // NOTE: if you flip the joystick values, then you may need to flip the comparator signs above as well.
- // This means the joystick is pushed right.
- if (print_joystick_leftRight_messages == true) {
- Serial.print("right joystick detected: value = ");
- Serial.println(leftRightValue);
- }
- if (blinker_right_state == 0) {
- switch (blinker_right_trigger_count) {
- case 0:
- blinker_right_trigger_count = 1; // increment this trigger count to 1
- blinker_left_trigger_count = 0; // reset the other blinker trigger count.
- break;
- case 1:
- // if the trigger count reaches 2, the left trigger count is reset to zero and the left blinker is turned on here.
- blinker_right_trigger_count = 0;
- start_right_blinker();
- break;
- }
- }
- }
- }
- }
- else {
- blinker_control_begin_time = millis();
- }
- }
- void check_headlight_1_control_value() {
- if (print_function_messages == true) {
- Serial.println("check_headlight_1_control_value() called");
- }
- // All this does is check the joystick at the headlight_1_activation_time interval,
- // and turns on the headlight #1 if the joystick has been held up for 2 complete intervals.
- headlight_1_control_current_time = millis();
- if (headlight_1_control_current_time > headlight_1_control_begin_time) {
- if (headlight_1_control_current_time >= (headlight_1_control_begin_time + headlight_1_activation_time)) {
- int upDownValue = analogRead(joystick_upDown_axis);
- // Below checks the timer to see if headllight 1 should be turned on.
- if (upDownValue >= joystick_up_active_value) {
- if (headlight_1_state == 0) {
- // NOTE: if you flip the joystick values, then you may need to flip the comparator signs above as well.
- // This means the joystick is pushed left.
- switch (headlight_1_trigger_count) {
- case 0:
- headlight_1_trigger_count = 1; // increment this trigger count to 1
- break;
- case 1:
- // if the trigger count reaches 2, the left trigger count is reset to zero and headlight 1 is turned on here.
- headlight_1_trigger_count = 0;
- turn_on_headlight_1();
- set_brake_light_low(); // also turn on the brake light to low
- break;
- }
- }
- if (print_joystick_upDown_messages == true) {
- Serial.print("UP joystick detected: value = ");
- Serial.println(upDownValue);
- }
- }
- // This function is also used to see if the headlights should be turned off.
- // This is done here because the headlight-1 function has its own timing code anyway,
- // and the headlights-off fuction doesn't really need its own timing code.
- // The headlights-off function just needs to be called periodically, so it shares the same control interval as headlight-1.
- check_headlight_turnOff_value(upDownValue);
- headlight_1_control_begin_time = millis();
- }
- }
- else {
- headlight_1_control_begin_time = millis();
- }
- }
- void turn_on_headlight_1() {
- // All that this does is turn on headlight #1.
- if (print_headlight_1_messages == true) {
- Serial.println("headlight 1 turning on");
- }
- headlight_1_state = 1;
- digitalWrite(headlight_1_pin, headlight_1_state);
- }
- void turn_off_headlight_1() {
- // All that this does is turn off headlight #1.
- if (print_headlight_1_messages == true) {
- Serial.println("headlight 1 turning off");
- }
- headlight_1_state = 0;
- digitalWrite(headlight_1_pin, headlight_1_state);
- }
- void check_headlight_2_control_value() {
- if (print_function_messages == true) {
- Serial.println("check_headlight_2_control_value() called");
- }
- // This function turns on headlight #2, if the joystick has been held up long enough.
- headlight_2_control_current_time = millis();
- if (headlight_2_control_current_time > headlight_2_control_begin_time) {
- if (headlight_2_control_current_time >= (headlight_2_control_begin_time + headlight_2_activation_time)) {
- int upDownValue = analogRead(joystick_upDown_axis);
- if (headlight_2_state == 0) {
- if (upDownValue >= joystick_up_active_value) {
- // NOTE: if you flip the joystick values, then you may need to flip the comparator signs above as well.
- // This means the joystick is pushed left.
- switch (headlight_2_trigger_count) {
- case 0:
- headlight_2_trigger_count = 1; // increment this trigger count to 1
- break;
- case 1:
- // if the trigger count reaches 2, the left trigger count is reset to zero and headlight 1 is turned on here.
- headlight_2_trigger_count = 0;
- turn_on_headlight_2();
- set_brake_light_low(); // also turn on the brake light to low (it should already be on from headlight #1 turning on, but anyway)
- break;
- }
- }
- }
- headlight_2_control_begin_time = millis();
- }
- }
- else {
- headlight_2_control_begin_time = millis();
- }
- }
- void turn_on_headlight_2() {
- // All that this does is turn on headlight #2.
- if (print_headlight_2_messages == true) {
- Serial.println("headlight 2 turning on");
- }
- headlight_2_state = 1;
- digitalWrite(headlight_2_pin, headlight_2_state);
- }
- void turn_off_headlight_2() {
- // All that this does is turn off headlight #2.
- if (print_headlight_2_messages == true) {
- Serial.println("headlight 2 turning off");
- }
- headlight_2_state = 0;
- digitalWrite(headlight_2_pin, headlight_2_state);
- }
- void check_headlight_turnOff_value(int upDownValue) {
- if (print_function_messages == true) {
- Serial.println("check_headlight_turnOff_value(int) called");
- }
- // This function turns off the headlights, if the joystick has been held down for two cycles of headlight #1's interval.
- upDownValue = analogRead(joystick_upDown_axis);
- // Below checks the timer to see if headllight 1 should be turned on.
- if (upDownValue <= joystick_down_active_value) {
- if (headlight_1_state == 1 || headlight_2_state == 1 ) { // Probably don't need to check #2 (#2 would not be on unless #1 was also) but anyway.
- // NOTE: if you flip the joystick values, then you may need to flip the comparator signs above as well.
- // This means the joystick is pushed left.
- switch (headlights_off_trigger_count) {
- case 0:
- headlights_off_trigger_count = 1; // increment this trigger count to 1
- break;
- case 1:
- // if the trigger count reaches 2, the left trigger count is reset to zero and headlight 1 is turned on here.
- headlights_off_trigger_count = 0;
- turn_off_headlight_1();
- turn_off_headlight_2();
- if (brake_light_mode == 1) {
- // If the brake light is in low mode, turn it off here.
- set_brake_light_off();
- }
- break;
- }
- }
- if (print_joystick_upDown_messages == true) {
- Serial.print("DOWN joystick detected: value = ");
- Serial.println(upDownValue);
- }
- }
- }
- void check_brake_input_value() {
- // This function checks the brake input pin and turns the brake light to the [off] or [low] level as needed.
- brake_control_current_time = millis();
- if (brake_control_current_time > brake_control_begin_time) {
- if (brake_control_current_time >= (brake_control_begin_time + brake_input_check_interval)) {
- int buttonState = digitalRead(brake_trigger_pin);
- if (buttonState == 1) {
- if (brake_light_state == 0) {
- brake_light_state = 1;
- set_brake_light_high(); // Turn the brake light on high, any time the brake input pin is activated.
- if (print_brake_input_messages == true) {
- Serial.println("brake input = HIGH");
- }
- }
- }
- else { // If the brake input pin is low:
- if (brake_light_state == 1) {
- brake_light_state = 0;
- if (headlight_1_state == 1 || headlight_2_state == 1) {
- set_brake_light_low(); // If the headlights are on, turn the brake to 'low'.
- }
- else {
- set_brake_light_off(); // If the headlights are off, turn the brake to 'off'.
- }
- if (print_brake_input_messages == true) {
- Serial.println("brake input = LOW");
- }
- }
- }
- brake_control_begin_time = millis();
- }
- }
- else {
- brake_control_begin_time = millis();
- }
- }
- void set_brake_light_off() {
- // This turns the brake light pin off entirely.
- if (print_brake_light_messages == true) {
- Serial.println("set_brake_light_off() called");
- }
- analogWrite(brake_light_pin, 0);
- }
- void set_brake_light_low() {
- // This turns the brake light pin to the 'low' setting (running light when the headlights are on).
- if (print_brake_light_messages == true) {
- Serial.println("set_brake_light_low() called");
- }
- analogWrite(brake_light_pin, brake_light_low_value);
- }
- void set_brake_light_high() {
- // This turns the brake light pin on at the high setting.
- if (print_brake_light_messages == true) {
- Serial.println("set_brake_light_high() called");
- }
- analogWrite(brake_light_pin, brake_light_high_value);
- }
- void check_blinker_cancel_button_value() {
- // This turns off the blinkers, if the cancel button has been held down for two interval cycles.
- blinker_cancel_control_current_time = millis();
- if (blinker_cancel_control_current_time > blinker_cancel_control_begin_time) {
- if (blinker_cancel_control_current_time >= (blinker_cancel_control_begin_time + blinker_cancel_button_time_interval)) {
- int buttonState = digitalRead(blinker_cancel_pin);
- if (blinker_left_state == 1 || blinker_right_state == 1) {
- if (print_blinker_cancel_button_messages == true) {
- Serial.println("blinker cancel button press detected");
- }
- if (buttonState == 1) {
- switch (blinkers_off_trigger_count) {
- case 0:
- blinkers_off_trigger_count = 1; // increment this trigger count to 1
- break;
- case 1:
- // if the trigger count reaches 2, the trigger count is reset to zero and both blinkers can be turned off.
- blinkers_off_trigger_count = 0;
- stop_left_blinker();
- stop_right_blinker();
- if (print_blinker_cancel_button_messages == true) {
- Serial.println("blinkers turning off");
- }
- break;
- }
- }
- }
- blinker_cancel_control_begin_time = millis();
- }
- }
- else {
- blinker_cancel_control_begin_time = millis();
- }
- }
- void start_left_blinker() {
- // All that this function does is start the left blinker.
- if (print_blinker_messages == true) {
- Serial.println("start_left_blinker() called");
- }
- stop_right_blinker(); // Make sure the other blinker is turned off first.
- blinker_left_state = 1;
- blinker_left_LED_state = 1; // This should be zero anyway, but it can be set here to be certain.
- digitalWrite(blinker_left_pin, blinker_left_LED_state);
- blinker_interval_time = blinker_on_time;
- blinker_blink_begin_time = millis();
- }
- void start_right_blinker() {
- // All that this function does is start the right blinker.
- if (print_blinker_messages == true) {
- Serial.println("start_right_blinker() called");
- }
- stop_left_blinker(); // Make sure the other blinker is turned off first.
- blinker_right_state = 1;
- blinker_right_LED_state = 1; // This should be zero anyway, but it can be set here to be certain.
- digitalWrite(blinker_right_pin, blinker_right_LED_state);
- blinker_interval_time = blinker_on_time;
- blinker_blink_begin_time = millis();
- }
- void stop_left_blinker() {
- // All that this function does is shut off the left blinker.
- if (print_blinker_messages == true) {
- Serial.println("stop_left_blinker() called");
- }
- blinker_left_state = 0;
- blinker_left_LED_state = 0;
- change_left_blinker_pin();
- }
- void stop_right_blinker() {
- // All that this function does is shut off the right blinker.
- if (print_blinker_messages == true) {
- Serial.println("stop_right_blinker() called");
- }
- blinker_right_state = 0;
- blinker_right_LED_state = 0;
- change_right_blinker_pin();
- }
- void check_blinker_state() {
- // This function causes the blinker LEDs to switch on and off, if they are activated.
- // If the left blinker is {in use}, then this part switches the left blinker LED pin on and off on its own.
- if (blinker_left_state == 1) {
- blinker_blink_current_time = millis();
- if (blinker_blink_current_time > blinker_blink_begin_time) {
- if (blinker_blink_current_time >= (blinker_blink_begin_time + blinker_interval_time)) {
- if (blinker_left_LED_state == 0) {
- blinker_left_LED_state = 1;
- blinker_interval_time = blinker_on_time;
- }
- else {
- blinker_left_LED_state = 0;
- blinker_interval_time = blinker_off_time;
- }
- change_left_blinker_pin();
- blinker_blink_begin_time = millis();
- }
- }
- else {
- blinker_blink_begin_time = millis();
- }
- }
- // If the right blinker is {in use}, then this part switches the right blinker LED pin on and off on its own.
- if (blinker_right_state == 1) {
- blinker_blink_current_time = millis();
- if (blinker_blink_current_time > blinker_blink_begin_time) {
- if (blinker_blink_current_time >= (blinker_blink_begin_time + blinker_interval_time)) {
- if (blinker_right_LED_state == 0) {
- blinker_right_LED_state = 1;
- blinker_interval_time = blinker_on_time;
- }
- else {
- blinker_right_LED_state = 0;
- blinker_interval_time = blinker_off_time;
- }
- change_right_blinker_pin();
- blinker_blink_begin_time = millis();
- }
- }
- else {
- blinker_blink_begin_time = millis();
- }
- }
- }
- void change_left_blinker_pin() {
- // All that this function does is change the output pin value of the left blinker.
- // This is a separate function so that a serial.print message can be attached to the pin change event.
- if (print_blinker_messages == true) {
- if (blinker_left_LED_state == 0) {
- Serial.println("left blinker blinking off");
- }
- else {
- Serial.println("left blinker blinking on");
- }
- }
- digitalWrite(blinker_left_pin, blinker_left_LED_state);
- }
- void change_right_blinker_pin() {
- // All that this function does is change the output pin value of the right blinker.
- // This is a separate function so that a serial.print message can be attached to the pin change event.
- if (print_blinker_messages == true) {
- if (blinker_right_LED_state == 0) {
- Serial.println("right blinker blinking off");
- }
- else {
- Serial.println("right blinker blinking on");
- }
- }
- digitalWrite(blinker_right_pin, blinker_right_LED_state);
- }
Advertisement
Add Comment
Please, Sign In to add comment