Button state problem

Hi!

I am trying to make a simple police light flasher and want to press a button to change between different flashing modes, but I cant figure out how to do it..

This is my code so far, but the different modes just keeps looping one after the other..

I would gladly appreciate some help :)

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

const int blue1 = 8;
const int blue2 = 3;
const int blue3 = 7;
const int blue4 = 5;
const int blue5 = 6;
const int red1 = 13;
const int red2 = 11;
const int red3 = 12;
const int red4 = 10;
const int red5 = 9;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current buttonState of the button
int lastButtonState = 0
;     // previous buttonState of the button

void setup() {

  pinMode(buttonPin, INPUT);
  
  pinMode(blue1, OUTPUT);  
  pinMode(blue2, OUTPUT);
  pinMode(blue3, OUTPUT);
  pinMode(blue4, OUTPUT);
  pinMode(blue5, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(red3, OUTPUT);
  pinMode(red4, OUTPUT);
  pinMode(red5, OUTPUT);

  Serial.begin(9600);
}

void loop() {

    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  

     
    // ======= START PROGRAM NR. 1 =======
    if(buttonState == 0){
    buttonState = 1;
    
      Serial.println("Program number 1 is running...");
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
         
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
    lastButtonState = buttonState;  
    }
    
// ======= START PROGRAM NR. 2 ======
  if(buttonState == 1);
  buttonState = 2;
      Serial.println("Program number 2 is running...");
      

        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        /*-----------------------------------------*/
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
      lastButtonState = buttonState;
    // ======= START PROGRAM NR. 1 =======
    if(buttonState == 0){
    buttonState = 1;

Maintain two distinct variables for state-of-control-button and current-program-mode.

What exactly do you mean by that?

Sorry, I'm new to coding in C..

do {} while (digitalRead(buttonPin) == LOW);
delay( <whatever> );
mode++;
switch (mode)
{
  case 1: run_program_1();
             break;
  case 2: run_program_2();
             break;
  ....
}
do {} while (digitalRead(buttonPin) == HIGH);
delay( <whatever> );

Like this?

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

const int blue1 = 8;
const int blue2 = 3;
const int blue3 = 7;
const int blue4 = 5;
const int blue5 = 6;
const int red1 = 13;
const int red2 = 11;
const int red3 = 12;
const int red4 = 10;
const int red5 = 9;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current buttonState of the button
int lastButtonState = 0;     // previous buttonState of the button

void setup() {

  pinMode(buttonPin, INPUT);
  
  pinMode(blue1, OUTPUT);  
  pinMode(blue2, OUTPUT);
  pinMode(blue3, OUTPUT);
  pinMode(blue4, OUTPUT);
  pinMode(blue5, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(red3, OUTPUT);
  pinMode(red4, OUTPUT);
  pinMode(red5, OUTPUT);

  Serial.begin(9600);
}

void loop() {

    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  
do {} while (digitalRead(buttonPin) == LOW);
delay( 20 );
mode++;
switch (mode)
{
  case 1: run_program_1(
  Serial.println("Program number 1 is running...");
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
         
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
    lastButtonState = buttonState;  
    });
             break;
  case 2: run_program_2(Serial.println("Program number 2 is running...");
      

        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        /*-----------------------------------------*/
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
      lastButtonState = buttonState;);}
             break;

}
do {} while (digitalRead(buttonPin) == HIGH);
delay( <20> );

When I try it like this I get the following error:
"In function ‘void loop()’:
error: ‘mode’ was not declared in this scope At global scope:"

run_program_1() ought to be a separate function outside of loop(), and I think you can figure out how to declare a missing variable.

I'm sorry, but its hard for me to understand the meaning of what you are saying due to my lack of experience.. But thank you anyhow!

What I have acomplished so far is to be able to switch thuogh the three modes by pushing the button, but they all stop after the first run. How can I repeat them until the next button-press? And is there other ways to write the on and off fucntions for the different modes, so that I can shorten up the codes a bit?

Here is my code now:

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

const int blue1 = 8;
const int blue2 = 3;
const int blue3 = 7;
const int blue4 = 5;
const int blue5 = 6;
const int red1 = 13;
const int red2 = 11;
const int red3 = 12;
const int red4 = 10;
const int red5 = 9;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current buttonState of the button
int lastButtonState = 0;     // previous buttonState of the button
int mode = 0;

void setup() {

  pinMode(buttonPin, INPUT);
  
  pinMode(blue1, OUTPUT);  
  pinMode(blue2, OUTPUT);
  pinMode(blue3, OUTPUT);
  pinMode(blue4, OUTPUT);
  pinMode(blue5, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(red3, OUTPUT);
  pinMode(red4, OUTPUT);
  pinMode(red5, OUTPUT);

  Serial.begin(9600);
}

void loop() {

    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  
do {} while (digitalRead(buttonPin) == LOW);
delay( 20 );
mode++;
switch (mode)
{
  case 1: 
  
        Serial.println("Program number 1 is running...");
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
         
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);;
        break;
  
  case 2: 
  
        Serial.println("Program number 2 is running...");
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        /*-----------------------------------------*/
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW); 
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        break;
}
do {} while (digitalRead(buttonPin) == HIGH);
delay( 20 ); 

if(mode == 2){mode = 0;}

}

And by the way; does anyone know of any good rescources for learning simple c-language ore somthing to better help me understand and program arduiono sketches myself?

Hava a look at this page:

http://arduino.cc/en/Tutorial/Foundations

Especially the "Programming technique" section.

HTH

Thanks for the tip mromani! I will check it out..

Still think I need some hints to my improve my code tough.. :-/

I don't want to sound rude, but drhex for example gave you a good tip on how to organize you code.

But then you wrote something like this:

 case 1: run_program_1(
  Serial.println("Program number 1 is running...");
  ...
 )

that shows you don't even know how to call a function in C. Please understand that I'm not judging you. I'm just saying that it's going to be difficult to help if you don't first learn the very basics of the C language.

That said, I promise I'll try to find some time to work on your code. I just can't right now.

See you later...

It was never my intention to offend anyone, but the fact is, as you say, I am still at the very beginning of learning the very_basics of C language and therefore may have some difficulties understanding aspects. I am very sorry if that is the case. I was just trying to experiment with the code..

Thank you so much for using of your spare-time to help me out learning!

You didn't offend anyone. And I'm glad you didn't feel offended by my words.

Now I just hope my next post will be a tad more useful code-wise :-)

Let's see if I can come up with something understandable...

First, let me recall that setup() is called only once at program startup, while loop() is called repeatedly.

Second, digitalRead() only returns 0 or 1, to represent the electrical state of a pin.

In your program, you must have a variable that represents the number of the currently selected flash program. You can then have another variable that represents the button status. They must not be confused.

Each "program", i.e. flash pattern is written as a separate function, like this:

void program0() {
    digitalWrite(...);
    ...
    delay();
    ...
    digitalWrite(...);
}

where the "0" at the end of the function name is the program number (you could also use A, B, C, etc.).

To call the function that implements the currently selected program, you can use the switch() statement, like this:

switch(currentProgramNumber) {   // it's an integer
    case 0:
        program0();
        break;

    case 1:
        program1();
        break;

    // etc.
}

Inside loop() you just have to do two things:

1) check whether the button was pressed; if yes, select next program. 2) run the currently selected program.

