stop button in a long program

hello, I'm working on a RGB led cube. He's programmed now and the animations work fine. The only disadvantage is that the cube immanently start to run when i turn on the power. So i bought a push-button which is acting as a switch. The intention of this push button is, when i push on the push-button, the codes start to run. When i push again on the push button the codes stops running. But this is a problem. The Code starts perfectly but i can't stop the code. The functions that run take around 5 minutes (check the code) and here situates the problem i think, for five minutes long there is no check if the code has been pushed or not... So my question is "is there a way where i can stop my code anytime when i want from running? Thanks in Advance! This is a piece of my code:

const int buttonPin = 4;    // the number of the pushbutton pin
const int ledPin = 9;      // the number of the LED pin

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {

  // set up IO pins as outputs
  // -------------------------------------------------------------
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
  pinMode(SPI_SCK,  OUTPUT);
  pinMode(SPI_MOSI, OUTPUT);
  pinMode(SR_LATCH, OUTPUT);
  pinMode(SR_BLANK, OUTPUT);

  digitalWrite(SR_BLANK, HIGH);              // temporarily disable all shift register outputs
  digitalWrite(SR_LATCH,  LOW);              // set shift register latch to initial state

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;

  if(ledState==HIGH) { //all together it takes auround 5minutes to run all these functions
    animatieDrie (TD12,TD13);
    animatieVier (TD14); 

This is a piece of my code

I'm only posting a piece of my answer.

The functions that run take around 5 minutes (check the code)

you didn't post all the code(surely you must have noticed this?)
Take a look at the blink without delay example in the IDE

ok here's the full code: cube code -

It's a good job you found this problem before your sketch got too large.
You need to get rid of all those for loops and delays.

ok here's the full code:

I reckon you should have sorted out that problem before you program got to line 1777. Maybe somewhere about line 177?

How many times per second does your loop() function repeat?


yes, i now the code is not so economic written. The code takes around 5 minutes before the loop starts over again

yes, i now the code is not so economic written. The code takes around 5 minutes before the loop starts over again

Then you need to change that so that loop() repeats at least 10 times per second and preferably a few hundred times per second.

Without making that change you cannot have a responsive program.

Have a look at the demo Several Things at a Time