Advertisement
stspringer

My Harley Fog Lights Arduino Script with police lights

Feb 6th, 2023 (edited)
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.45 KB | None | 0 0
  1. //My Working 03_02_ 2022 Working Harley Fog Lights With Police Lights LED Array for loop code
  2. //From John's Tech Wiki
  3. //Written by John Guarnieri 03_02_ 2022
  4. //note void run_police_lights_2() is not used it is commented out
  5. //void Police_Lights_Blink_3() is used for the police lights
  6.  
  7.  
  8. //My_Working_OneButton_Harley_Fog_Lights_With_Police_Fine_Tuneup_1.uno
  9. //button 4 blinks the fog lights
  10.  
  11. //Button 5 has 3 jobs short click, long press, double click
  12. //Button 5 will have 3 jobs, a short press will toggle on or off the high beam fog lights, as you have coded.
  13. //Button 5 the first long press will turn off all the fog lights, and a second long press will turn on just the low beam fog lights, as you have coded.
  14. //while all the fog lights are off , no other button presses will work
  15. //Button 5 a first double click, will turn on the police lights, and a second double click will turn off the police lights and just light the low beam fog lights, the high beams will be off.
  16. //The low beam fog lights are always on as default.
  17.  
  18. //Declarations
  19. #include "OneButton.h"
  20.  
  21. //====================================================================================================================================
  22. //BEGIN: testing another configuration and sequence here 03_04_2021 I like this one, change the BLINK_SPEED for more pleasing effects
  23. //====================================================================================================================================
  24.  
  25. //#define NUM_LEDS 4 //original 3
  26. //#define NUM_STEPS 48
  27. //#define BLINK_SPEED 75 //Original 40 // [ms] lower value = faster blinking
  28. //#define BLINK_TIME 10 // [s]
  29. //#define TRIGGER_PIN 5//original 2
  30. //byte led_pin[NUM_LEDS] = {6,7,8,9}; // original {8,9,10}; I added LED 9
  31.  
  32. //use this blink time
  33. #define NUM_LEDS 4
  34. #define NUM_STEPS 48
  35. #define BLINK_SPEED 100 //Original 40 // [ms] lower value = faster blinking
  36. #define BLINK_TIME 500 // [s]
  37. #define TRIGGER_PIN 5//original 2
  38. byte led_pin[NUM_LEDS] = {6,7,8,9}; // original {8,9,10}; I added LED 9
  39. byte sequencer[NUM_LEDS][NUM_STEPS] = {
  40. // 0 1 2 3 4
  41. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
  42. 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0, //LED 6 white low beam fog light
  43. 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0, //LED 7 blue low beam fog light indicator
  44. 0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0, //LED 8 white white high beam fog light
  45. 0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0 //LED 9 red high beam fog light indicator I added this sequence line for led 9 same as sequence for LED 8
  46. };
  47. //===================================================================================================================================
  48. //END: testing another configuration and sequence here 03_04_2021 I like this one, change the BLINK_SPEED for more pleasing effects
  49. //===================================================================================================================================
  50.  
  51.  
  52. byte state = 1, transition;
  53. byte n, p, blink_enabled;
  54. unsigned long time_to_change, time_to_switch_off;
  55. //=======================================================================================================================================================
  56. //END: run_police_lights_3 code
  57. //=======================================================================================================================================================
  58.  
  59. //=======================================================================================================================================================
  60. //BEGIN run_police_lights_2 code
  61. //=======================================================================================================================================================
  62. // HARDWARE AND TIMER SETTINGS
  63. // YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
  64. // CONSTANT DEFINITION
  65.  
  66.  
  67. constexpr byte LedPinsStages[] {6,7,8,9}; // portPin o---|220|---|LED|---GND
  68.  
  69.  
  70.  
  71. //LED pins
  72. constexpr byte LedPinsStage1[] {6,7}; // portPin o---|220|---|LED|---GND
  73. constexpr byte LedPinsStage3[] {8,9};
  74.  
  75. // portPin o---|220|---|LED|---GND
  76.  
  77. //How many flashes
  78. constexpr int numberOfFlashs1 {6};
  79. constexpr int numberOfFlashs3 {6};
  80.  
  81. //set the times
  82. constexpr unsigned long delayTimeStage1 {50};
  83. constexpr unsigned long delayTimeStage2 {100};
  84. constexpr unsigned long delayTimeStage3 {50};
  85.  
  86.  
  87.  
  88. #define OutPutTest
  89. unsigned long OutPutTestTime {500};
  90. // VARIABLE DECLARATION AND DEFINITION
  91. unsigned long currentTime;
  92. struct TIMER { // has the following members
  93. unsigned long duration; // memory for interval time
  94. unsigned long stamp; // memory for actual time
  95. bool onOff; // control for start/stop
  96. };
  97. TIMER stage1 {delayTimeStage1, 0, false};
  98. TIMER stage2 {delayTimeStage2, 0, false};
  99. TIMER stage3 {delayTimeStage3, 0, false};
  100.  
  101. bool timerEvent (TIMER &timer)
  102. {
  103. return (currentTime - timer.stamp >= timer.duration && timer.onOff);
  104. }
  105. void startTimer(TIMER &timer)
  106. {
  107. timer.stamp = currentTime;
  108. timer.onOff = true;
  109. }
  110. void resetTimer(TIMER &timer)
  111. {
  112. timer.stamp = currentTime;
  113. }
  114. void stoppTimer(TIMER &timer)
  115. {
  116. timer.onOff = false;
  117. }
  118. void toogleLedPin(int pin)
  119. {
  120. digitalWrite(pin, !digitalRead(pin));
  121. }
  122. //=======================================================================================================================================================
  123. //END run_police_lights_2 code
  124. //=======================================================================================================================================================
  125.  
  126. // Setup a new OneButton push buttons on pin 4 and 5
  127.  
  128. /*
  129. Button 4 - toggle blinking on/off
  130. Button 5 - toggle HIGH fog ligts on, long click - toggle all lights on/off
  131. */
  132.  
  133. //OneButton blink_button(4, true); //original
  134. //OneButton on_off_button(5, true); //original
  135.  
  136. OneButton button_blink_the_fog_lights(4, true);
  137. OneButton switch_turn_on_the_high_beam_fog_lights(5, true);
  138.  
  139.  
  140. //define
  141. #define DISABLED false
  142. #define ENABLED true
  143. #define LEDon HIGH
  144. #define LEDoff LOW
  145.  
  146. unsigned long currentMillis;
  147. unsigned long previousMillis;
  148. unsigned long on_off_long_press_start_time;
  149.  
  150. const unsigned long blinkInterval = 100;
  151. //this is not needed on a long press because of switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms
  152. const unsigned long longPressInterval = 2500;
  153.  
  154. //LED's
  155. const int fog_light_low_beam = 6;
  156. const int fog_light_low_beam_indicator = 7;
  157. const int fog_light_high_beam = 8;
  158. const int fog_light_high_beam_indicator = 9;
  159.  
  160. //bool
  161. bool blinking = DISABLED;
  162. bool inLongPress = false;
  163. bool all_the_fog_lights_are_off = DISABLED;
  164.  
  165. //enum
  166. //enum { LIGHTS_OFF, LIGHTS_LOW_BEAM, LIGHTS_BOTH };//original
  167. enum { LIGHTS_OFF, LIGHTS_LOW_BEAM, LIGHTS_BOTH, POLICE_LIGHTS };
  168. int lightState;
  169.  
  170.  
  171. //Police lights
  172. bool start_police_lights_now = DISABLED;
  173. bool Short_Button_Press_Allowed = ENABLED;
  174.  
  175. const long onDuration_1 = 250;// OFF time for LED//orig 100
  176. const long offDuration_1 = 200;// ON time for LED //orig 500 lower this number for a faster blink
  177.  
  178. const long onDuration_2 = 250;// OFF time for LED// orig 100
  179. const long offDuration_2 = 200;// ON time for LED//orig 500 lower this number for a faster blink
  180.  
  181.  
  182. int LEDState_1 = HIGH;// initial state of LED
  183. int LEDState_2 = LOW;// initial state of LED
  184.  
  185. long rememberTime_1 = 0;// this is used by the code
  186. long rememberTime_2 = 0;
  187. //end Police
  188.  
  189. //END Declarations
  190.  
  191. void setup()
  192. {
  193. for(n=0; n<NUM_LEDS; n++)
  194.  
  195. //=======================================================================================================================================================
  196. //BEGIN run_police_lights_2 code
  197. //=======================================================================================================================================================
  198. // https://www.learncpp.com/cpp-tutorial/for-each-loops/
  199. for (auto Output_ : LedPinsStages) pinMode(Output_, OUTPUT);
  200. #ifdef OutPutTest
  201. // check outputs
  202. //for (auto Output_ : LedPinsStages) digitalWrite(Output_, HIGH), delay(OutPutTestTime);//original
  203. //BEGIN my add edit
  204.  
  205. ////this does NOT light the 4 LED's, on startup, when Output is set to LOW, uncomment it if you wish, and then "comment out" for (auto Output_ : LedPinsStages) digitalWrite(Output_, HIGH), delay(OutPutTestTime);
  206. //for (auto Output_ : LedPinsStages) digitalWrite(Output_, LOW), delay(OutPutTestTime);
  207.  
  208.  
  209. //=======================================================================================================================================================
  210. //BEGIN: IMPORTANT NOTE optional 4 LED's light up in sequence,on startup just a fancy look on startup
  211. //=======================================================================================================================================================
  212. //this next line lights the 4 LED's, on startup, when Output is set to HIGH, comment it out is you wish, and then "uncomment" /for (auto Output_ : LedPinsStages) digitalWrite(Output_, LOW), delay(OutPutTestTime);
  213. //for (auto Output_ : LedPinsStages) digitalWrite(Output_, HIGH), delay(OutPutTestTime);
  214.  
  215. //END my add edit ok to comment out this next line for loop if you don't on start up low beam led's will turn on slow
  216. //for (auto Output_ : LedPinsStages) digitalWrite(Output_, LOW), delay(OutPutTestTime);
  217. //=======================================================================================================================================================
  218. //END: IMPORTANT NOTE optional 4 LED's light up in sequence,on startup just a fancy look on startup
  219. //=======================================================================================================================================================
  220. #endif
  221. startTimer(stage1);
  222. //=======================================================================================================================================================
  223. //END run_police_lights_2 code
  224. //=======================================================================================================================================================
  225.  
  226. pinMode(fog_light_low_beam, OUTPUT);
  227. pinMode(fog_light_low_beam_indicator, OUTPUT);
  228. pinMode(fog_light_high_beam, OUTPUT);
  229. pinMode(fog_light_high_beam_indicator, OUTPUT);
  230.  
  231. digitalWrite(fog_light_high_beam, LEDoff);
  232. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  233. digitalWrite(fog_light_low_beam, LEDon);
  234. digitalWrite(fog_light_low_beam_indicator, LEDon);
  235.  
  236. //turns on the low beeams default
  237. lightState = LIGHTS_LOW_BEAM;
  238.  
  239. // Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
  240. Serial.begin(115200);
  241. while (!Serial)
  242. {
  243. ; // wait for serial port to connect. Needed for Leonardo only
  244. }
  245.  
  246. //link your buttos here
  247. button_blink_the_fog_lights.attachClick(blink_click);//original this is for a "latched switch" if you want one
  248.  
  249. //begin my add or edit
  250. button_blink_the_fog_lights.attachLongPressStart(blink_click);//this is for a momentary-on switch the "blinking" variable gets switched on and off in the blink_click function, blinking = !blinking;
  251. button_blink_the_fog_lights.attachLongPressStop(blink_click);//this is for a momentary-on switch the "blinking" variable gets switched on and off in the blink_click function, blinking = !blinking;
  252. //
  253. button_blink_the_fog_lights.setDebounceTicks(50);
  254. button_blink_the_fog_lights.setPressTicks(10);//set faster click response
  255. //end my add or edit
  256.  
  257. //original
  258. switch_turn_on_the_high_beam_fog_lights.attachClick(on_off_click);//original
  259. switch_turn_on_the_high_beam_fog_lights.attachLongPressStart(on_off_long_press_start);//original
  260. switch_turn_on_the_high_beam_fog_lights.attachDuringLongPress(on_off_long_press);//original
  261. switch_turn_on_the_high_beam_fog_lights.attachLongPressStop(on_off_long_press_stop);//original
  262.  
  263. //begin my add or edit
  264. switch_turn_on_the_high_beam_fog_lights.setDebounceTicks(50);
  265. switch_turn_on_the_high_beam_fog_lights.setClickTicks(150);//this 150ms will allow the doubleclick to work, any "lower" and doubleclick will not be detected
  266. switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms
  267. switch_turn_on_the_high_beam_fog_lights.attachDoubleClick(doubleclick); //will use this for police lights
  268. //end my add or edit
  269.  
  270. } //setup
  271.  
  272.  
  273. void loop()
  274. {
  275. //don't forget to set start time with millis()
  276. currentMillis = millis();
  277.  
  278. //currentTime = millis(); used for run_police_lights_2
  279. currentTime = millis();
  280.  
  281.  
  282. // keep watching the push buttons:
  283. button_blink_the_fog_lights.tick();
  284. switch_turn_on_the_high_beam_fog_lights.tick();
  285.  
  286. if (blinking)
  287. {
  288. //call the function
  289. blink_the_fog_lights();
  290. }
  291.  
  292. if(start_police_lights_now)//toggled in doubleclick
  293. {
  294. lightState = POLICE_LIGHTS;//set lightState to POLICE_LIGHTS
  295. blinking = DISABLED;
  296. Short_Button_Press_Allowed = DISABLED;//don't allow short clicks
  297. //call the function
  298. //run_police_lights();
  299. //run_police_lights_2();
  300. // this uses array and for loop 03_02_2022
  301. blink_enabled = ENABLED;
  302. start_police_lights_now = ENABLED;
  303. switchState();
  304. switchtransition();
  305. Police_Lights_Blink_3();
  306. }
  307. else//police lights are off
  308. {
  309. Short_Button_Press_Allowed = ENABLED;// allow short clicks
  310. }
  311.  
  312. }//loop
  313.  
  314. //SHORT PRESS BUTTON 4 toggle on and off the high beam fog lights
  315. void on_off_click()//button 5 short press hereh "LIGHTS_BOTH" is doing 1 job here for a "short press" high beams are OFF low beams are ON
  316. {
  317. // if the high beam fog lights are off, turn them on
  318. // if the high beam fog lights are on, turn them off
  319.  
  320. if(Short_Button_Press_Allowed)
  321. {
  322. Serial.println("switch_turn_on_the_high_beam_fog_lights click.");
  323. switch ( lightState )
  324. {
  325. case POLICE_LIGHTS:// short press do noting
  326. break;
  327.  
  328. case LIGHTS_OFF: // all lights are off so ignore
  329. break;
  330.  
  331. //this is called initally from void setup() lightState = LIGHTS_LOW_BEAM;
  332. case LIGHTS_LOW_BEAM: // low beam fog lights are on so switch to just high beam fog lights
  333. digitalWrite(fog_light_high_beam, LEDon);
  334. digitalWrite(fog_light_high_beam_indicator, LEDon);
  335. digitalWrite(fog_light_low_beam, LEDon);
  336. digitalWrite(fog_light_low_beam_indicator, LEDon);
  337. lightState = LIGHTS_BOTH;
  338. Serial.println("high ON");
  339. break;
  340.  
  341. case LIGHTS_BOTH: // both low and high beam fog lights are on so switch high off, "LIGHTS_BOTH" is doing 1 job here for the second "short press" high beams are OFF low beams are ON
  342. digitalWrite(fog_light_high_beam, LEDoff);
  343. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  344. digitalWrite(fog_light_low_beam, LEDon);
  345. digitalWrite(fog_light_low_beam_indicator, LEDon);
  346. lightState = LIGHTS_LOW_BEAM;
  347. Serial.println("high OFF");
  348. break;
  349. }//switch case
  350. }//if
  351. }// end on_off_click()
  352.  
  353. //LONG PRESS BUTTON 5 on the first long press turn off all the fog lights then on the second long press turn back on just the low beam fog lights
  354. void on_off_long_press()//button 5 long press here or doubleclick
  355. {
  356. // this gets called while a long press is in progress
  357. // if enough time has elapsed, toggle the state
  358. //using millis() here
  359. unsigned long duration = millis() - on_off_long_press_start_time;
  360.  
  361. if ( duration >= longPressInterval && inLongPress == true )
  362. {
  363. // if the high beam fog lights are off, turn them on
  364. // if the high beam fog lights are on, turn them off
  365.  
  366. Serial.println("Button 5 on_off_longPress.");
  367. //lightState can be used in other functions
  368. switch (lightState)
  369. {
  370.  
  371. case LIGHTS_OFF: // all lights are off so turn on low beam fog lights
  372. Serial.println("long press button 5 High Beam Fog Lights Off will call LIGHTS_BOTH on the next press");
  373. digitalWrite(fog_light_high_beam, LEDoff);
  374. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  375. digitalWrite(fog_light_low_beam, LEDon);
  376. digitalWrite(fog_light_low_beam_indicator, LEDon);
  377. all_the_fog_lights_are_off = DISABLED;
  378. lightState = LIGHTS_LOW_BEAM;
  379. Serial.println("low ON");
  380. break;
  381.  
  382. case LIGHTS_LOW_BEAM: // low beam fog lights are on so switch everything off
  383.  
  384.  
  385. case LIGHTS_BOTH: // "LIGHTS_BOTH" is doing another job here for a "long press", both low and high beam fog lights are off
  386. Serial.println("long press button 5 ALL Fog Lights Off will call LIGHTS_OFF on the next press");
  387. digitalWrite(fog_light_high_beam, LEDoff);
  388. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  389. digitalWrite(fog_light_low_beam, LEDoff);
  390. digitalWrite(fog_light_low_beam_indicator, LEDoff);
  391. all_the_fog_lights_are_off = ENABLED;
  392. lightState = LIGHTS_OFF;
  393. Serial.println("all OFF");
  394. break;
  395.  
  396. case POLICE_LIGHTS:
  397. lightState = POLICE_LIGHTS;
  398. break;
  399.  
  400. }//switch case
  401.  
  402. inLongPress = false;//this was set true in void on_off_long_press_start(), and set false also in void on_off_long_press_stop()
  403. }//if
  404. }//on_off_long_press_stop()
  405.  
  406. void on_off_long_press_start()
  407. {
  408. // record time of the long press
  409. //begin my add edit
  410. //on_off_long_press_start_time = millis();//don't need millis here because of switch_turn_on_the_high_beam_fog_lights.setPressTicks(2500); // a long press will be detected after 2500 ms
  411. //end my add edit
  412. inLongPress = true;
  413. } //on_off_long_press_start
  414.  
  415. void on_off_long_press_stop()
  416. {
  417. inLongPress = false;
  418. }
  419.  
  420.  
  421. void blink_click()
  422. {
  423.  
  424. //this blinking bool variable, toggles "blinking" back and forth on each run of the blink_click() function
  425. blinking = !blinking;
  426.  
  427. // if we are done blinking, make sure we leave the lights on
  428. if ( blinking == DISABLED )//if blinking is DISABLED do nothing
  429. {
  430.  
  431. Serial.println("blinking");
  432. Serial.println(blinking);
  433. if ( lightState == LIGHTS_BOTH )
  434. {
  435. Serial.println("blinking");
  436. Serial.println(blinking);
  437. digitalWrite(fog_light_high_beam, LEDon);
  438. digitalWrite(fog_light_high_beam_indicator, LEDon);
  439. }//if
  440.  
  441. //if the value of "lightState" equals the value of "LIGHTS_HIGH_BEAM" "OR" the value of "lightState" equals the value of "LIGHTS_BOTH"
  442. if ( lightState == LIGHTS_LOW_BEAM || lightState == LIGHTS_BOTH )
  443. {
  444. // toggle low beams
  445. digitalWrite(fog_light_low_beam, LEDon);
  446. digitalWrite(fog_light_low_beam_indicator, LEDon);
  447. }//if
  448. }//blinking DISABLED
  449. }//blink_click()
  450.  
  451.  
  452. void blink_the_fog_lights()
  453. {
  454. //if lighrState = LIGHTS_OFF bail out, do not blink
  455. if ( lightState == LIGHTS_OFF )
  456. {
  457. // lights are off so nothing to do
  458. return;
  459. }//if
  460. //using millis() here
  461. if (currentMillis - previousMillis >= blinkInterval)
  462. {
  463. // enough time passed yet?
  464. previousMillis = currentMillis; // sets the time we wait "from"
  465. if ( lightState == LIGHTS_BOTH )
  466. {
  467. // toggle high beams
  468. digitalWrite(fog_light_high_beam, !digitalRead(fog_light_high_beam)); // shortcut to toggle the LED
  469. digitalWrite(fog_light_high_beam_indicator, !digitalRead(fog_light_high_beam_indicator)); // shortcut to toggle the LED
  470. }//if
  471.  
  472. //if the value of "lightState" equals the value of "LIGHTS_LOW_BEAM" "OR" the value of "lightState" equals the value of "LIGHTS_BOTH"
  473. if ( lightState == LIGHTS_LOW_BEAM || lightState == LIGHTS_BOTH )
  474. {
  475. // toggle low beams
  476. digitalWrite(fog_light_low_beam, !digitalRead(fog_light_low_beam)); // shortcut to toggle the LED
  477. digitalWrite(fog_light_low_beam_indicator, !digitalRead(fog_light_low_beam_indicator)); // shortcut to toggle the LED
  478. }//if
  479. }//if
  480. }//blink_the_fog_lights()
  481.  
  482. // this function will be called when the button was pressed 2 times in a short timeframe.
  483. void doubleclick()
  484. {
  485. if (!all_the_fog_lights_are_off)//all the fog lights are NOT off, so continue to run the police lights
  486. {
  487. if (!start_police_lights_now)//police lights are on
  488. {
  489. start_police_lights_now = ENABLED;
  490. Short_Button_Press_Allowed = DISABLED;
  491. }
  492. else//police lights are off
  493. {
  494. start_police_lights_now = DISABLED;
  495. Short_Button_Press_Allowed = ENABLED;
  496. if (lightState = POLICE_LIGHTS)
  497. {
  498. digitalWrite(fog_light_high_beam, LEDoff);
  499. digitalWrite(fog_light_high_beam_indicator, LEDoff);
  500. digitalWrite(fog_light_low_beam, LEDon);
  501. digitalWrite(fog_light_low_beam_indicator, LEDon);
  502. lightState = LIGHTS_LOW_BEAM;//go back to short press LIGHTS_LOW_BEAM
  503. } //if
  504. }//else
  505. } //all_the_fog_lights_are_off
  506. } //end doubleclick
  507.  
  508.  
  509. //Police
  510. void run_police_lights()
  511. {
  512. if(start_police_lights_now)
  513. {
  514. Serial.println("the police lights are on");
  515. // LED blink with millis()
  516.  
  517. if( LEDState_1 == HIGH )
  518. {
  519. if( (millis() - rememberTime_1) >= onDuration_1)
  520. {
  521. LEDState_1 = LOW;// change the state of LED
  522. rememberTime_1 = millis();// remember Current millis() time
  523. }//if
  524. }
  525. else
  526. {
  527. if( (millis()- rememberTime_1) >= offDuration_1)
  528. {
  529. LEDState_1 = HIGH;// change the state of LED
  530. rememberTime_1 = millis();// remember Current millis() time
  531. }//if else
  532. }//if
  533.  
  534. // LED blink with millis()
  535. digitalWrite(fog_light_low_beam,LEDState_1);// turn the LED ON or OFF
  536. digitalWrite(fog_light_low_beam_indicator,LEDState_1);// turn the LED ON or OFF
  537.  
  538. if( LEDState_2 ==LOW )
  539. {
  540. if( (millis() - rememberTime_2) >= onDuration_2)
  541. {
  542. LEDState_2 = HIGH;// change the state of LED
  543. rememberTime_2 = millis();// remember Current millis() time
  544. }
  545.  
  546. }
  547. else
  548. {
  549. if( (millis()- rememberTime_2) >= offDuration_2)
  550. {
  551. LEDState_2 = LOW;// change the state of LED
  552. rememberTime_2 = millis();// remember Current millis() time
  553. }//if
  554. }//if else
  555.  
  556. // LED blink with millis()
  557. digitalWrite(fog_light_high_beam,LEDState_2);// turn the LED ON or OFF
  558. digitalWrite(fog_light_high_beam_indicator,LEDState_2);// turn the LED ON or OFF
  559. }
  560. else
  561. {
  562. if (!start_police_lights_now)
  563. {
  564. digitalWrite(fog_light_high_beam,LEDoff);
  565. digitalWrite(fog_light_high_beam_indicator,LEDoff);
  566. Short_Button_Press_Allowed = ENABLED;
  567. }//if
  568. }//if else
  569.  
  570. } //end void
  571.  
  572.  
  573. void run_police_lights_2()
  574. {
  575.  
  576. if(start_police_lights_now)
  577. {
  578. if (timerEvent(stage1))
  579. {
  580. for (auto LedPinStage1 : LedPinsStage1) toogleLedPin(LedPinStage1);
  581. resetTimer(stage1);
  582. static int counter=0;
  583. if(!(++counter%=numberOfFlashs1<<1)) startTimer(stage2),stoppTimer(stage1);
  584. }
  585. if (timerEvent(stage2))
  586. {
  587. startTimer(stage3),stoppTimer(stage2);
  588. }
  589. if (timerEvent(stage3))
  590. {
  591. for (auto LedPinStage3 : LedPinsStage3) toogleLedPin(LedPinStage3);
  592. resetTimer(stage3);
  593. static int counter=0;
  594. if(!(++counter%=numberOfFlashs3<<1)) startTimer(stage1),stoppTimer(stage3);
  595. }
  596. }
  597. else
  598. {
  599. if (!start_police_lights_now)
  600. {
  601. digitalWrite(fog_light_high_beam,LEDoff);
  602. digitalWrite(fog_light_high_beam_indicator,LEDoff);
  603. Short_Button_Press_Allowed = ENABLED;
  604. }//if
  605. }//else
  606.  
  607. }//run_police_lights_2
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614. //=======================================================================================================================================================
  615. //BEGIN: run_police_lights_3 code
  616. //=======================================================================================================================================================
  617.  
  618.  
  619.  
  620. void switchState()
  621. {
  622. switch(state)
  623. {
  624. case 1: // idle
  625. //if(digitalRead(TRIGGER_PIN) == LOW) transition = 12;//original if you want to use button press
  626. //BEGIN my add edit
  627. //don't need a button press here if you just call transition = 12;
  628. start_police_lights_now = ENABLED;
  629. transition = 12;//sets blink_enabled to true, starts the police blinking
  630. //END my add edit
  631. break;
  632. case 2: // blinking for ON_TIME seconds check the time on
  633. if(millis() > time_to_switch_off) transition = 21;
  634. break;
  635. }//switch(state)
  636. }//void switchState
  637. //
  638.  
  639. void switchtransition()
  640. {
  641. if (start_police_lights_now)
  642. {
  643. switch(transition)
  644. {
  645. case 12:
  646. blink_enabled = 1;
  647. time_to_switch_off = millis() + (unsigned long)BLINK_TIME * 1000;
  648. Serial.println("Triggered, blinking started");
  649. transition = 0;
  650. state = 2;//checks the millis time left for blinking
  651. break;
  652. case 21://time ran out stop blinking and turn off led's
  653. blink_enabled = 0;
  654. time_to_change = 0;
  655. for(n=0; n<NUM_LEDS; n++)
  656. digitalWrite(led_pin[n], LOW);
  657. Serial.println("Time is up, blinking stopped");
  658. transition = 0;
  659. state = 1;//checks for a button press if there is a button, I eliminated the button
  660. break;
  661. }//switch(transition)
  662.  
  663. }//start_police_lights_now
  664.  
  665. }//void switchtransition()
  666.  
  667. //
  668. void Police_Lights_Blink_3()
  669. {
  670. if (start_police_lights_now)
  671. {
  672. //blinks the led's
  673. if (blink_enabled)
  674. {
  675. if (millis() > time_to_change)
  676. {
  677. time_to_change = millis() + (unsigned long)BLINK_SPEED;
  678. for(n=0; n<NUM_LEDS; n++)
  679. {
  680. digitalWrite(led_pin[n], sequencer[n][p]);
  681. }
  682. p = (p + 1) % NUM_STEPS;
  683. }//if
  684. }
  685. }
  686. else//start_police_lights_now = DISABLED turn on the low beam fog lights only
  687. {
  688. blink_enabled = DISABLED;
  689. start_police_lights_now = DISABLED;
  690. }//else
  691.  
  692. }//Police_Lights_Blink_3()
  693.  
  694. //=======================================================================================================================================================
  695. //END: run_police_lights_3 code
  696. //=======================================================================================================================================================
  697.  
  698.  
  699.  
  700.  
  701. Retrieved from John's Media Wiki
  702. title=My_Working_03_02_2022_Working_Harley_Fog_Lights_With_Police_Lights_LED_Array_for_loop&oldid=35534"
  703. This page was last modified on 15 March 2022, at 12:08.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement