Loop stuck in a delay.

So i have this code that is almost working.
The point of it is to start 4 LEDS after a button push and when the time runs out then some sound comes out.
And that is the end of the program.

However i also have to be able to shut down everything when i push that button again. But i can’t…
It is so focused on doing eveything else that it won’t even see the button anymore.

In other words, my loop is like a big delay that i don’t know how to fix.

int sound = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int buttonPin = 2;
int buttonPin2 = 8;
int CM = 0;
int Timer1 = 100;
int Timer2 = 0;
int LedTimer = 0;
int LedOfTimer = 0;
int first = 0;
int mintimer = 0;
int MIN = 0;
int mintryck = 0;
int ledState = 0;
int i = 8;
int ledon = 0;
int CM1 = 0;
int CM2 = 0;
int CM3 = 0;
int CM4 = 0;
int num = 0;
int pip = 1;
int wait = 0;
int sd = 1;
int first2 = 0;
int lastButtonState = HIGH;
int lastDebounceTime = 0;
int debounceDelay = 0;
int buttonState;
int dab = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(sound, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);


}




void loop()
{


  int reading = digitalRead(buttonPin); //Im reading the button.

  if (reading == LOW)
  {
    Serial.println(dab);
  }

  else if (reading != lastButtonState)
  {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    if (reading != buttonState) {
      buttonState = reading;

      if (first == 1)
      {
        first = 0;
        sd++;
        delay(20);
      }
      else
      {
        first = 1;

      }
    }
  }


  if (sd >= 2)   //It gets stuck from here and i cannot start from skratch.
                    //Leds and such start from here. 
  {
    CM = millis();

    Serial.println("knapp");
    if (ledon == 0)
    {
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      ledon = 1;
    }

    if (CM - LedTimer >= 3000)
    {
      Serial.println("Hej Hej");
      LedTimer = CM;
      digitalWrite(i, LOW);
      --i;

    }
    if (i <= 3)
    {
      i = 8;
      wait = 42;
      Timer1 = CM;
    }



    if (wait == 42)
    {
      mintryck = 1;
    }


    if ((mintryck == 1) && (CM - Timer1 >= 150))
    {
      tone(sound, 400, 100);
      Serial.println("klar");
      Timer1 = CM;
      pip++;
    }
    Serial.print(pip);
    if (pip == 2)
    {
      digitalWrite(i, LOW);
    }
    if (pip == 4)
    {
      mintryck = 0;
      first = 0;
      pip = 1;
      ledon = 0;
      wait = 0;
      Timer1 = CM;
      LedTimer = CM;
      MIN = CM;
      sd = 1;
    }

  }

}

Any tips at this point would be very helpful!

Then it's time to learn to debug your logic.

Just before the first "if" place a line of code, such as: Serial.println("ready for if 1");

Then you you will see that message on the screen and you know there the logic is in the program.

Now add another line just before the next if, showing for if 2.

Now when you run the program you should see both messages.

Continue for each "if" in your code. When you DO NOT see one of your messages, that is the "if" that may be causing the problem.

Print the values used in that "if" statement and see if what you see is what you expect.

Good luck!

Paul

int lastDebounceTime = 0;
int debounceDelay = 0;
...
  if ((millis() - lastDebounceTime) > debounceDelay)

ALWAYS use unsigned long for variables associated with millis().

The above is [u]guaranteed[/u] to fail after about 32 seconds.