0

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.

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;
    }
  }
}
3
  • what is the purpose of the while loop? ... how do you want the program to behave?
    – jsotola
    Oct 17 '19 at 4:36
  • I'm sending the data to Arduino UNO via serial(Keyboard) and then UNO is to turn expected to scroll the received text on P10 DMD display. Whenever it receive new serial data it start displaying that data into display. I want this. Oct 17 '19 at 6:50
  • scroll the received text ... you have not clearly defined the desired operation... serial characters arrive one at a time ... keep that in mind when you decide how you want the character scrolling to work
    – jsotola
    Oct 17 '19 at 8:27
0

Forget using serialEvent(). It is one of the more stupid things that Arduino implemented. It is nothing more than a function that gets called (wrapped in a check to Serial.available()) once per iteration of the main loop() function.

It is not really an "event" function at all.

To have anything happen inside your while loop you have to manually implement that thing within your while loop. Nothing outside of the while loop (with the exception of interrupts) can affect the operation of the while loop.

2
  • So is there any possibilities to change in code?? Please help me out Oct 17 '19 at 0:55
  • There are plenty of possibilities. Personally I would ditch the while loop completely.
    – Majenko
    Oct 17 '19 at 11:49
0

Now my code is working like I'm sending the data to Arduino UNO via serial(Keyboard) when it receive any serial data it scroll till 10 time and when (n>10) then will loop stop. In between this whenever I'm send any new data it is not accepting.

what exactly I want whenever I send new data to Arduino UNO via serial(Keyboard) and then UNO is to turn expected to scroll the received text on P10 DMD display at the same time. Whenever it receive new serial data it start displaying that data into display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.