Here's some code.

I tried to keep is simple.

const int buttonPin = 2;   // pushbutton pin


const int blue1 = 8;
const int blue2 = 3;
const int blue3 = 7;
const int blue4 = 5;
const int blue5 = 6;
const int red1 = 13;
const int red2 = 11;
const int red3 = 12;
const int red4 = 10;
const int red5 = 9;


// since we want to cycle through several different
// "programs", i.e. flash sequences, we must keep track
// of which one is executing now
// Note that programs are counted from 0, not from 1.
int runningProgram;


// first program
void program0() {
      Serial.println("Program number 1 is running...");
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, HIGH);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, HIGH);
        
}


// second program
void program1() {
      Serial.println("Program number 2 is running...");


        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH);
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        /*-----------------------------------------*/
        
        delay(50);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, HIGH);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, HIGH);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, HIGH);
        digitalWrite(red2, LOW);
        digitalWrite(red3, HIGH);
        digitalWrite(red4, LOW);
        digitalWrite(red5, HIGH);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
        
        delay(50);
        
        digitalWrite(blue1, HIGH);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, HIGH);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, HIGH);
        digitalWrite(red1, LOW);
        digitalWrite(red2, HIGH);
        digitalWrite(red3, LOW);
        digitalWrite(red4, HIGH);
        digitalWrite(red5, LOW);
        
        delay(80);
        
        digitalWrite(blue1, LOW);
        digitalWrite(blue2, LOW);
        digitalWrite(blue3, LOW);
        digitalWrite(blue4, LOW);
        digitalWrite(blue5, LOW);
        digitalWrite(red1, LOW);
        digitalWrite(red2, LOW);
        digitalWrite(red3, LOW);
        digitalWrite(red4, LOW);
        digitalWrite(red5, LOW);
}


