Patrick398
New Member
I have a some long press/short press code which is working nicely, controlling a relay and an opto from a single button. I thought it would be nice to blink the indicator LED to show when the change has occurred but my indicator LED is on the same pin as the relay. I thought it would be easy enough to use a spare pin for the LED but here we all are, head it hands.
When the button is short pressed the relay is toggled. So i need my indicator LED to also toggle with it. When the button is held for 2.5 seconds the opto is toggled. So far so good. But i need the indicator LED to blink a few times and remain on. It should blink a few times every time the long press occurs but toggle on and off along with the relay. As it is i can't even get it to stay on when the opto pin is toggled. It feels like it should be so easy but i've been banging my head against the keyboard for hours now.
I should point out i know that the code as is won't do what i want, i've stripped it back to a point where it's mostly doing what i want but i've run out of ideas what to try next to get the blinking mode change
When the button is short pressed the relay is toggled. So i need my indicator LED to also toggle with it. When the button is held for 2.5 seconds the opto is toggled. So far so good. But i need the indicator LED to blink a few times and remain on. It should blink a few times every time the long press occurs but toggle on and off along with the relay. As it is i can't even get it to stay on when the opto pin is toggled. It feels like it should be so easy but i've been banging my head against the keyboard for hours now.
I should point out i know that the code as is won't do what i want, i've stripped it back to a point where it's mostly doing what i want but i've run out of ideas what to try next to get the blinking mode change
Code:
#include <ezButton.h>
const int buttonPin = 3; //2
int relayPin = 0;// 5
int optoPin = 1; //6
int ledPin = 2; // 7
const int SHORT_PRESS_TIME = 1000; // 1700 milliseconds
const int LONG_PRESS_TIME = 2500; // 2500 milliseconds
ezButton button(buttonPin); // create ezButton object that attach to pin 7;
int ledState ;
int optoState = LOW;
int relayState = LOW;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
void setup() {
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT_PULLUP);
pinMode (relayPin, OUTPUT);
pinMode (optoPin, OUTPUT);
button.setDebounceTime(30); // set debounce time to 30 milliseconds
digitalWrite (relayPin, relayState);
digitalWrite (optoPin, optoState);
digitalWrite (ledPin, ledState);
}
void loop() {
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
pressedTime = millis();
isPressing = true;
isLongDetected = false;
}
if (button.isReleased()) {
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if ( pressDuration < SHORT_PRESS_TIME ) {
relayState = !relayState;
digitalWrite(relayPin, relayState);
ledState = ! ledState;
digitalWrite (ledPin, ledState);
}
}
if (isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if ( pressDuration > LONG_PRESS_TIME ) {
optoState = ! optoState;
digitalWrite (optoPin, optoState);
isLongDetected = true;
}
}
}