62Views1Replies

Author Options:

Countdown timer doesn't exit and the loop never repeats? Answered

We want to run the stepper motor by 180 degrees and want to repeat it after long delay (say 2 hours or so) with a countdown timer displayed in LCD attached to Arduino. Every thing happening successfully except the loop doesn't start after the countdown ends.

#include <Stepper.h>
#include <LiquidCrystal.h>


const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 10; //start seconds

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.

}

void loop() {
  // set the cursor to column 0, line 0
  // (note: line 0 is the first row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print("DRUM ROTAING");
  for (int i = 0; i <= 100; i++) {
    // step one step:
    myStepper.step(1);
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
    delay(125);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.begin(16, 2);
  lcd.print("Count Down Timer ");
  delay(150);

  while (hours > 0 || minutes > 0 || seconds >= 0) {

    lcd.setCursor(4, 2);

    (hours < 10) ? lcd.print("0") : NULL;
    lcd.print(hours);
    lcd.print(":");
    (minutes < 10) ? lcd.print("0") : NULL;
    lcd.print(minutes);
    lcd.print(":");
    (seconds < 10) ? lcd.print("0") : NULL;
    lcd.print(seconds);
    lcd.display();
    stepDown();
    delay(1000);
  }
}

void stepDown() {
  if (seconds > 0) {
    seconds -= 1;
  } else {
    if (minutes > 0) {
      seconds = 59;
      minutes -= 1;
    } else {
      if (hours > 0) {
        seconds = 59;
        minutes = 59;
        hours -= 1;
      } else {

      }
    }
  }
}

We used break, return 0, continue, etc. but somehow didn't work. Kindly tell how to exit so that program repeats itself. Thanks.

Discussions

The forums are retiring in 2021 and are now closed for new topics and comments.
0
steveastrouk
steveastrouk

4 years ago

Put everything you want in the infinite loop in an infinite loop, like while (1=1) or

while (TRUE)