Does this loop stop running?

Apologies, this is quite a bit of code and it’s not all of it…

This code has a countdown timer in it and it closes a relay at the start when I push a button if (sensorVal == LOW) { and starts the count. When it counts to zero, it opens the relay and prints “PROGRAM FINISHED”

I use this “if statement” as I want to use three buttons to run three individual timers to control 3 relays. I will work out how to swop my display between the three timers later. First, I want to be able to restart one timer after it finishes by pressing the button again. I will have three timers running at the same time.
Problem: Once it’s run, the button has no more effect, I need to reset the board to get it going again… It only runs once?

void loop () {
 lcd.begin(16, 2);
 lcd.clear(); // clears the screen and buffer
 lcd.print("Push Button "); 
 int sensorVal = digitalRead(Button1);
 Serial.println(sensorVal);  //print out the value of the pushbutton

// Wash starts here 
if (sensorVal == LOW) {
  sensorVal = LOW;

 lcd.clear(); // clears the screen and buffer
 lcd.print("Washing Machine ");

 // wait a bit:
 delay(2000);

 digitalWrite(MotorControl1,HIGH); // NO3 and COM3 Connected (the relay is closed)

 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();

 if (seconds > 0) {
 seconds -= 1;
 } else {
 if (minutes > 0) {
 seconds = 59;
 minutes -= 1;
 } else {
 if (hours > 0) {
 seconds = 59;
 minutes = 59;
 hours -= 1;
 } else {
 digitalWrite(MotorControl1,LOW); // NO3 and COM3 Disconnected (the relay is not open)
 lcd.clear(); // clears the screen and buffer
 lcd.setCursor(0, 1); // set timer position on lcd for end.
 lcd.println("PROGRAM FINISHED");
 digitalWrite(Button1, HIGH);
        }
      }
    }
      delay(1000);  // This is actually the count by second!!
  }
}
// Wash stops here
}

Another little problem, the code

lcd.begin(16, 2);
 lcd.clear(); // clears the screen and buffer
 lcd.print("Push Button ");

Renders the test “Push Button” on the LCD but for some reason, it flickers and the second line flickers with no text in it?

Any help/advice will be gratefully accepted.
Thanks

Partial and/or badly formatted code will not give you much help. If you have a sketch which has multiple functions, it would be easy to do something like this:

int currentProgram = 0;
unsigned long programStarted = 0, runTime;
unsigned int hours, minutes, seconds;

void setProgram(int prog) {
  currentProgram = prog;
  programStarted = millis();
}

void calcRunTime() {
  runTime = millis() - programStarted;
  seconds = runTime / 1000;
  minutes = seconds / 60;
  seconds %= 60;
  hours = minutes / 60;
  minutes %= 60;
}

void loop() {

  if (currentProgram == 0) {

    if (readButton()) setProgram(1);

  } else if (currentProgram == 1) {

    calcRunTime();

    if ((hours == 1) && (minutes == 30) && (seconds == 15)) {

      setProgram(0);

    }

  }

}

This code will start with program 0 which means that it is waiting for a button to be pressed. When the button is pressed, go to program 1. When program 1 has been running for 1 hour, 30 minutes and 15 seconds, it reverts to program 0.

EDIT: Btw, you should not call "clear()" on the LCD unless it is necessary. It will cause flicker since the LCD has a very slow refresh rate. Instead you should just print new text as needed, but avoid printing the same text for each loop.

EDIT2: Oh, and "lcd.begin(16, 2);" belongs int the "setup()" void since it should only be called once.

if (sensorVal == LOW) {
  sensorVal = LOW;

If it's low, make it low. Uh, ok.

digitalWrite(Button1, HIGH);

digitalWrite to an input pin?

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

If 'seconds' is an unsigned type this loop will go on forever.

Your timing will be a little slow because your delay(1000) gets added to the time it takes to run the code in the loop. The BlinkWithoutDelay example shows how to run code at a known interval.

evanmars:

digitalWrite(Button1, HIGH);

digitalWrite to an input pin?

Enabling the pullup resistor, maybe? :wink:

the lcd problem:
it should be lcd.begin (0,2)

the lcd problem:
it should be lcd.begin (0,2)

Really?

devcoder:
the lcd problem:
it should be lcd.begin (0,2)

Wow. An LCD with 2 rows of zero characters

Yes, the LCD solution given "lcd.begin (0,2)" makes no sense and doesn't work, obviously.
Putting "lcd.begin(16, 2);" in the setup doesn't work either, just screws up the display. Tried it before, but did it again and still doesn't work?

Repeating "digitalWrite to an input pin?" leaves me none the wiser? I do this as what I read yesterday on some post in this forum, I can't enable the internal pullup resistor in my sketch as my IDE is too old? Cant upgrade as my OS is too old? "http://forum.arduino.cc/index.php?topic=5313.0"

I'm new at this so I do what I'm told.

Apologies, putting "lcd.begin(16, 2);" in the setup does work. I just needed to make sure I cleared the screen before I start? Can't figure why, but it works.

You must have another issue if "lcd.begin" does not work in setup.

const char MSG1[17] = " HELLO          ";
const char MSG2[17] = "          HELLO ";

byte first = 0;
unsigned long lastChange = 0;

void setup() {
  lcd.begin(16,2);
  lcd.clear();
}

void loop() {
  if ((lastChange == 0) || (millis() - lastChange >= 1000)) {
    lcd.setCursor(0, 0);
    lcd.print(first == 0 ? MSG1 : MSG2);
    lcd.setCursor(0, 1);
    lcd.print(first == 0 ? MSG2 : MSG1);
    first ^= 1;
    lastChange = millis();
  }
}

Your method of enabling the pullup resistor is correct, "evanmars" made a mistake.

fatman1961:
Repeating "digitalWrite to an input pin?" leaves me none the wiser? I do this as what I read yesterday on some post in this forum, I can't enable the internal pullup resistor in my sketch as my IDE is too old? Cant upgrade as my OS is too old? "http://forum.arduino.cc/index.php?topic=5313.0"

Are you sure you weren't told to digitalWrite() to the input pin in setup() and not at the end of loop() after you have already done a digitalRead() to the pin at the beginning of loop()?

Actually, I just read that thread. I guess it doesn't really say where to put it. My apologies.
But, put it in setup()