Question about changing modes with a button

hey guys.
i am new here on this platform and i already checked out some topics but i did not really found something about my problem.

i have an rgb led and one button and an arduino (uno, nano, ...). i want to press the button and then the mode changes between my different programs. like mode=0 is all leds are off. mode=1 just blue. mode=2 just green. mode=3 just red. mode=4 smooth change between all colors.. and so on

i found a video on youtube of a guy who makes some mushrooms out of silicon, with an led included. i want to use a rgb led and a an arduino to control pwm colors. i want to use an arduino nano. the boards will arrive soon.

i dont have any program to give u my electric scheme, but so far all different colors and the buttons are working, so dont mind.

my sourcecode is following:

int mode = 0;                          // Beim Start LED aus

int rotPin          = 9;
int blauPin         = 10;
int grunPin         = 11;
int buttonPin    = 7;

void setup() {
  pinMode(blauPin, OUTPUT);         // LED Blau
  pinMode(grunPin, OUTPUT);         // LED Gelb
  pinMode(rotPin, OUTPUT); 
  pinMode(buttonPin, INPUT);        // Signal - Taster
}

boolean buttonPressed() // Taster abfragen auf drücken des Buttons
{
  static boolean lastButtonState;
  boolean buttonState=digitalRead(buttonPin);
  if (buttonState!=lastButtonState) // Status hat sich geändert
  {
    lastButtonState=buttonState;  // letzten Status merken
    if (buttonState==HIGH) return true; // Status hat sich auf "gedrückt" geändert
  }
  return false;
}

