LongPress and ShortPress Button events
I'm trying to do call nextColor() on a longpress of the button and change the brightness on a shortPress.
This code calls nextColor() even though the button has not been pressed.
If I change the HIGH to LOW the longpress works, but then the brightness is set higher on start.
Can you help me understand what I'm missing?
void loop() {
// this is the main loop where we call the other functions.
int pressLength=0;
while (digitalRead(BUTTON_PIN) == HIGH && pressLength < 10)
{
delay(100);
pressLength++;
}
if (pressLength < 10) {
BRIGHT_LEVEL+=20;
LEDS.setBrightness(BRIGHT_LEVEL);
} else {
nextColor();
}
}
Thank you!
P.s. Its been years now... how about we upgrade to a forum that render code.
This code calls nextColor() even though the button has not been pressed.
If I change the HIGH to LOW the longpress works, but then the brightness is set higher on start.
Can you help me understand what I'm missing?
void loop() {
// this is the main loop where we call the other functions.
int pressLength=0;
while (digitalRead(BUTTON_PIN) == HIGH && pressLength < 10)
{
delay(100);
pressLength++;
}
if (pressLength < 10) {
BRIGHT_LEVEL+=20;
LEDS.setBrightness(BRIGHT_LEVEL);
} else {
nextColor();
}
}
Thank you!
P.s. Its been years now... how about we upgrade to a forum that render code.
Comments
if(digitalRead(BUTTON_PIN)==LOW) {
int pressLength=0;
while (digitalRead(BUTTON_PIN) == HIGH && pressLength < 10) { delay(100);
pressLength++;
}
if (pressLength < 10) {
BRIGHT_LEVEL+=20;
LEDS.setBrightness(BRIGHT_LEVEL);
} else {
nextColor();
}
}
We are doing something similar in the production firmware, however I implemented it using interrupts instead of digitalRead() to make it a little more responsive.
And yeah, good call about the forums. Vanilla doesn't seem to be evolving as fast as we had hoped. I'll see if there is a code formatting plugin though.
I'm looking at the code now.
I get what you're describing and in another sample I built I used your logic to do 1 thing after a button press.
void loop() {
// Check if the button has been pushed
uint8_t buttonState = digitalRead(BUTTON_PIN);
if((buttonState != lastButtonState) && (buttonState == 0)) {
nextColor();
}
lastButtonState = buttonState;
}
When I use the code in your previous suggestion, or when I insert my WHILE loop into the above code, what happens is the BlinkyTape blinks once and then nothing. When I hold the button down it will blink a color, I then let up hold it down again and it blinks a new color. No change in brightness and the BlinkyTape does not stay lit.
I get that the problem is I'm not quite understanding how the Loop works. Thanks for the help.
Hey Line Returns work now. ;-)
Let's try some test markdown right from the link they provide.
Heading
Sub-heading
Another deeper heading
Paragraphs are separated
by a blank line.
Leave 2 spaces at the end of a line to do a
line break
Text attributes italic, bold,
monospace
, strikethrough .A link.
[28]
Shopping list:
Numbered list:
The rain---not the reign---in
Spain.
hmm... that's sad. :-(
Vanilla Forums is a fitting name. ;-)
At least the emoji work now I'm not giving up on getting this enabled, yet.
I love the dedication Matt. Thank you!!
OK... back to the original question.
While we work out the markdown... here's a gist of my code.
https://gist.github.com/W7PEA/fc92e0083faeed05900b
I added a runPattern(); to my loop which calls LEDS.ShowColor() and LEDS.SHOW with the for the currentColor in my set of colors.
My nextColor() and cycleBrightness() work on the long and short presses.
!! YAY !!
BUT, the light looks like its blinking rapidly instead of showing a steady light...
Any thoughts?
thanks!
Figured it out. My button logic was perfect. The problem was calling LEDS.show(); so many times. I updated the code in the gist, its now working.
This program will change the brightness on a short press and the color on a longpress. It goes through RED, BLUE and WHITE. I plan to use this as a lamp in my tent. Here's the link again: https://gist.github.com/W7PEA/fc92e0083faeed05900b
Ah, glad to hear you got it working! Sounds like a cool project