void setup() {
  
  pinMode(buttonPin, INPUT);

  pinMode(blue1, OUTPUT);  
  pinMode(blue2, OUTPUT);
  pinMode(blue3, OUTPUT);
  pinMode(blue4, OUTPUT);
  pinMode(blue5, OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(red3, OUTPUT);
  pinMode(red4, OUTPUT);
  pinMode(red5, OUTPUT);
  
  // we start with the first program
  runningProgram = 0;

  Serial.begin(9600);
}


void loop() {
    int buttonState;
    
    buttonState = digitalRead(buttonPin);
  
    // if button was pressed, swich to the next program
    if (buttonState) {
        runningProgram++;
        
        // if we have two programs, valid values
        // are 0 and 1, there fore 2 means "restart"
        if (runningProgram >= 2) {
            runningProgram = 0;
        }
    }
    
    // call the selected program
    switch(runningProgram) {
        case 0:
            program0();
            break;
        
        case 1:
            program1();
            break;
    }
}

I compiled this code with the Arduino IDE, but didn't run it on a real board, so I cannot guarantee it will work exactly as intended.

Let me add a personal note: if you are still in the early stages of your C learning, I suggest you start with a simpler task than this. What you are trying to do is not so simple as it seems.

Thank you so much again for helping me out! ::)

The code works great, exept from the change-mode part. Now it just runs trough both the modes at the same time all in a big loop and nothing happens when i press the button.. :-/ (Yes, I have checked the connections..)

Yeah, i understand what you mean about me trying to learn C with this as my first project. It's just that I have been wanting to build this for a long time and have some friends who would like one as well.. I'm just not patient enough I guess.. BUT, today i found a VERY nice article/wiki/guide in _absolute_beginner_C_language wich i spent hours reading, so I have allready written som simple text programs by myself :) I am looking forward to learning more! My big dream is to build my girlfriend one of the NerdKits-projects kalled LED Valentine Heart - without the kit ofcourse..

Enogh off-topic!

Maybe the button pin is active low...

try to replace the line

if (buttonState) {

with:

if (!buttonState) {

That is, put an exclamation mark in front of the variable buttonState. The meaning is: change the program when the button pin goes low.

That didnt make any difference.. :-/

You're not using the internal pullups - how is your button wired?

When you get your buttons wired up correctly you must remember the state of the last time and only move on to the next program when the button state changes from being a zero to a one.

 int LastButtonState;
void loop() {
    int buttonState;
    
    buttonState = digitalRead(buttonPin);
  
    // if button was pressed, swich to the next program
    if ((buttonState == true) && (lastButtonState == false)) {
        runningProgram++;
        
        // if we have two programs, valid values
        // are 0 and 1, there fore 2 means "restart"
        if (runningProgram >= 2) {
            runningProgram = 0;
        }
    }
    
   lastButtonState == buttonState;
    // call the selected program
    switch(runningProgram) {
        case 0:
            program0();
            break;
        
        case 1:
            program1();
            break;
    }
}