Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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.

Comments

  • Interesting. The button on the blinkytape is active low, which means it is normally HIGH when the button isn't being pressed, and LOW when the button is pressed. The design looks good, it looks like you might be missing a check to skip the whole counting loop if the button wasn't pressed- maybe put the while loop and if/else statements in a new if() section that only runs if the button is pressed (I'm on my phone so apologies if there is a syntax error here):

    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.
  • RE: Vanilla Forums. Are you on the latest? Looks like it supports Markdown now which I think would be great for this community. https://vanillaforums.com/features/user-experience

    I'm looking at the code now.

  • OK, work over. Play begin.

    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. ;-)
  • edited December 2015
    Ok, I've updated to Vanilla 2.2 and enabled the new Advanced Editor. Now trying to get markdown support to work :smile:
  • 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](http://example.com). [28] Shopping list: * apples * oranges * pears Numbered list: 1. apples 2. oranges 3. pears The rain---not the reign---in Spain.
  • hmm... that's sad. :-( Vanilla Forums is a fitting name. ;-)
  • At least the emoji work now :D 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 :smile:
Sign In or Register to comment.