Go Down

Topic: [solved] Momentary Pushbutton with pulsing LED (Read 244 times) previous topic - next topic

Tissi_2

Mar 15, 2017, 06:24 pm Last Edit: Mar 19, 2017, 05:29 pm by Tissi_2
Hi folks,

I am getting mad with this  >:( I am trying to realize a pushbutton with following features:

powerup of arduino --> LED is off
pushed and released button --> LED is on an pulsing
pushed again --> LED is off again.

Here is my code.

Code: [Select]
const uint32_t debounceTime = 10;
const uint8_t switchPin     = 8;
const uint8_t ledPin        = 3;
int brightness = 200;
int fadeAmount = 5;
long currentTime = 0;

const bool switchOn  = false;
const bool switchOff = true;

bool lastState   = switchOff;
bool newState    = switchOff;
bool toggleState = false;

void setup()
{
 pinMode ( switchPin, INPUT_PULLUP );
 pinMode ( ledPin, OUTPUT );
 }

void loop ()
{
  newState = digitalRead( switchPin );

  if( lastState != newState )
  {
    delay( debounceTime );
    lastState = newState;
  
    // push on, push off
    if( newState == switchOn && toggleState == false )
    {
      toggleState = true;
      analogWrite( ledPin, brightness );
      /*do {
          brightness = brightness + fadeAmount;
            if (brightness == 20 || brightness == 255) {
                fadeAmount = -fadeAmount ;}  
                analogWrite( ledPin, brightness );
                delay (30);
               }  
      while ( newState == switchOn);*/
    }      
    else if( newState == switchOn && toggleState == true )
    {
      toggleState = false;
      digitalWrite( ledPin, LOW );
    }
  }
}

 

If I use the code without the "pulsing function" the button led goes on and off as wanted. I believe that the "delay" function could be the reason why the next push of the button is not recognized anymore. Any ideas to realize this maybe with "millis" to avoid the delay? Thank you for any suggestions!

Tissi

PaulRB

You have correctly diagnosed the problem! Also it looks like your led will switch on and off rather than fade on and off. You must get rid of the delay.

PaulRB

Code: [Select]

const uint32_t debounceTime = 10;
const uint8_t switchPin     = 8;
const uint8_t ledPin        = 3;
int brightness = 200;
int fadeAmount = 5;
long currentTime = 0;

const bool switchOn  = false;
const bool switchOff = true;

bool lastState   = switchOff;
bool newState    = switchOff;
bool toggleState = false;

unsigned long lastFadeTime;

void setup() {
  pinMode ( switchPin, INPUT_PULLUP );
  pinMode ( ledPin, OUTPUT );
}

void loop () {
  newState = digitalRead( switchPin );

  if ( lastState != newState ) {
    delay( debounceTime );
    lastState = newState;

    // push on, push off
    if ( newState == switchOn && toggleState == false ) {
      toggleState = true;
    }
    else if ( newState == switchOn && toggleState == true ) {
      toggleState = false;
      digitalWrite( ledPin, LOW );
    }
  }

  if (toggleState  && lastFadeTime - millis() > 30) {
    brightness = brightness + fadeAmount;
    if (brightness == 20 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }
    analogWrite( ledPin, brightness );
    lastFadeTime = millis();
  }

}

Tissi_2

 :smiley-eek:

Thank you very much, Paul! The code works perfectly and you made me happy! I changed the speed of the brigthness change by using a delay() so that the LED pulses a little bit slower. I donĀ“t need the int lastFadeTime then:

Code: [Select]
if (toggleState) {
    brightness = brightness + fadeAmount;
    delay(30);
    if (brightness == 20 || brightness == 255) {
      fadeAmount = -fadeAmount ;   
    }
    analogWrite( ledPin, brightness );
    lastFadeTime = millis();
  }


When I saw the code I just thought: well, it is that simple?! Sometimes it is like not being able to see the wood for trees. Once again - thank you very much!!!

PaulRB

I changed the speed of the brigthness change by using a delay() so that the LED pulses a little bit slower.
That would be a backward step. To slow the pulses, try a lower figure for fadeAmount, and/or increase the value "30".

Tissi_2

Solved the problem!

Code: [Select]
if (toggleState  &&  millis()-lastFadeTime> 30) {
    brightness = brightness + fadeAmount;
    if (brightness == 20 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }
    analogWrite( ledPin, brightness );
    lastFadeTime = millis();
  }


millis() and lastFadeTime were mixed up.

PaulRB

#6
Mar 19, 2017, 07:03 pm Last Edit: Mar 19, 2017, 07:04 pm by PaulRB
Solved the problem!
millis() and lastFadeTime were mixed up.
Excellent, well spotted. Karma++

Go Up