Comment puis-je briser cette boucle sur un bouton de la presse?

Je suis en train de créer un compte à rebours dans Arduino qui va commencer à la simple pression d'un bouton, et d'annuler en appuyant sur le même bouton. La valeur est comprise entre 0 - 60 et réglé par un potentiomètre. Le problème que j'ai est que je ne peut pas sortir de la boucle après son démarrage. Je sais que cela peut être fait à l'aide de la "casser", mais je ne vois pas où le mettre que le résultat sera comme souhaité. C'est ce que j'ai à ce jour:

const int  buttonPin = 2;    //The pin that the pushbutton is attached to
int buttonState = 0;         //Current state of the button
int lastButtonState = 0;     //Previous state of the button

void setup() {
    //initialize serial communication:
    Serial.begin(9600);
}

void timer(){
    int pot_value = analogRead(A0); //read potentiometer value
    if (pot_value > 17) { //i set it so, that it doesn't start before the value
                          //isn't greater than the 60th part of 1023
        int timer_value = map(pot_value, 0, 1023, 0, 60); //Mapping pot values to timer
        for (int i = timer_value; i >= 0; i--){ //Begin the loop
            Serial.println(i);
            delay(1000);
        }
    }
}

void loop() {
  //Read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  //Compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      //If the current state is HIGH then the button
      //went from off to on:
      timer(); //run timer
    }
    else {
      //If the current state is LOW then the button
      //went from on to off:
      Serial.println("off");
    }
  }
  //Save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

Par exemple, si j'ai mis le potentiomètre 5 et appuyez sur le bouton je vois 5, 4, 3, 2, 1, 0, mais je ne peut pas sortir de il si j'appuie sur le bouton de nouveau jusqu'à la fin. Comment puis-je échapper à cette boucle sur la simple pression d'un bouton?

InformationsquelleAutor skamsie | 2013-06-30