Wie kann ich brechen diese Schleife auf eine Taste drücken?

Ich versuche zu erstellen, einen countdown-timer in der Arduino gestartet wird auf Knopfdruck, und das wird auch Abbruch bei drücken der gleichen Taste. Der Wert wird zwischen 0 - 60 und eingestellt durch ein potentiometer. Ich habe das problem bisher ist, dass ich nicht beenden Sie die Schleife, nachdem es beginnt. Ich weiß, dass es getan werden kann, mit der 'Pause', aber ich kann nicht herausfinden, wo zu setzen, dass das Ergebnis wie gewünscht. Dies ist, was ich habe, so weit:

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;
}

Wenn ich zum Beispiel einstellen, das potentiometer auf 5 und drücken Sie die Taste sehe ich 5, 4, 3, 2, 1, 0, aus, aber ich kann nicht brechen aus ihm heraus, wenn ich drücken Sie die Taste erneut, bis es abgeschlossen ist. Wie kann ich aus diesem loop auf Knopfdruck?

InformationsquelleAutor skamsie | 2013-06-30

Schreibe einen Kommentar