problem with button modes

hi. i have this code here:

int counter = 0;
long randNumber;
long randNumber2;
int ledPin =  9; 
int buttonPin = 13;




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

void loop()
{
  //Handle input
 
 int switchVal = digitalRead(buttonPin);
if(switchVal == HIGH)
{
   delay(500);
    counter ++;
    //Reset count if over max mode number
    if(counter == 5)
    {
      counter = 0;
    }
  }
  else
   //Change mode
  switch (counter) {
  case 1:
     // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(20);                            
  } 

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(20); 
  }
    break;
  case 2:
     randNumber = random(300);
  randNumber2 = random(300);
  digitalWrite(ledPin, 255);  
  delay(randNumber);    
  digitalWrite(ledPin, 0);    
  delay(randNumber2); 
    break;
  case 3:
     digitalWrite(ledPin, 255);
    break;
  case 4:
     digitalWrite(ledPin, 0);
    break;
 
 }

}

it changes the led modes upon button push and it works fine, except for one thing. it only registers the button push when the loop is through (for example when the fade goes back to 0 before it restarts to dim).
i would like to have the arduino register the push of the button at any time without having to hold the button till the fade is through and it registers the push. just one short push of the button at any given time and the mode should proceed to the next.
another small problem which i dont understand is that the modes are: fade, random blink, on, off. but after on i have to push twice to get to fade. tried some but wasnt able to fix it.
thanks in advance.

it only registers the button push when the loop is through (for example when the fade goes back to 0 before it restarts to dim).

Not quite. It recognizes the button push at the start of loop. Once a pattern begins, loop does not get called again for some time. You need to hold the button down to be sure that the button is being pressed when loop is called again. To solve this, you need loop to be executed more frequently. The blink without delay example shows how to make this happen.

another small problem which i dont understand is that the modes are: fade, random blink, on, off. but after on i have to push twice to get to fade. tried some but wasnt able to fix it.

These modes correspond to 0, 1, 2, and 3. Until counter gets from 3 to 5, nothing happens. The limit value to switch back to mode 0 should be 4, not 5.

thanks for the reply. i changed the limit value to 4 but now it skipps the off and goes straight to the fade after on. so now i only have the first 3 modes.
i´m also pretty confused where and how to implement the without delay method. how do i use it with the random? the led doesn´t fade anymore either. could you maybe give me a hint please? im stuck. thanks.

i changed the limit value to 4 but now it skipps the off and goes straight to the fade after on. so now i only have the first 3 modes.

Why do you reset to 0 when the counter reaches the upper limit? Because you start with counter = 0. But, you don't have a case for counter == 0. You really should.

Think about how YOU would handle the job of turning the LEDs on/off/up/down, given either a stopwatch, pencil, and paper or a timer.

i´m also pretty confused where and how to implement the without delay method. how do i use it with the random?

For the fade example, using the timer, you'd increase the voltage to the LED and start the timer. When the timer went off, you'd see if you needed to increase the voltage again. If so, you'd increase the voltage to the LED and start the timer.

Using the stopwatch, you'd increase the voltage to the LED and note the time. Periodically, you'd note the time and compare it to when you changed the voltage last. If it was time to change the voltage again, you'd see if you needed to increase the voltage again. If so, you'd increase the voltage to the LED and note the time.

The delay() function represents the timer. When you start the timer, you can do nothing else until the timer goes off.

The millis() function represents the stopwatch. You can't change the time, but you can watch it elapse. You can note when you do things (unsigned long then = millis();), and compare now to then (if(millis() - then > interval) // do something).

the led doesn´t fade anymore either.

"All I changed was..." a deep dark secret, apparently. You need to show us what you changed, by posting your code.

ok i couldnt figure out how to use the millis() with the random, but i read a post where sin/cos was suggested to dim so i tried it and now it works. i still have delays in the code but they dont seem to interfere. the other problem i had was because i started with case1 instead of case0. i dont have that much time now to figure the millis/random thing out but i hope i will soon. im just happy it works the way it was intended to. or do you think this way would cause problems? thanks paul for your help.