stspringer

Harley fog lights blinking

Dec 9th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. //============================================================================================
  2. //BEGIN:Declarations
  3. //============================================================================================
  4. //
  5. #define LEDon HIGH
  6. #define LEDoff LOW
  7.  
  8. #define PUSHED LOW
  9.  
  10. bool blink_the_high_bem_fog_lights = false;
  11.  
  12. int current_buttonState;
  13. int current_button_press_reading = 0;
  14. int Val = 0;
  15. int previous_button_state_reading = LOW; // the previous reading from the input pin
  16. int ledState = LOW;
  17.  
  18.  
  19.  
  20.  
  21.  
  22. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
  23. unsigned long debounceDelay = 25; // the debounce time; increase if the output flickers
  24.  
  25.  
  26.  
  27.  
  28. //
  29. const unsigned long led_blink_speed = 100;
  30. const unsigned long led_blink_speed_2 = 100;
  31.  
  32. byte button_blink_all_fog_lights = 8; //+5V----[Internal Pullup Resistor]----Input Pin ----[switch]----GND
  33. byte button_turn_on_high_beam_fog_lights = 5;
  34. bool onAndOff = false;
  35.  
  36.  
  37. byte fog_light_halo_low_beam_indicator = 6;
  38. byte fog_light_halo_low_beam = 7; //Pin Number----[>|]----[220 ohm]----GND
  39. ////
  40. byte fog_light_high_beam_indicator = 10;
  41. byte fog_light_high_beam = 11; //Pin Number----[>|]----[220 ohm]----GND
  42.  
  43.  
  44. byte lastButton;
  45.  
  46. boolean LEDflag = false;
  47.  
  48.  
  49. unsigned long blinkMillis;
  50. unsigned long switchMillis;
  51. unsigned long currentMillis;
  52.  
  53. ///////
  54. byte lastButton_2;
  55.  
  56. boolean LEDflag_2 = false;
  57.  
  58. unsigned long blinkMillis_2;
  59. unsigned long switchMillis_2;
  60. unsigned long currentMillis_2;
  61.  
  62. int lastButtonState;
  63. int currentButtonState; // the current state of button
  64.  
  65. int trythis = 0;
  66. //============================================================================================
  67. //END:Declarations
  68. //============================================================================================
  69.  
  70.  
  71.  
  72. //============================================================================================
  73. //BEGIN:setup ()
  74. //============================================================================================
  75.  
  76.  
  77. void setup ()
  78. {
  79. Serial.begin(9600);
  80.  
  81. digitalWrite(fog_light_halo_low_beam, LOW );
  82.  
  83. pinMode(fog_light_halo_low_beam_indicator , OUTPUT);
  84. pinMode(fog_light_halo_low_beam, OUTPUT);
  85. //
  86. pinMode(fog_light_high_beam_indicator , OUTPUT);
  87. pinMode(fog_light_high_beam, OUTPUT);
  88.  
  89. //
  90. pinMode(button_blink_all_fog_lights, INPUT_PULLUP);
  91. pinMode(button_turn_on_high_beam_fog_lights, INPUT_PULLUP);
  92.  
  93.  
  94. lastButton = digitalRead (button_blink_all_fog_lights);
  95. digitalWrite(fog_light_halo_low_beam, LEDon);
  96. digitalWrite(fog_light_halo_low_beam_indicator, LEDon);
  97. //
  98. lastButton_2 = digitalRead (button_turn_on_high_beam_fog_lights);
  99. digitalWrite(fog_light_high_beam, LEDoff);
  100. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  101. Serial.begin(9600);
  102.  
  103. } //END of setup()
  104. //===========================================================================================
  105. //END:setup ()
  106. //===========================================================================================
  107.  
  108.  
  109. //============================================================================================
  110. //BEGIN:void loop ()
  111. //===========================================================================================
  112.  
  113. void loop ()
  114. {
  115. currentMillis = millis();
  116. currentMillis_2 = millis();
  117.  
  118.  
  119.  
  120.  
  121. //===========================================================================================
  122. //BEGIN:Do the work here
  123. //===========================================================================================
  124. //this is where you do the work of the temporary on button press
  125. //************************
  126. checkSwitches_1();
  127. //************************
  128. if (LEDflag == true && currentMillis - blinkMillis >= led_blink_speed)
  129. {
  130.  
  131. //start the TIMER
  132. blinkMillis = currentMillis;
  133. //put your code here
  134. //toggle LED
  135.  
  136. //Serial.println(Val);
  137. //Serial.println(current_button_press_reading);
  138.  
  139. Serial.println("this is trythis");
  140. Serial.println(trythis);
  141. Serial.println("this is onAndoff");
  142. Serial.println(onAndOff);
  143.  
  144.  
  145. //trythis = digitalRead(fog_light_high_beam_indicator);
  146.  
  147. if (onAndOff == 0)
  148. //if (trythis == 0)
  149. {
  150.  
  151. //Serial.println(onAndOff);
  152.  
  153. //Serial.println("button pressed");
  154. //trythis = digitalRead(fog_light_high_beam_indicator);
  155. //Serial.println(trythis);
  156. //Serial.println("the High beam fog light indicator is OFF");
  157. // turn LED on:
  158. //digitalWrite(ledPin, HIGH);
  159. digitalWrite (fog_light_halo_low_beam, ! digitalRead (fog_light_halo_low_beam));
  160. digitalWrite (fog_light_halo_low_beam_indicator, ! digitalRead (fog_light_halo_low_beam_indicator));
  161. Serial.println("I am just blinking the low beams now");
  162. }
  163.  
  164. if (onAndOff == 1)
  165. //if (trythis == 1)
  166. {
  167.  
  168. //Serial.println(onAndOff);
  169.  
  170. //Serial.println("button pressed");
  171. //trythis = digitalRead(fog_light_high_beam_indicator);
  172. //Serial.println(trythis);
  173. //Serial.println("the High beam fog light indicator is OFF");
  174. // turn LED on:
  175. //digitalWrite(ledPin, HIGH);
  176. digitalWrite (fog_light_halo_low_beam, ! digitalRead (fog_light_halo_low_beam));
  177. digitalWrite (fog_light_halo_low_beam_indicator, ! digitalRead (fog_light_halo_low_beam_indicator));
  178. digitalWrite (fog_light_high_beam, ! digitalRead (fog_light_high_beam));
  179. digitalWrite (fog_light_high_beam_indicator, ! digitalRead (fog_light_high_beam_indicator));
  180. Serial.println("I am blinking the low beam and high beams too");
  181. }
  182. //===========================================================================================
  183. //END:Do the work here
  184. //===========================================================================================
  185.  
  186. } //end void loop
  187. //============================================================================================
  188. //END:void loop ()
  189. //===========================================================================================
  190.  
  191.  
  192. //this is where you do the work of the led stays on utill button pressed again button press
  193. checkSwitches_2();
  194. if (LEDflag_2 == true && currentMillis_2 - blinkMillis_2 >= led_blink_speed_2)
  195. {
  196. //start the TIMER
  197. blinkMillis_2 = currentMillis_2;
  198. //put your code here
  199. //toggle LED
  200. }
  201.  
  202.  
  203. } //END of void loop()
  204.  
  205. //============================================================================================
  206. //BEGIN:checkSwitches_1()
  207. //============================================================================================
  208. void checkSwitches_1()
  209. {
  210. if (currentMillis - switchMillis < 50) //debounce
  211. {
  212. //it is not time to check the switches
  213. return;
  214. }
  215.  
  216. //start the TIMER
  217. switchMillis = currentMillis;
  218.  
  219.  
  220. byte button = digitalRead (button_blink_all_fog_lights);
  221.  
  222. if (lastButton != button)
  223. {
  224. //save the new state of the switch
  225. lastButton = button;
  226.  
  227. if (button == PUSHED) // button pressed
  228. {
  229. //enable flashing
  230. LEDflag = true;
  231. //digitalWrite(fog_light_high_beam, LEDon);
  232. //digitalWrite(fog_light_high_beam_indicator, LEDon);
  233. }
  234.  
  235. else //not pushed
  236. {
  237. //disable flashing
  238. LEDflag = false;
  239. digitalWrite(fog_light_halo_low_beam, LEDon);
  240. digitalWrite(fog_light_halo_low_beam_indicator, LEDon);
  241.  
  242. }
  243.  
  244. }
  245.  
  246.  
  247. } //END of checkSwitches_1()
  248.  
  249. //============================================================================================
  250. //END:checkSwitches_1()
  251. //============================================================================================
  252.  
  253.  
  254.  
  255. //============================================================================================
  256. //BEGIN:checkSwitches_2()
  257. //============================================================================================
  258. void checkSwitches_2()
  259. {
  260.  
  261.  
  262. // put your main code here, to run repeatedly:
  263. //get the value of current_button_press_reading
  264. current_button_press_reading = digitalRead(button_turn_on_high_beam_fog_lights);
  265.  
  266. // If the switch changed, due to noise or pressing:
  267. if (current_button_press_reading != previous_button_state_reading) {
  268. // reset the debouncing timer
  269.  
  270.  
  271. //NOTE the "+=" in - "lastDebounceTime += millis();"
  272. //The reason for this is that millis() may accumulate "jitter", and the clock then drifts.
  273. //Fine if you use only timer, not so great if you have other timers running
  274. //and you want them all to remain in lock step.
  275.  
  276. lastDebounceTime += millis();
  277.  
  278. }//end Debounce
  279. if ((millis() - lastDebounceTime) > debounceDelay)
  280.  
  281. {
  282. lastButtonState = currentButtonState; // save the last state
  283. currentButtonState = digitalRead(button_turn_on_high_beam_fog_lights); // read new state
  284.  
  285. if(lastButtonState == HIGH && currentButtonState == LOW)
  286. {
  287. if (onAndOff == true)
  288. onAndOff = false;
  289. //Serial.println("The button is pressed");
  290.  
  291. //Serial.println(ledState );
  292. onAndOff = ledState;
  293. LEDflag = false;
  294. //Serial.println(onAndOff);
  295. // toggle state of LED
  296. ledState = !ledState;
  297. }
  298. else
  299. { //button not pushed
  300. onAndOff = false;
  301. //LEDflag = true;
  302. //Serial.println(lastButtonState);
  303. //Serial.println(ledState );
  304. onAndOff = ledState;
  305. //Serial.println(onAndOff);
  306. //delay(2000);
  307. // control LED arccoding to the toggled state
  308. digitalWrite(fog_light_high_beam, ledState);
  309. digitalWrite(fog_light_high_beam_indicator, ledState);
  310.  
  311. }
  312. }//end if millis
  313.  
  314. } //END of checkSwitches_2()
  315.  
  316.  
  317. //============================================================================================
  318. //END:checkSwitches_2()
  319. //============================================================================================
Advertisement
Add Comment
Please, Sign In to add comment