PWM LED, then button push, then Separate LED sequence. Where am I going wrong?

Hello, I've been working on this for about a week now and I'm a little stuck. The sketch I'm trying to make uses PWM to fade on startup as long as a button is held down, then if button is pushed, flashes 2 LEDs a few times, then does nothing forever. Once button is pushed, it resumes fade.

I've been working on it for a week now. I seem to be able to get all separate pieces working, like just a fade on wake, just a flash on button push. But when I throw it all together I'm having trouble. Where am I going wrong?

Here's my code

// Thor Hammer sketch.
// This sketch fades on startup til button release, then flashes an LED, then does nothing. Once button is pushed, it resumes fade.




const int pulseLED = 9;      // the pin that the LED is attached to
const int flashled1 = 2;     // Naming the first flashing led pin
const int flashled2 = 3;     // Naming the second flashing led pin                               
const int buttonPin = 12;    // button pin. If pushed, then PWM fade, if released; flash
int brightness = 0;          // how bright the LED is
int fadeAmount = 5;          // how many points to fade the LED by
int buttonState = 0;         // current state of the button



void setup()
{  
  pinMode(pulseLED, OUTPUT);     // setting pulseLED to output
  pinMode(flashled1, OUTPUT);    // setting flashled1 to output
  pinMode(flashled2, OUTPUT);    // setting flashled2 to output
  pinMode(buttonPin, INPUT);     // set button pin to be an input
  digitalWrite(buttonPin, HIGH); // set pullup resistor to buttonPin
}


void loop()  

{ 
  {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH)               { 
  // set the brightness of pin 9:
  analogWrite(pulseLED, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
                                            }
else   {  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
  delay(20);
  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
  delay(20);
  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
        
        }
  }
}

I haven’t spent very long studying your code. It looks like it will cause fading while the button is pressed and flashing when it isn’t. is that what it actually does?

If that is not what you want, please explain more clearly wht you want to happen. It sounds like you want some other button press option but there is no sign of that in your code.

I suspect if you put the fading code in one function and the flashing code in another the structure of your program would be clearer to you.

…R

I'd like to have fading happen on a loop forever when I have a button held down, once the button is released I'd like it to flash a different LED a few times then stop forever. Once the button is held down again I'd like it to resume fading again.

So far when I hook it all up it fades on startup, but it doesn't seem to recognize the button push.

When I try your code the led fades when the button is NOT pressed (not pressed gives a HIGH output) and when the button is pressed (LOW output) the leds flash continuously and the fade pauses wherever it was. The fad resumes when the button is released.

If your button isn’t working perhaps you have the switch wired incorrectly. One end should go to pin 12 and the other to Ground.

…R

Ok so I did a few things, first I added a line on the flashing part that stops the LED fade, since it kept going during the flash.
I have my breadboard setup as: pin 12 to one side of a 1000 ohm resistor, then on the other side of the resistor goes to side 1 of the button. Then I have ground on board to side 2 of the button.

The code seems to be working... I think. I'm still not sure how to get it to blink only a few times then stop until the button is held down, then resume fade. It just either blinks forever or it goes back into fade.

Here's the new code:

// Thor Hammer sketch.
// This sketch fades on startup til button release, then flashes an LED, then does nothing. Once button is pushed, it resumes fade.




const int pulseLED = 9;      // the pin that the LED is attached to
const int flashled1 = 2;     // Naming the first flashing led pin
const int flashled2 = 3;     // Naming the second flashing led pin                               
const int buttonPin = 12;    // button pin. If pushed, then PWM fade, if released; flash
int brightness = 0;          // how bright the LED is
int fadeAmount = 5;          // how many points to fade the LED by
int buttonState = 0;         // current state of the button



void setup()
{  
  pinMode(pulseLED, OUTPUT);     // setting pulseLED to output
  pinMode(flashled1, OUTPUT);    // setting flashled1 to output
  pinMode(flashled2, OUTPUT);    // setting flashled2 to output
  pinMode(buttonPin, INPUT);     // set button pin to be an input
 // digitalWrite(buttonPin, HIGH); // set pullup resistor to buttonPin
}


void loop()  

{ 
  {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH)               { 
  // set the brightness of pin 9:
  analogWrite(pulseLED, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
                                            }
else   {  
  digitalWrite(pulseLED, LOW);
  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
  delay(20);
  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
  delay(20);
  digitalWrite(flashled1, HIGH);     // turn first led on 
  digitalWrite(flashled2, HIGH);     // turn second led on
  delay(20);
  digitalWrite(flashled1, LOW);     // turn first led off 
  digitalWrite(flashled2, LOW);     // turn second led off
        
        }
  }
}

You don't need an external resistor on the pin as you are using the internal pullup resistor. Indeed, the way you have the resistor wired would not be useful even if you did not have the internal pullup resistor.

I'm sure you will think of a way to stop the flashing after a good night's sleep - that is you homework assignment. HINT, think about counting.

...R