void loop() { 

  switch(mode){

          case 0: 
            while(mode==0){
            analogWrite(rotPin,255);
            analogWrite(grunPin,255);
            analogWrite(blauPin,255);
            if (buttonPressed()){
              mode++;
              delay(5);}
            }
            break;
            
          case 1:
            while(mode==1){
            analogWrite(rotPin,30);
            analogWrite(grunPin,30);
            analogWrite(blauPin,10);
            if (buttonPressed()){
              mode++;
              delay(5);}
            }
            break;
            
          case 2:
            while(mode==2){
            analogWrite(rotPin,255);
            analogWrite(grunPin,255);
            analogWrite(blauPin,30);
            if (buttonPressed()){
              mode++;
              delay(5);}
            }
            break;
delay(5);

}

i hope u can help me out. it should not be to difficult but i dont get any solution myself and i am searching now for about a week.

i tried links like:
http://forum.arduino.cc/index.php?topic=273438.0

but in this explaination there are just three modes and the 'solution sourcecode' is to complicated for my simple programming knowledge. they differ just between on, off and blinking. i dont want to use commands like onmode and offmode and so on because i dont know how to handle them. of course its easier and you could give me a solution like this.

i have allready written some while loops for changing the colors smoothly or do some color changing blink or something like that and i would be happy to use my own code. but i just dont know how to set the switch-case to give the button the command to change the mode.
i also have some potentiometers programmed to change the indensity of the different colors

i would be really happy if you give me some help or some links to check out. of course it can be that i missed some topics where they already discussed problems, like those that i have.

First thing I see, 1 lead from your button is connected to pin 7, what is the other lead connected to? If connected to Vcc, you need a 10k pulldown resistor connected from pin7 to GND so, when the button is not pressed, the pin is not floating and is solid LOW. Best way is connect button between pin 7 and GND and use internal pullup.

pinMode(buttonPin, INPUT_PULLUP);

Then, "buttonState" will be LOW when pressed and HIGH when not. Then go from there.

I like using expresssch from www.expresspcb.com for schematic capture, here's an Arduino symbol I made showing your example


Ctrl-Print screen to capture it (or Export to a Metafile (.emf) ), paste (either one) into MS Power Point, Save As a .jpg for posting here.

ExpressSch and ExpressPCB are good for capturing designs that I intend to build up using sockets and stuff onto perfboard. Makes it easy to see what goes where and how well it will fit.

For schematics intended to made into PCBs, I use Eagle tho. https://cadsoft.io/
Their free version limits you to 80x100mm boards, and you can fit plenty onto a double sided board of that size.
It's conveniently the size of a Velleman ECS1/2 board if you build up a prototype and want to move that design to a PCB.
http://www.allspectrum.com/store/prototype-pcb-hole-island-12-eurocard-size-fr4-p-669.html?gclid=COeZtPuf_NACFYeEswodiH4DDw
Priced a lot higher in other places - up to $8 on Amazon! wow.
High quality board with very evenly spaced holes. Drills in the cheap boards are awful, can be hard to work sockets into the boards they are so unevenly placed.

I documented these boards with ExpressSCH and ExpressPCB and wirewrapped them all up on Velleman boards.

hey,
a lot of thanks for your answers. the button is connected to pin 7 and in the same row on the breadboard its connected to Gnd, but for that i use (4x250 Ohm=1kOhm, because i got no other ones, i will buy some later...) resistors and so far the button works fine.

first i got some problems with changing the modes, none with recognizing the pushing of a button, whis works well. but finally i fixed it now with some work. i am really happy now and everything works fine. i hope i can help some guys with this code. i would be happy if someone can create something more 'beautiful and easy'. but so far this works.

/*
 * Das linke Bein vom Knopf ist an +5V angeschlossen, das rechte Bein über 1kOhm (besser wäre 10kohm) an Masse angschlossen,
 * sowie das rechte Bein an Pin 7 angeschlossen ist.
 * Die RGB LED ist mit der gemeinsamen Anode an +5V angeschlossen. Die einzelnen Farbkanäle brauchen jeweils einen Vorwiderstand.
 * Rot=150 Ohm, grün=100 Ohm, Blau=100 Ohm
 * 
 */


int mode = 0;                          // Beim Start LED aus

int rotPin          = 9;
int blauPin         = 10;
int grunPin         = 11;

int buttonPin    = 7;

int grun=255;
int blau=255;
int rot=0;

int tempo = 1;  //tempo for changing rgb led smooth fading tempo
int entprellen = 180;    //reactiontime for button to wait for new klick in ms


void setup() {
  pinMode(blauPin, OUTPUT);         // LED Blau
  pinMode(grunPin, OUTPUT);         // LED Gelb
  pinMode(rotPin, OUTPUT); 
  pinMode(buttonPin, INPUT);        // Signal - Taster
}

boolean buttonPressed() // Taster abfragen auf drücken des Buttons
{
  static boolean lastButtonState;
  boolean buttonState=digitalRead(buttonPin);
  if (buttonState!=lastButtonState) // Status hat sich geändert
  {
    lastButtonState=buttonState;  // letzten Status merken
    if (buttonState==HIGH) return true; // Status hat sich auf "gedrückt" geändert
  }
  return false;
}

void loop() { 

  switch(mode){

          case 0: 
            analogWrite(rotPin,255);
            analogWrite(grunPin,255);           // LED is off
            analogWrite(blauPin,255);
            if (buttonPressed()){
              mode++;
              delay(entprellen);}
          break;
            
          case 1:  
            analogWrite(rotPin,255);
            analogWrite(grunPin,255);         //LED is blue
            analogWrite(blauPin,0);
            if (buttonPressed()){
              mode++;
              delay(entprellen);}
          break;

            
          case 2: 
            analogWrite(rotPin,255);
            analogWrite(grunPin,0);           //LED is green
            analogWrite(blauPin,255);
            if (buttonPressed()){
              mode++;
              delay(entprellen);}
          break;
       
            
          case 3:
            analogWrite(rotPin,0);
            analogWrite(grunPin,255);     
            analogWrite(blauPin,255);         //LED is red
            if (buttonPressed()){
              mode++;
              delay(entprellen);
            }
            break;


          case 4:                       //Smooth Color Changing


            while(rot != 255){
            analogWrite(rotPin,rot);
            analogWrite(grunPin,grun);
               rot++;
               grun--;
               delay(tempo);
            if (buttonPressed()){
              mode++;
              delay(entprellen);
              }
            }

            
            while(grun != 255){
            analogWrite(grunPin,grun);
            analogWrite(blauPin,blau);
               grun++;
               blau--;
            if (buttonPressed()){
              mode++;
              delay(entprellen);
              }
            }   

                        
            while(blau != 255){
            analogWrite(blauPin,blau);
            analogWrite(rotPin,rot);
              blau++;
              rot--;
            if (buttonPressed()){
              mode++;
              delay(entprellen);
              }
            }
          
          break;
            
          
          case 5:
            analogWrite(rotPin,0);
            analogWrite(grunPin,0);     
            analogWrite(blauPin,0);         //LED is white
            if (buttonPressed()){
              mode++;
              delay(entprellen);
            }
          break;


         
          case 6:                       // turn LED off
            mode=0;
            delay(entprellen);
          break;
  }
  
}

i will try to update everything in english and post a scheme how i built up everythng.