stspringer

blink fog lights

Jan 6th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. //st3v3n92 Arduino Forum https://forum.arduino.cc/t/trying-to-blink-4-leds-with-two-buttons/934148/35
  2.  
  3.  
  4. #include "KTS_Button.h"
  5. #define ARRSIZE(x) sizeof(x) / sizeof(x[0])
  6.  
  7. #define BLINK_INTERVAL 100
  8. #define LONG_PRESS_TIMEOUT 2500 //2.5 seconds
  9. /*
  10. Yes.. physical pin 6 is GPIO pin 4...
  11. but that doesn't mean you need to change your code -
  12. your code ALWAYS refers to the GPIO PIN number.
  13.  
  14. You just need to know that GPIO pin 4...
  15. is physical pin 6 on the ATMEGA328 when you are wiring your circuit.
  16. So you would connect your button to pin 6...
  17. but you still refer to it in the code as pin 4.
  18.  
  19. So these will stay the same...
  20.  
  21. /*
  22. // working new pin layout for arduino uno
  23. // new pin layout for arduino uno
  24. #define BTN_ONE_PIN 4 //the blinker button
  25. #define BTN_TWO_PI;[[image:Pointing_Finger_Right_With_Note_2.jpg]]<span style="font-size:150%">'''<span style="color:red;">This is the script code you may modifyN 5 //the high beam fog light turn on switch
  26. //LED's
  27. #define LED_LOW_L 6//halo fog light low beam
  28. #define LED_LOW_R 7//halo fog light low beam
  29. #define LED_HIGH_L 8 //high beam
  30. #define LED_HIGH_R 9//high beam
  31. //==================================================
  32. */
  33.  
  34.  
  35.  
  36. //================================================
  37. // below working new pin layout for Arduino UNO
  38. //================================================
  39. #define BTN_ONE_PIN 4 //the blinker button - ATMEGA328 pin 6
  40. #define BTN_TWO_PIN 5 //the high beam fog light turn on button - ATMEGA328 pin 11
  41. //LED's
  42. #define LED_LOW_L 6 //halo fog light low beam - ATMEGA328 pin 12
  43. #define LED_LOW_R 7 //halo fog light low beam - ATMEGA328 pin 13
  44. #define LED_HIGH_L 8 //high beam - ATMEGA328 pin 14
  45. #define LED_HIGH_R 9//high beam - ATMEGA328 pin 15
  46.  
  47.  
  48.  
  49. /*
  50. #define BTN_ONE_PIN 8
  51. #define BTN_TWO_PIN 5
  52. #define LED_LOW_L 6
  53. #define LED_LOW_R 7
  54. #define LED_HIGH_L 10
  55. #define LED_HIGH_R 11
  56. */
  57.  
  58. KTS_Button buttons[] = { BTN_ONE_PIN, BTN_TWO_PIN };
  59. byte leds[] = { LED_LOW_L, LED_LOW_R, LED_HIGH_L, LED_HIGH_R };
  60.  
  61. void setup() {
  62. for (byte i = 0; i < ARRSIZE(leds); i++)
  63. pinMode(leds[i], OUTPUT);
  64. buttons[1].setLongPressTimeout(LONG_PRESS_TIMEOUT);
  65.  
  66. }
  67.  
  68. void loop() {
  69. static uint32_t timeCapture;
  70. static bool highBeam = false;
  71. static bool lightsOn = true;
  72.  
  73. ActionType btnOneAction = buttons[1].read();
  74.  
  75. if (btnOneAction == SINGLE_PRESS && lightsOn)
  76. highBeam = !highBeam;
  77. else if (btnOneAction == LONG_PRESS)
  78. lightsOn = !lightsOn;
  79.  
  80. if (buttons[0].isHeld() && lightsOn) {
  81. if ((millis() - timeCapture) > BLINK_INTERVAL) {
  82. timeCapture = millis();
  83. for (byte i = 0; i < (highBeam ? 4 : 2); i++)
  84. digitalWrite(leds[i], !digitalRead(leds[i]));
  85. }
  86. }
  87. else {
  88. for (byte i = 0; i < ARRSIZE(leds); i++) {
  89. if (i < 2)
  90. digitalWrite(leds[i], lightsOn);
  91. else
  92. digitalWrite(leds[i], highBeam && lightsOn);
  93. }
  94. }
  95. }
  96.  
  97. ======================================================================================================
  98. #ifndef KTS_BUTTON_H
  99. #define KTS_BUTTON_H
  100.  
  101. enum ActionType {
  102. NOTHING = 0,
  103. SINGLE_PRESS,
  104. LONG_PRESS,
  105. DOUBLE_PRESS,
  106. TRIPLE_PRESS,
  107. };
  108.  
  109. class KTS_Button {
  110.  
  111. // Private Variables
  112. private:
  113. byte inputPin;
  114. byte currentState = HIGH;
  115. byte lastState = HIGH;
  116. //testing here
  117. //byte currentState = LOW;
  118. //byte lastState = LOW;
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. unsigned long currentTime;
  126. unsigned long timeCapture;
  127. unsigned long debounceLength;
  128. unsigned long longPressTimeout;
  129. unsigned long multiPressTimeout;
  130. bool longPressEnabled = true;
  131. byte pressCount;
  132. void (*btnFuncPtr)(void);
  133.  
  134. // Public Variables - NONE
  135.  
  136. // Constructor
  137. public:
  138.  
  139. KTS_Button(int pin, unsigned long dbLength = 5, unsigned long lpTimeout = 350, unsigned long mtTimeout = 150)
  140. : inputPin(pin),
  141. debounceLength(dbLength),
  142. longPressTimeout(lpTimeout),
  143. multiPressTimeout(mtTimeout)
  144. { pinMode(inputPin, INPUT_PULLUP); }
  145.  
  146.  
  147. // Public Modifiers
  148. void setLongPressEnabled(bool newState) { longPressEnabled = newState; }
  149. void setDebounceLength(unsigned long newDBLength) { debounceLength = newDBLength; }
  150. void setLongPressTimeout(unsigned long newLPTimeout) { longPressTimeout = newLPTimeout; }
  151. void setButtonFunction(void (*func)(void)) { btnFuncPtr = func; }
  152. bool isHeld() { return digitalRead(inputPin) == LOW; }
  153.  
  154.  
  155.  
  156. // Public Functions
  157.  
  158. void execute(){ btnFuncPtr();}
  159.  
  160. ActionType read() {
  161. updateButton();
  162. if (buttonHasChanged()) {
  163. if (buttonIsDown()) captureTime();
  164. else if (timeCapture && hasBeenDebounced()) {
  165. timeCapture = 0;
  166. return SINGLE_PRESS;
  167. }
  168. else timeCapture = 0;
  169. }
  170. if (longPressEnabled && timeCapture && aboveLPTimeout()) {
  171. timeCapture = 0;
  172. return LONG_PRESS;
  173. }
  174. return NOTHING;
  175. } // END ActionType Read()
  176.  
  177. ActionType readMultiPress() {
  178. updateButton();
  179. if (buttonHasChanged()) {
  180. if (buttonIsDown()) captureTime();
  181. else setLongPressEnabled(true);
  182. if (hasBeenDebounced()) {
  183. if (belowLPTimeout()) { pressCount++; }
  184. }
  185. }
  186. if (longPressEnabled && buttonIsDown() && aboveLPTimeout()) {
  187. pressCount = timeCapture = 0;
  188. setLongPressEnabled(false);
  189. return LONG_PRESS;
  190. }
  191. if (aboveDPTimeout()) {
  192. switch (pressCount) {
  193. case 1: pressCount = timeCapture = 0; return SINGLE_PRESS; break;
  194. case 2: pressCount = timeCapture = 0; return DOUBLE_PRESS; break;
  195. case 3: pressCount = timeCapture = 0; return TRIPLE_PRESS; break;
  196. default: pressCount = 0; break;
  197. }
  198. }
  199. return NOTHING;
  200. } // END ActionType Read_MultiPress()
  201.  
  202.  
  203. private:
  204.  
  205. // Private Functions
  206. bool buttonIsDown() { return currentState == LOW; }
  207. bool buttonHasChanged() { return currentState != lastState; }
  208. void updateStates() { lastState = currentState; }
  209. bool hasBeenDebounced() { return currentTime - timeCapture > debounceLength; }
  210. bool aboveLPTimeout() { return currentTime - timeCapture > longPressTimeout; }
  211. bool belowLPTimeout() { return currentTime - timeCapture < longPressTimeout; }
  212. bool aboveDPTimeout() { return currentTime - timeCapture > multiPressTimeout; }
  213. void captureTime() { timeCapture = currentTime; }
  214.  
  215. void updateButton() {
  216. if (buttonHasChanged()) updateStates();
  217. currentTime = millis();
  218. currentState = digitalRead(inputPin);
  219. }
  220. }; // END class button
  221.  
  222. #endif
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. // ActionType ReadWithoutTimeout() {
  232. // updateButton();
  233. // if (buttonHasChanged())
  234. // {
  235. // if (buttonIsDown()) UpdateTimeCapture();
  236. // if (hasBeenDebounced()) {
  237. // if (belowLPTimeout()) return SINGLE_PRESS;
  238. // if (aboveLPTimeout()) return LONG_PRESS;
  239. // }
  240. // }
  241. // return NOTHING;
  242. // } END ActionType ReadWithoutTimeout()
  243.  
  244.  
  245.  
  246.  
Advertisement
Add Comment
Please, Sign In to add comment