facing issues with while loop

Hi
I’m trying to develop one code in which I’m facing issue with while loop. Once I enter any newString it enter in while loop it count till 10 times, when (n>10) then it is coming out from the while loop.
Facing issues in this part

while (n < 10)
  {
    while (!ret) {
      if ((time + 15) < millis()) {
        ret = dmd.stepMarquee(-1,0);
        time = millis();
      }
    }
     ret = false;
     n++;
    }

I just want when I enter any newString when loop is running it break while loop and start display newString. If any other way to do this then also please suggest me.

Please help me out with this. Thanks for your help, greatly appreciated.

Here is my full code:

#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"

//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 2
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

String inputString = "";
boolean stringComplete = false;
String commandString = "";


void ScanDMD()
{ 
  dmd.scanDisplayBySPI();
}


void setup()
{
   Serial.begin(9600);
   Serial.println("Arduino Start");
   Timer1.initialize( 5000 );
   Timer1.attachInterrupt( ScanDMD );
   dmd.clearScreen( true );
}

void loop() {

if(stringComplete)
{
  stringComplete = false;
  getCommand();
  
  if(commandString.equals("STAR"))
  {
   dmd.clearScreen( true );
  }

  else if(commandString.equals("PRSC"))
  {
    String text = getTextToPrint();
    printText(text);
  }
  inputString = "";
}

}


void getCommand()
{
  if(inputString.length()>0)
  {
     commandString = inputString.substring(1,5);
  }
}


String getTextToPrint()
{
  String value = inputString.substring(5,(inputString.length())-2);
  return value;
}

void printText(String text)
{
  char newString[256];
  int sLength = text.length();
  text.toCharArray( newString, sLength+1 );
  dmd.selectFont(System5x7);
  dmd.drawMarquee( newString , sLength , (32*DISPLAYS_ACROSS )-1 ,1);
  unsigned long time;
  boolean ret = false;
  time = millis();
  int n;
  n= 0;
  while (n < 10)
  {
    while (!ret) {
      if ((time + 15) < millis()) {
        ret = dmd.stepMarquee(-1,0);
        time = millis();
      }
    }
     ret = false;
     n++;
    }
} 

void serialEvent() {        
  while (Serial.available()) {            // get the new byte:
    char inChar = (char)Serial.read();    // add it to the inputString:
    inputString += inChar;                // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

DMD_ask_question.ino (1.98 KB)

So you seem to have a reasonably complex system there. You have a DMD display, which seems to be made by Freetronics. That display works in a slightly-unusual way from most displays. It has a lot of helper functions like stepMarquee() which do things that would require you to write much more code on other types of displays.

It looks like you can enter some text and it will scroll in “marquee” fashion for 10 repetitions, then it’s ready to receive more commands. But you want to be able to interrupt the repetitions and enter a new command at any time? Am I right?

So don’t use a while loop. Make your main loop check the timer and advance the marquee when required.

Additionally, don’t use addition when testing times. Always use subtraction. In the maths they teach you at school the following two expressions are identical but in unsigned integer maths used inside the Arduino, they are very different:

      if ((time + 15) < millis()) { //works most of the time
      if (millis() - time > 15) { //works all of the time

read through this old thread which is basically the same question and contains answers

blh64:
read through this old thread which is basically the same question and contains answers

In my case it is not working.

Please suggest me what I'have to change in my code.

Thanks for your help, greatly appreciated.

MorganS:
It looks like you can enter some text and it will scroll in "marquee" fashion for 10 repetitions, then it's ready to receive more commands. But you want to be able to interrupt the repetitions and enter a new command at any time? Am I right?

Exactly you are right.

Can you please suggest me what I have to change in my code.

Please help me out with this. Thanks for your help, greatly appreciated.

Take that tutorial and get it working with the Arduino's onboard LED first. Then add just enough of the Freetronics code to put a fixed message on the DMD display and "marquee" it with each blink of the LED.

Keep modifying the code until you understand how it works - change the speed, change a few other things.

Then apply the core of that example into your original program, in the loop() function. Remove the while().