Multitasking motor and LCD countdown

To multitask with an arduino or something close to it. I am trying to update a LCD (countdown) and and use a microswitch to control a motor at the same time. I’m using while loops to control the motors and if statements for the LCD updating. The LCD will update as soon as the microswitch is released. Is there a way that I can get the LCD to update while in a while loop?

Code is attached.

Draco

simplecode_with_timelimit_lcd.ino (15.6 KB)

Have a look at the demo Several Things at a Time

You should not use WHILE loops because they block until they finish.

...R

Thanks for the help! Is there a way to get an IF statement to act like a WHILE? I have tried a couple ways to change the WHILE.

Will allow the motors to move but blocks countdown

        while (digitalRead(joyup) == LOW)
        {
          analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
        }

Motors will not move at all

        if (digitalRead(joyup) == LOW)
        {
          analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
        }

Will not move the motors but will allow the countdown (Changing the timer interval does nothing)

        if (digitalRead(joyup) == HIGH)
        {
          timestop = millis();
          if ((millis() - timestop) < 8000)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
          }
        }

Will allow countdown, but motors are stop and go as I change the time interval I will lose one or the other. So far I can’t find a happy middle ground.

        if (digitalRead(joyup) == HIGH)
        {
          timestop = millis();
          while ((millis() - timestop) < 600)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
          }
        }

Am I misunderstanding how to use IF and millis together?

          timestop = millis();
          while ((millis() - timestop) < 600)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
          }

How will millis() - timestop ever be greater than 600 ? timestop is never updated in that while loop.
Did you look at the link that Robin gave you ?

UKHeliBob:
How will millis() - timestop ever be greater than 600 ? timestop is never updated in that while loop.
Did you look at the link that Robin gave you ?

Yes. I have tried all of the following and it was the same as a regular IF statement. The motors would not move and the LCD would update.

        if (digitalRead(joyup) == HIGH)
        {
          timestop = millis();
           ((millis() - timestop) < interval)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
            interval += timestop;
          }
        }
 if (digitalRead(joydown) == HIGH)
        {
          timestop = millis();
          if ((millis() - timestop) < interval)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 1);
            timestop ++;
          }
        }
        if (digitalRead(joyup) == HIGH)
        {
          timestop = millis();
          if ((millis() - timestop) < interval)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 0);
            interval ++;
          }
        }
        if (digitalRead(joydown) == HIGH)
        {
          timestop = millis();
          if ((millis() - timestop) < interval)
          {
            analogWrite(PWM_M1, ms);  digitalWrite(DIR_M1, 1);
            timestop += interval;
          }
        }

In all of your examples you set timestop to millis() when the button is pressed. What you need to do is to record the time when the button becomes pressed.

Have a look at the StateChangeDetection example in the IDE

Draco_Elessar:
Thanks for the help! Is there a way to get an IF statement to act like a WHILE?

You need to use the natural repetition of loop() in place of the repetition in a WHILE. Then the IF statement can test for conditions.

There is more example code in Planning and Implementing a Program

Post the complete latest version of your program so we can keep up with you.

...R