Pages: 1 [2]   Go Down
Author Topic: Consistent flash on button press (Button + FlashWithoutDelay)  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's it! that has done it  smiley 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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
1) We're not mindreaders.
I knew you were going to say that.  smiley-cool

Well, that somebody was, anyway.
Logged

Pages: 1 [2]   Go Up
Jump to: