How to count the number of button press then play sequence..?

1.when i press the button once, sequence s1() play.
2.when i press the button twice, sequence s2() play.
3.when i press the button thrice, sequence s3() play.
4.when i press the button 4 times, sequence s4() play.

const int buttonPin = 10;  
int buttonState = 0;

void setup(){
  Serial.begin(9600);
  //initialize pin 2 - 9 as output
  for(int i=2;i<=9;i++){ 
    pinMode(i,OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop(){
 buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {  


   s1();
   s2();
   s3();
   s4();

   }

}





void s1(){
   int i = 2 ;
   for(int z=0;z<=1;z++){
      for(int x=i;x<=9;x++){   
       digitalWrite(i,HIGH);
       digitalWrite(x-1,LOW);
       delay(500);
       i = i + 1;
      }
      i = 2;
      if(i = 2){
       digitalWrite(9,LOW);
       }
   }
}

void s2(){
  int y = 9;  
  for(int i=0;i<=1;i++){
    for(int x=2;x<=5;x++){
      if(y>=1){
       digitalWrite(x,HIGH);
       digitalWrite(y,HIGH);
       delay(500);
       y = y - 1;
      }
    }
    off();
    y = 9;
    delay(500);
  }  
}

void s3(){
  for(int i=0;i<=2;i++){
      for(int z=2; z<=9;z++){
            if(z % 2 == 0){
               digitalWrite(z,HIGH);
            }
      }
      delay(500);
      for(int z=2; z<=9;z++){
            if(z % 2 == 0){
               digitalWrite(z,LOW);
            }
        }
      delay(500);
      for(int z=2; z<=9;z++){
            if(z % 2 != 0){
               digitalWrite(z,HIGH);
            }
      }
      delay(500);
      for(int z=2; z<=9;z++){
            if(z % 2 != 0){
               digitalWrite(z,LOW);
            }
        }
       delay(500);
  }
}

void s4(){
    for(int i=0;i<=2;i++){
      for(int x=2;x<=9;x++){
         digitalWrite(x,HIGH);
         delay(500);
        }
        off();
        delay(500);
      }
 }


void off(){
  for(int i=2;i<=9;i++){ 
    digitalWrite(i,LOW);
  } 
}

In order to count the number of time the button as become pressed you need to spot when it changes state.

Have a look at the StateChangeDetection example in the IDE.

However, as currently written the program will not be very responsive to button presses as it stays locked into the functions until each sequence if finished and the button is only read at the end of the sequence at best. In order to read the button more frequently you need to use millis() for timing and take advantage of the fact that the loop() function does what its name suggests.

Save the millis() value at the time that the start action happens. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don’t block the free running of loop().

Incidentally, when you do this
  for (int z = 0; z <= 1; z++)How many times do you expect the for loop to iterate ?

Incidentally, when you do this

Code: [Select]
  for (int z = 0; z <= 1; z++)

How many times do you expect the for loop to iterate ?

2times

is there a way to break the loop when i press the button when the loop is running? sorry im new in programming.

2times

This is equivalent to the for loop

z = 0;
while (z <= 1)  //note that z starts with a value less than 1
{
//do stuff
z++;
}

How many times will the while loop execute the //do stuff code

is there a way to break the loop when i press the button when the loop is running?

You could read the button pin inside the for loop then use a break; statement to exit the for loop when the button becomes pressed, but that would be regarded by many as an inelegant solution to the problem.