Fade sequence X times when button pressed

Hi everyone-
I’d like to use this fade sequence (it’s uh, pretty) but I only want it to loop a certain number of times (say, 3) when a button is pressed.

#include <math.h>

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
}

Or conversely, fade constantly unless a button is pressed, then go from the fade to a tripple blink, then back to the fade sequence.
I’m having trouble finding the right answer/direction, mostly because I don’t think I know the right terms. Would this be an interupt? or an else?

Credit for that fade code to http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/

Step 1 is to define what you mean by “loop a certain number of times” as your current code doesn’t define a start or end point for what you consider to be a cycle. Based on your current code, It will likely have to do with time.

Step 2 is to figure out how to perform an action when a switch is pressed down. The StateChangeDetection example shows you how to accomplish this. Start by getting your switch to simply print something like “Hello” when this occurs.

Once you got that working, Step 3 it’s time to figure out how to “record the time”. Blink Without Delay example demonstrates using millis() for timing related concepts. Combined with step two, you can identify the time at which the button was pressed down. Combining that with step 1, you can figure out the time at which X number of cycles should stop.

At this point it’s just a matter of if statements.

Mermalady:
Hi everyone-
I’d like to use this fade sequence (it’s uh, pretty) but I only want it to loop a certain number of times (say, 3) when a button is pressed.

#include <math.h>

void setup()
{
 pinMode(11, OUTPUT);
}

void loop()
{
 float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
 analogWrite(11, val);
}



Or conversely, fade constantly unless a button is pressed, then go from the fade to a tripple blink, then back to the fade sequence. 
I'm having trouble finding the right answer/direction, mostly because I don't think I know the right terms. Would this be an interupt? or an else?

Credit for that fade code to http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/

something like this (not tested) to switch between fading/blinking with a button press, changing a “state”, and modularizing your code into functions…

#include <math.h>
int ledPin = 11;
int buttonPin = 2;
int lastPressed;
int state = 0;
unsigned long startTime;

void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
}

void loop()
{
  int pressed = digitalRead(buttonPin); 
  if (pressed)
  {
    if (pressed != lastPressed)
    {
      state++;
      if (state > 1) state = 0;// add more light tricks by adding states
    }
  }
  lastPressed = pressed;
  if (state == 0)
  {
    prettyFade();
  }
  else if (state == 1)
  {
    blinkyLed();
  }
  else if (state == 2)
  {
    //another cool led trick?
  }
}

void prettyFade()
{
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(ledPin, val);
}

void blinkyLed()
{
  if (millis() - startTime >= 100UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime += 100UL;
  }
}