/* Arduino Kurs FES Juli 2017 Programm 5b: Verbesserter Wuerfel */ int first_led_pin = 2; // erste LED int last_led_pin = 8; // letzte LED int button_pin = 12; // Taster an Pin 12 boolean dice_rolling = true; // zeigt an, ob der Würfel gerade rollt int dice_score = 1; // aktuelle Augenzahl des Würfels int delay_time = 5; // Zeit zwischen den Augenzahlen void setup() { int i; for (i = first_led_pin; i <= last_led_pin; ++i) { pinMode(i, OUTPUT); // Pin 2 bis Pin 7 werden Output }; pinMode(button_pin, INPUT_PULLUP); // Pin 12 wird Eingang für den Taster } void loop() { if (dice_rolling) { // Falls der Würfel rollt ... show_dice(dice_score); // Zeige die aktuelle Augenzahl ++dice_score; // Erhöhe die Augenzahl if (dice_score > 6) dice_score = 1; // Wenn 6 überschritten wird, wieder bei 1 anfangen if (digitalRead(button_pin) == HIGH) { // Wenn die Taste frei gegeben wurde ... delay_time = delay_time + 5; // ... verlangsamere das Tempo if (delay_time > 200) { // Wenn delay_time sehr gross wird ... dice_rolling = false; // ... halte den Würfel an }; }; } else { // Falls der Würfel nicht rollt ... if (digitalRead(button_pin) == LOW) { // ... checke, ob die Taste gedrückt ist. dice_rolling = true; // In dem Fall starte den Würfel neu ... delay_time = 5; // ... und setze delay_time zurück }; }; delay(delay_time); // einen Moment warten } /* show_dice() ------------------------------------------------------------------------------------- * Zeigt die aktuelle Augenzahl an */ void show_dice(int score) { // Funktion zur Anzeige der Augenzahl boolean score_table[] = { // oben --------------- Mitte unten -------------- // links, Mitte, rechts, Mitte, links, Mitte, rechts LOW, LOW, LOW, HIGH, LOW, LOW, LOW, // 1 HIGH, LOW, LOW, LOW, LOW, LOW, HIGH, // 2 HIGH, LOW, LOW, HIGH, LOW, LOW, HIGH, // 3 HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, // 4 HIGH, LOW, HIGH, HIGH, HIGH, LOW, HIGH, // 5 HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, // 6 }; int i; for (i = 0; i < 8; ++i) { digitalWrite(first_led_pin + i, score_table[(score - 1) * 7 + i]); } }