Problem with Task after Countdown

Hi,

my current project is a Dice (with LED and LCD) with an Countdown Timer for TableTop Games.

The Dice works perfect, also i found a cool Countdown with adjustable Time, but unfortunately does the action when the countdown is finished not properly work.

I would like, if the countdown is at zero - a piezo gaves a tone until a stop - button is pressed.
But now the piezo (in the code i use a LED at the moment - is less annoying for the tests) is only silent when the
countdown is running.
Shortly after boot it starts to sound. I press the start button, the countdown starts and the piezo is silent.
After the countdown the piezo starts - as it should - but never stopps.

I fear that my idea is not possible with this countdown code, so i have to take another one?
Does anyone have a solution to make it possible with this code?

I tried to add another variable, like the countrun variable, but the only result was a never stopping looping countdown.

Thank you very much for your effort

The code exceeds the maximum allowed post length so i attach it as a txt.

Best Chris

lcddice.txt (10.5 KB)

The code exceeds the maximum allowed post length so i attach it as a txt.

It really isn't necessary to comment EVERY line. Some things are just plain obvious.

It IS necessary to use Tools + Auto Format BEFORE posting code. It is necessary to remove commented out code.

Sry, the reason for the mass of comments was for documentation for myself

I removed the comments and cleaned the code with Auto Format:

// Arduino code by ChMik
// Led Würfel mit LCD Anzeige und 
// Timer mit akustischem Ton With Arduino
// based on Arduino Led Dice by Davuzz11
// http://www.instructables.com/id/Arduino-Led-Dice/
// and Erstes eigenes Projekt by squaremeter 
// http://forum.arduino.cc/index.php?topic=71395.0

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pinLeds1 = 10;
int pinLeds2 = 9;
int pinLeds3 = 8;
int pinLeds4 = 7;
int buzzer = 6;
int d = 0;
char alarm = 0;
int buttonPin = 16;
int const buttonPin2 = 15;
int const buttonPin3 = 17;
int varpin = A0;
int countrun;
int buttonState;
int buttonState2;
int buttonState3;
boolean debug = 1;
long ran; 
const int rand_sequence[] = {    
  0, 1, 0, 6, 0, 2, 0, 5, 0, 3, 0, 4,
  0, 1, 0, 6, 0, 2, 0, 5, 0, 3, 0, 4,
};
const int rand_sequence_len = (sizeof(rand_sequence) / sizeof(rand_sequence[0]));


int long countdowntime;
int val;
int lastval;
int long minutes;
int long seconds;
volatile unsigned char tick;
int inputtime ;



ISR (TIMER2_OVF_vect)
{
  ++tick;
}

void setup ()
{

  pinMode (pinLeds1, OUTPUT);
  pinMode (pinLeds2, OUTPUT);
  pinMode (pinLeds3, OUTPUT);
  pinMode (pinLeds4, OUTPUT);
  pinMode (buzzer, OUTPUT);
  pinMode (buttonPin, INPUT);
  pinMode(15,INPUT);
  pinMode(17,INPUT);
  int buttonPin = digitalRead(16);
  int buttonPin2 = digitalRead(15);
  int buttonPin3 = digitalRead(17);
  inputtime = analogRead(varpin);
  countdowntime = 0;
  countrun = 1;

  lcd.begin(16, 2);
  lcd.print("T: 01:00  W:");
  lcd.setCursor(3, 0);
  readAin();
  lcd.setCursor(0,1);
  lcd.print("C: 00:00");

  if (debug){
    Serial.begin(9600);
    Serial.print("Setup Complete\n");
  }
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  val = digitalRead(tick);
  Serial.println(countrun);

  if (buttonState2 != HIGH & countrun == 1)
  {
    lcd.setCursor(0,0);
    lcd.print("T: ");
    lcd.setCursor(0, 1);
    lcd.print("C:");

    if (countdowntime<=0 & countrun == 1);
    {

      // tone(buzzer, 140);             //Final Usage ?
      digitalWrite (buzzer, HIGH);
      Serial.print(countdowntime);
      Serial.print(lastval);
    }

    if (countdowntime>0 & countrun == 1)
    {       
      if (val != lastval)
      {
              countdowntime--;

        digitalWrite (buzzer, LOW);
        // noTone (buzzer);

      }

      lcd.setCursor(0,1);
      printTime();


      val = lastval;
      delay(1000);
    }
  } 
  else
  { 
    lcd.setCursor(0,0);
    lcd.print("S: ");

    lcd.setCursor(3, 0);
    readAin();
    countdowntime = inputtime;
    //countrun = 1;

  }


  /* if (buttonState3 == HIGH) {            //Attemp to solve the Countdown-Finish Problem
   
   countdowntime = inputtime;
   countrun = 0;
   
   } */

  if (buttonState == HIGH) {
    randomSeed(analogRead(5));
    ran = random(6) + 1;
        displayRandomSequence();

    // Flash the number a few times
    displayNum(ran);
    delay(200);
    displayNone();
    delay(200);
    displayNum(ran);
    delay(200);
    displayNone();
    delay(200);
    displayNum(ran);
  }

  delay(50);
}

