Button switch controlled LED

Hi there, I am new to Arduino and would like to tap some advice from you good folks more knowledgeable than me. I have been trying to get this to work for about a week of evenings now.

The purpose is to control and LED from a push button switch. The LED is going to light up a big Tabasco bottle I am setting into nice piece of cedar.

The light needs to have three functions: on, breath, off.

Here the code so far:

int counter = 0;

int brightness = 255;

int switchPin = 2;

int ledPin = 11;


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

void loop() {
 //Handle input
 int switchVal = digitalRead(switchPin);
 if(switchVal == HIGH)
 {
   delay(20);  
   counter ++;
   //Reset count if over max mode number
   if(counter == 4)
   {
     counter = 0;
   }
 }

 else
   //Change mode
   switch (counter) {
   case 1:
 {
  float val = (exp(sin(millis()/4000.0*PI)) - 0.36787944)*108.0;
  analogWrite(ledPin, val);
  }
   case 2:
     analogWrite(ledPin, brightness);
     break;
   case 3:
     analogWrite(ledPin, LOW);
     break;
      }
}

Attached is a poor picture of it operating. It turns on, off, but the breath function does not work. Apologies and thank you ahead of time as I am new to this and programming.

Any help would be awesome.

Render

Moderator edit: Code tags

I guess you need to convert your float to integer for analogWrite.

Your float calculation of the throb is OK, and it is truncated to an integer in the analogWrite().

I have fixed a few problems. First, you needed better debounce as the sketch was flying through the counter. You may want to take a look at some debounce and state change examples. Second, you needed a break; statement in each switch case.

int counter = 0;
int brightness = 255;
int switchPin = 2;
int ledPin = 11;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  //Serial.begin(115200);
}

void loop() {
  //Handle input
  int switchVal = digitalRead(switchPin);
  if (switchVal == HIGH)
  {
    delay(250);//debounce
    counter ++;
   // Serial.println(counter);
    //Reset count if over max mode number
    if (counter == 4)
    {
      counter = 0;
    }
  }
  switch (counter) {
    case 1:
      {
        float val = (exp(sin(millis() / 4000.0 * PI)) - 0.36787944) * 108.0;
        analogWrite(ledPin, val);
        break;
      }
    case 2:
      {
        analogWrite(ledPin, brightness);
        break;
      }
    case 3:
      {
        analogWrite(ledPin, LOW);
        break;
      }
  }
}

Thanks so much for the assistance. Works great now.

Cheers,
Render