Countdown timer

Hello,

I have created a count down program that works very well. The problem is when the ran time reaches 30sec the program stops working. For example if I have a 10sec (10000millis) countdown it only works for two times in a row and then it stops. What I would like to be able to do is allow the program to ran for as long I decide and definitely over 30sec.

Just to clarify a couple more things about the program. I want to eventually connect it to my phone via bluetooth and have the ability to ran different programs ‘cases’ that’s why ‘case’ is used.

Any advice will be appreciated!

Thomas.

 #include <Servo.h>
Servo s2;
int BUTTON = 11;
int COUNT = 1;
int buzzerPin = 8;
int counter;
unsigned long startTime;

void setup(){ 
Serial.begin(9600);
pinMode(11, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
s2.attach(5); 
}
void loop(){
s2.write(6);
if(Serial.available()>0){     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
     {   
        { 
       case '1':
       Serial.println(" ");
       Serial.println(" Go...Go.. ");
       int duration = 10000;
       int startTime = millis();
       int endTime = startTime + duration;  
while(millis() < endTime) { 
    switch (COUNT);
    int servo = random(5); 
   while(digitalRead(BUTTON) == LOW)   {
 noTone(buzzerPin);
     digitalWrite(13, HIGH);
     digitalWrite(12, LOW);
    s2.write(90);
     delay(10);
  }
if(digitalRead(BUTTON) == HIGH) {
tone(buzzerPin, 600);
    delay(50);
     noTone(buzzerPin);
     digitalWrite(13, LOW);
     digitalWrite(12, HIGH);
    s2.write(6);
     delay(600);
    COUNT ++;
  Serial.print("You hit ");
      Serial.println(COUNT); 
   }
  }
   Serial.println(" ");       
  Serial.println(" Finished: ");
 Serial.println("Your result is: ");
      Serial.print(COUNT);
          Serial.print(" Target in 10 seconds ");
          Serial.println(" ");

            if(COUNT > 0){
              COUNT = 0;
            }
        break;
}
      delay(10);
      }        
      }
  }

The first problem that I see is your use of ints to hold timing values derived from millis() rather than unsigned longs

I have tried it and it works perfectly!!! Thank you!

I also have another question if you could help me with that as well. In case 1 shown in this code I would like to set a timer along with a counter. Is that possible? Thanks again for your help.

 #include <Servo.h> 

///////////// 
Servo s2;  //     #######################
Servo s3;  //     #                     #
Servo s4;
Servo s5;  //     #  (2)    (3)    (4)  #   #######################

Servo s7;
/////////////

Servo exec [] = {s2, s3, s4, s5, s7};

int i = 200;
int value;
 

int BUTTON = 11;

int whichMotors;
int quantatyMotor;
int counter;
int buzzerPin = 8;

int COUNT = 1;



  

void setup(){ 
pinMode(11, INPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
  s2.attach(5); 
 s3.attach(7);
 s4.attach(9);
 s5.attach(3);
  s7.attach(4); 
 

} 

void loop(){
s2.write(6);
  s3.write(6);
  s4.write(6);
  s5.write(6);
  s7.write(6);
  
  if(Serial.available()>0)
  
 {     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
     {   
        {
     
        case '1':
   s2.write(90);
  s3.write(90);
  s4.write(90);
  s5.write(90);
  s7.write(90);
          delay(700);
  s2.write(6);
  s3.write(6);
  s4.write(6);
  s5.write(6);
  s7.write(6);
          delay(1000);
        {     
  for (counter = 0; counter < 5; ++counter) {
     switch (COUNT);
    int servo = random(5);  
  
    while(digitalRead(BUTTON) == LOW) {
  noTone(buzzerPin);
     digitalWrite(13, HIGH);
     digitalWrite(12, LOW);
    exec[servo].write(90);
     delay(10);
    
   }
 if(digitalRead(BUTTON) == HIGH) {
tone(buzzerPin, 600);
    delay(50);
     noTone(buzzerPin);
     digitalWrite(13, LOW);
     digitalWrite(12, HIGH);
    exec[servo].write(6);
     delay(600);
     COUNT ++;
   }
             Serial.print("You hit  ");
       Serial.println(COUNT); 
 }
          Serial.print("Your result is  ");
       Serial.println(COUNT);
           Serial.println("  Target ");
      if(COUNT > 0){
              COUNT = 0;
          }
  break;
      case '2': 
    s2.write(90);
  s3.write(90);
  s4.write(90);
  s5.write(90);
  s7.write(90);
          delay(700);
  s2.write(6);
  s3.write(6);
  s4.write(6);
  s5.write(6);
  s7.write(6);
          delay(1000);
     {        
     Serial.println(" ");
         Serial.println(" Go...Go.. ");
            unsigned long duration = 6000;
       unsigned long startTime = millis();
       unsigned long endTime = startTime + duration;  
          while(millis() < endTime) { 
         switch (COUNT);
    int servo = random(5); 
   while(digitalRead(BUTTON) == LOW)   {
   noTone(buzzerPin);
     digitalWrite(13, HIGH);
     digitalWrite(12, LOW);
    exec[servo].write(90);
     delay(10);
    
  }     
   if(digitalRead(BUTTON) == HIGH) {
tone(buzzerPin, 600);
    delay(50);
     noTone(buzzerPin);
     digitalWrite(13, LOW);
     digitalWrite(12, HIGH);
    exec[servo].write(6);
     delay(600);
    COUNT ++;
  Serial.print("You hit ");
      Serial.println(COUNT); 
   }
  }
    Serial.println(" ");       
  Serial.println(" Finished: ");
 Serial.println("Your result is: ");
      Serial.print(COUNT);
          Serial.print(" Target in 10 seconds ");
          Serial.println(" ");

            if(COUNT > 0){
              COUNT = 0;
            }
        }
          }
   break;
               
 case '3': 
        
  s2.write(90);
  s3.write(90);
  s4.write(90);
  s5.write(90);
  
  s7.write(90);
          delay(700);
  s2.write(6);
  s3.write(6);
  s4.write(6);
  s5.write(6);
  s7.write(6);
          delay(1000);
        for (counter = 0; counter < 2; ++counter) {  
          
          switch (COUNT);
 int servo = random(5);
        
        while(digitalRead(BUTTON) == LOW) {
         noTone(buzzerPin);
     digitalWrite(13, HIGH);
     digitalWrite(12, LOW);
    exec[servo].write(90);
     delay(10);
    
        }      
          if(digitalRead(BUTTON) == HIGH) {
tone(buzzerPin, 600);
    delay(50);
     noTone(buzzerPin);
     digitalWrite(13, LOW);
     digitalWrite(12, HIGH);
    exec[servo].write(6);
     delay(600);
     COUNT ++;
          
          }   
       Serial.print("Your target ");
      Serial.println(COUNT); 
    }
    }
       break;
     case '4': 
    for (i; i>0; i--){
  Serial.print("time ");  
  Serial.println(i);
  delay(1000);
  }
  if (i==0)
{
Serial.print("time ");
  Serial.println("END"); 

  }
    break;
      Serial.println(data);
}
   delay(50);
      }
}

I would like to set a timer along with a counter

What do you mean by this ?

What will you be timing ?
You can, of course, save the current value of miilis() in case '1' and determine whether a period has elapsed somewhere in the program. Is that what you want to do ?

I would like to use a stopwatch at the same time with the counter. I want the stopwatch to start when Case 1 starts and stop when the counter finishes.
Thanks,
Thomas.

So, save the time when case '1' is first entered and the time when the counter finishes and do whatever calculations you need. Using a boolean variable to flag that timing is taking place may also be useful in avoiding starting the timing again when timing is already taking place.

Thank you for the advice I will look into it a bit more and give it a try. I also have another problem with the same program which I just posted, that is a different to this one. If you could please check that out as well I would really appreciate it ('Return of a servo with and without a button').

Thanks!
Thomas