void printTime ()
{
  minutes = countdowntime/60;
  seconds = countdowntime%60;

  if (minutes<10)
  {
    lcd.print("C: 0");
  }

  lcd.print(minutes);
  lcd.print(":");

  if (seconds<10)
  {
    lcd.print("0");
    lcd.print(seconds);
    lcd.write(' ');
  }
  else
  {
    lcd.print(seconds);
    lcd.print(" ");
  }

}

void readAin ()
{
  inputtime = analogRead(varpin);
  int showminutes = inputtime /60;
  int showseconds = inputtime %60;

  if (showminutes<10)
  {
    lcd.print('0');
  }

  lcd.print(showminutes);

  lcd.print(":");

  if (showseconds<10)
  {
    lcd.print("0");
    lcd.print(showseconds);
    lcd.write('  ');
  }
  else
  {
    lcd.print(showseconds);
    lcd.print(" ");
  }
  delay(150);

}

void displayRandomSequence() {
  for (int i=0; i < rand_sequence_len; i++) {
    displayNum(rand_sequence[i]);
    delay(100);
  }
}

void displayNone() {
  // Clear the output
  digitalWrite (pinLeds1, LOW);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, LOW);
  digitalWrite (pinLeds4, LOW);
}

void displayOne() {
  // Display 1
  digitalWrite (pinLeds1, LOW);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, LOW);
  digitalWrite (pinLeds4, HIGH);
}

void displayTwo() {
  // Display 2
  digitalWrite (pinLeds1, HIGH);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, LOW);
  digitalWrite (pinLeds4, LOW);
}

void displayThree() {
  // Display 3
  digitalWrite (pinLeds1, LOW);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, HIGH);
  digitalWrite (pinLeds4, HIGH);
}

void displayFour() {
  // Display 4
  digitalWrite (pinLeds1, HIGH);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, HIGH);
  digitalWrite (pinLeds4, LOW);
}

void displayFive() {
  // Display 5
  digitalWrite (pinLeds1, HIGH);
  digitalWrite (pinLeds2, LOW);
  digitalWrite (pinLeds3, HIGH);
  digitalWrite (pinLeds4, HIGH);
}

void displaySix() {
  // Display 6
  digitalWrite (pinLeds1, HIGH);
  digitalWrite (pinLeds2, HIGH);
  digitalWrite (pinLeds3, HIGH);
  digitalWrite (pinLeds4, LOW);
}

void displayNum(int num) {
  // Displays num
  switch (num) {
  case 1:
    displayOne();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  case 2:
    displayTwo();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  case 3:
    displayThree();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  case 4:
    displayFour();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  case 5:
    displayFive();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  case 6:
    displaySix();
    lcd.setCursor(14,0);
    lcd.print(num);
    break;
  default:
    displayNone();
    break;
  }
  // Serial.println(num);      

}