Go Down

Topic: Consistent flash on button press (Button + FlashWithoutDelay) (Read 870 times) previous topic - next topic

Henry_Best



When the button checks have previousMillis in like this, the LED is constantly at half brightness when a button is held.


Looks to me like the error is in your button reading routine.
Each time round the loop, you need to check if the button state has changed from the previous time. At the moment, keeping the button pressed resets everything each time round the loop, hence the dimmed ( = rapid flashing) LED.
You need a 'ButtonsLastTime' variable that you check against 'ButtonsThisTime'. If they are the same, don't reset anything.

PaulS

You need to remove the reset of previousMillis when pressed is true.

Your REAL requirement, as is becoming clear, is not just to start flashing the LED when the switch is pressed, but to immediately start flashing the LED with the LED on to start with.

To do this, you need to take some action when the switch is released:
  else
  {
    pressed = false;     // if no button is pressed set boolean to false
    previousMillis = 0;
    strobeState = LOW;

  }

Then, when the switch is pressed, the LED will be off, and the time it was last turned on will be 0, so:
    if(currentMillis - previousMillis > interval || previousMillis == 0) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      strobeState = !strobeState;     //toggle
      digitalWrite(ledPin, strobeState);
    }

This will then change the state of the LED immediately, since previousMillis is 0. It will change the LED pin to HIGH, since the state was LOW.

Then, since previousMillis is no longer 0, the regular blinking will happen, on schedule.

A lot of this would have been avoided had you been able to state your requirements clearly from the beginning. Don't worry about that, though. That is one of the hardest parts of application development - getting the requirements right. Making the code match the requirements is trivial, by comparison.

Jebimasta

That's it! that has done it  :) just curious, what do the verticals in the if clause mean?

Now starters is over, it's time for the main course! I'm going to attempt to use the tenth button as a "one shot" press and the strobe will fire once, or press it in combination with another button to get a burst of flashes.

Thanks again, hopefully I can use what I've learned from the rest of the thread to put this into action.

PaulS

Quote
just curious, what do the verticals in the if clause mean?

|| is the logical OR
http://arduino.cc/en/Reference/Boolean

Quote
I'm going to attempt to use the tenth button as a "one shot" press and the strobe will fire once, or press it in combination with another button to get a burst of flashes.

For that, you WILL need to detect when it transitions from released to pressed. See the state change detection example.

Quote
hopefully I can use what I've learned from the rest of the thread to put this into action.

Especially the part about being as clear as possible when defining requirements.

Jebimasta

Certainly the clarity, I'll try and give more detail next time. It does seem like I'm being very demanding or boring with the details though.

PaulS

Quote
It does seem like I'm being very demanding or boring with the details though.

Not to me, it doesn't. Wishy-washy is far worse.

Henry_Best


Certainly the clarity, I'll try and give more detail next time. It does seem like I'm being very demanding or boring with the details though.


1) We're not mindreaders.
2) You'll only get the correct answer if the question is correct.


PaulS

Quote
1) We're not mindreaders.

I knew you were going to say that.  8)

Well, that somebody was, anyway.

Go Up