Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //st3v3n92 Arduino Forum https://forum.arduino.cc/t/trying-to-blink-4-leds-with-two-buttons/934148/35
- #include "KTS_Button.h"
- #define ARRSIZE(x) sizeof(x) / sizeof(x[0])
- #define BLINK_INTERVAL 100
- #define LONG_PRESS_TIMEOUT 2500 //2.5 seconds
- /*
- Yes.. physical pin 6 is GPIO pin 4...
- but that doesn't mean you need to change your code -
- your code ALWAYS refers to the GPIO PIN number.
- You just need to know that GPIO pin 4...
- is physical pin 6 on the ATMEGA328 when you are wiring your circuit.
- So you would connect your button to pin 6...
- but you still refer to it in the code as pin 4.
- So these will stay the same...
- /*
- // working new pin layout for arduino uno
- // new pin layout for arduino uno
- #define BTN_ONE_PIN 4 //the blinker button
- #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
- //LED's
- #define LED_LOW_L 6//halo fog light low beam
- #define LED_LOW_R 7//halo fog light low beam
- #define LED_HIGH_L 8 //high beam
- #define LED_HIGH_R 9//high beam
- //==================================================
- */
- //================================================
- // below working new pin layout for Arduino UNO
- //================================================
- #define BTN_ONE_PIN 4 //the blinker button - ATMEGA328 pin 6
- #define BTN_TWO_PIN 5 //the high beam fog light turn on button - ATMEGA328 pin 11
- //LED's
- #define LED_LOW_L 6 //halo fog light low beam - ATMEGA328 pin 12
- #define LED_LOW_R 7 //halo fog light low beam - ATMEGA328 pin 13
- #define LED_HIGH_L 8 //high beam - ATMEGA328 pin 14
- #define LED_HIGH_R 9//high beam - ATMEGA328 pin 15
- /*
- #define BTN_ONE_PIN 8
- #define BTN_TWO_PIN 5
- #define LED_LOW_L 6
- #define LED_LOW_R 7
- #define LED_HIGH_L 10
- #define LED_HIGH_R 11
- */
- KTS_Button buttons[] = { BTN_ONE_PIN, BTN_TWO_PIN };
- byte leds[] = { LED_LOW_L, LED_LOW_R, LED_HIGH_L, LED_HIGH_R };
- void setup() {
- for (byte i = 0; i < ARRSIZE(leds); i++)
- pinMode(leds[i], OUTPUT);
- buttons[1].setLongPressTimeout(LONG_PRESS_TIMEOUT);
- }
- void loop() {
- static uint32_t timeCapture;
- static bool highBeam = false;
- static bool lightsOn = true;
- ActionType btnOneAction = buttons[1].read();
- if (btnOneAction == SINGLE_PRESS && lightsOn)
- highBeam = !highBeam;
- else if (btnOneAction == LONG_PRESS)
- lightsOn = !lightsOn;
- if (buttons[0].isHeld() && lightsOn) {
- if ((millis() - timeCapture) > BLINK_INTERVAL) {
- timeCapture = millis();
- for (byte i = 0; i < (highBeam ? 4 : 2); i++)
- digitalWrite(leds[i], !digitalRead(leds[i]));
- }
- }
- else {
- for (byte i = 0; i < ARRSIZE(leds); i++) {
- if (i < 2)
- digitalWrite(leds[i], lightsOn);
- else
- digitalWrite(leds[i], highBeam && lightsOn);
- }
- }
- }
- ======================================================================================================
- #ifndef KTS_BUTTON_H
- #define KTS_BUTTON_H
- enum ActionType {
- NOTHING = 0,
- SINGLE_PRESS,
- LONG_PRESS,
- DOUBLE_PRESS,
- TRIPLE_PRESS,
- };
- class KTS_Button {
- // Private Variables
- private:
- byte inputPin;
- byte currentState = HIGH;
- byte lastState = HIGH;
- //testing here
- //byte currentState = LOW;
- //byte lastState = LOW;
- unsigned long currentTime;
- unsigned long timeCapture;
- unsigned long debounceLength;
- unsigned long longPressTimeout;
- unsigned long multiPressTimeout;
- bool longPressEnabled = true;
- byte pressCount;
- void (*btnFuncPtr)(void);
- // Public Variables - NONE
- // Constructor
- public:
- KTS_Button(int pin, unsigned long dbLength = 5, unsigned long lpTimeout = 350, unsigned long mtTimeout = 150)
- : inputPin(pin),
- debounceLength(dbLength),
- longPressTimeout(lpTimeout),
- multiPressTimeout(mtTimeout)
- { pinMode(inputPin, INPUT_PULLUP); }
- // Public Modifiers
- void setLongPressEnabled(bool newState) { longPressEnabled = newState; }
- void setDebounceLength(unsigned long newDBLength) { debounceLength = newDBLength; }
- void setLongPressTimeout(unsigned long newLPTimeout) { longPressTimeout = newLPTimeout; }
- void setButtonFunction(void (*func)(void)) { btnFuncPtr = func; }
- bool isHeld() { return digitalRead(inputPin) == LOW; }
- // Public Functions
- void execute(){ btnFuncPtr();}
- ActionType read() {
- updateButton();
- if (buttonHasChanged()) {
- if (buttonIsDown()) captureTime();
- else if (timeCapture && hasBeenDebounced()) {
- timeCapture = 0;
- return SINGLE_PRESS;
- }
- else timeCapture = 0;
- }
- if (longPressEnabled && timeCapture && aboveLPTimeout()) {
- timeCapture = 0;
- return LONG_PRESS;
- }
- return NOTHING;
- } // END ActionType Read()
- ActionType readMultiPress() {
- updateButton();
- if (buttonHasChanged()) {
- if (buttonIsDown()) captureTime();
- else setLongPressEnabled(true);
- if (hasBeenDebounced()) {
- if (belowLPTimeout()) { pressCount++; }
- }
- }
- if (longPressEnabled && buttonIsDown() && aboveLPTimeout()) {
- pressCount = timeCapture = 0;
- setLongPressEnabled(false);
- return LONG_PRESS;
- }
- if (aboveDPTimeout()) {
- switch (pressCount) {
- case 1: pressCount = timeCapture = 0; return SINGLE_PRESS; break;
- case 2: pressCount = timeCapture = 0; return DOUBLE_PRESS; break;
- case 3: pressCount = timeCapture = 0; return TRIPLE_PRESS; break;
- default: pressCount = 0; break;
- }
- }
- return NOTHING;
- } // END ActionType Read_MultiPress()
- private:
- // Private Functions
- bool buttonIsDown() { return currentState == LOW; }
- bool buttonHasChanged() { return currentState != lastState; }
- void updateStates() { lastState = currentState; }
- bool hasBeenDebounced() { return currentTime - timeCapture > debounceLength; }
- bool aboveLPTimeout() { return currentTime - timeCapture > longPressTimeout; }
- bool belowLPTimeout() { return currentTime - timeCapture < longPressTimeout; }
- bool aboveDPTimeout() { return currentTime - timeCapture > multiPressTimeout; }
- void captureTime() { timeCapture = currentTime; }
- void updateButton() {
- if (buttonHasChanged()) updateStates();
- currentTime = millis();
- currentState = digitalRead(inputPin);
- }
- }; // END class button
- #endif
- // ActionType ReadWithoutTimeout() {
- // updateButton();
- // if (buttonHasChanged())
- // {
- // if (buttonIsDown()) UpdateTimeCapture();
- // if (hasBeenDebounced()) {
- // if (belowLPTimeout()) return SINGLE_PRESS;
- // if (aboveLPTimeout()) return LONG_PRESS;
- // }
- // }
- // return NOTHING;
- // } END ActionType ReadWithoutTimeout()
Advertisement
Add Comment
Please, Sign In to add comment