For Loop Problem

Having problems in looping it 5 times. See picture attached please
it should have been 1 by 1 per button pressed, instead they spammed 5 times per button pressed.

const int button = 15;
int buttonstate = 0;
int i = 0;

void setup(){
  pinMode(button,INPUT);
  Serial.begin(9600);
  testing();
}


void loop(){
}

void testing(){

  for(i=0; i<5; i++){
    Serial.println("led1 On");
    Serial.println("led2 On"); 
    buttonstate = digitalRead(button);
  if(buttonstate == HIGH){
    Serial.println("Mixer On");
  }

 while(!digitalRead(button)){
   }
  }
}

How is your switch wired?

Time to GOGGLE

example: http://www.eng.utah.edu/~cs5780/debouncing.pdf

It was like this.

Can you please explain what should happen when you run the program ?

As written you are detecting when the button IS pressed not when it BECOMES pressed. You maybe need to look at the StateChangeDetection example in the IDE.

const int button = 2;
int buttonstate = 0;
int i = 0;

void setup(){
  pinMode(button,INPUT);
  Serial.begin(9600);
  testing();
}


void loop(){
  testing();
  delay(1000);
  Serial.println("...........");
}

void testing(){

  for(i=0; i<5; i++){
    Serial.println("led1 On");
    Serial.println("led2 On"); 
    buttonstate = digitalRead(button);
  }
  if(buttonstate == LOW){
    Serial.println("Mixer On");
  }

  while(!digitalRead(button)){
  }
}

check the code above is that you expected.In your code you put for loop bracket wrongly. The value of pin pullup. SO it keep printing mixer On. so make it as low. so you get proper value

it only loops led1 and led 2 five times.

instead I want it to loop the whole function. five times like:

led1 On led2 On (if you press button display) mixer ON led1 On led2 On (if you press button display) mixer ON led1 On led2 On (if you press button display) mixer ON led1 On led2 On (if you press button display) mixer ON led1 On led2 On (if you press button display) mixer ON then end of loop.

You need to explain a bit more clearly what should happen if the button is pressed and if it is not pressed. A for loop is almost certainly not the best way to do what you want although it may be possible to use one.

So, in plain words please : The program starts - what output should there be if any ? If the user presses the button what should happen ? If the user does not press the button what should happen ? If the user holds the button down what should happen ?

Bear in mind that if you don't hold up the for loop it will complete 5 iterations in microseconds. If you hold up the for loop waiting for the user to press the button then the output will be the same each time through the for loop because it will only move on when the user presses the button.