Missing test for <CR> ???

HI,

On my ARD 2560 Mega I use port A to port D in a project, in which I plan to control an external TTL circuit. I do that by sending a command to port A, to port B … and to port D or by sending a command (integer) to the specific port.

(For test purposes I have 2 * 8 leds connected to port B and C).

EG:

A:255,B:255,C:23,D:45 (all ports addressed) , or

A:255,B:255 (port A and B addressed) or

B:255,D:32 ( port B and D addressed) or

B:255 (port B addressed )

Port addressing will always be in order A B C D

Here is my code:

#include <Esplora.h>

void setup()
{
    Serial.begin(9600);
    DDRA = B11111111;       /* PORT A = OUTPUT */
    DDRB = B11111111;       /* PORT B = OUTPUT */
    DDRC = B11111111;       /* PORT C = OUTPUT */
    DDRD = B11111111;       /* PORT D = OUTPUT */

    PORTA = B00000000;
    PORTB = B00000000;
    PORTC = B00000000;
    PORTD = B00000000;
}

void loop()
{
  if (Serial.available() > 0)
  {
    String pport = Serial.readStringUntil(':');
    {
      String pos = Serial.readStringUntil(',');
      if (pport == "A")
      {
        Serial.print("Pport = A value = ");
        Serial.println(pos.toInt());
        PORTA = pos.toInt();
      }
      else
      if (pport == "B")
      {
        Serial.print("Pport = B value = ");
        Serial.println(pos.toInt());
        PORTB = pos.toInt();
      }
      else
      if (pport == "C")
      {
        Serial.print("Pport = C value = ");
        Serial.println(pos.toInt());
        PORTC = pos.toInt();
      }
      else
      if (pport == "D")
      {
        Serial.print("Pport = D value = ");
        Serial.println(pos.toInt());
        PORTD = pos.toInt();
      }
    }
  }
}

The lines:
Serial.print("Pport = A value = ");
Serial.println(pos.toInt());

are included for test-purposes only.

When I do this: B:23,C:25

the leds in port B illuminates immidiately and after 1/2 sec (approx.) the
leds in port C illuminates.

And when I do this: B:23,C:25,D:45

the leds in both port B and C illuminates immidiately.

I guess theat the problem is that I am not testing for the ("\n") in

String pos = Serial.readStringUntil(’,’); line.

Can anyone help me ?

And can I optimize this code ?

snestrup2016 AKA Kris

Hi,

snestrup2016:
I guess theat the problem is that I am not testing for the ("\n") in

String pos = Serial.readStringUntil(’,’); line.

Yeh, readStringUntil() can only look for one terminator, doesn’t find it and times out after a while.

You could add another ‘,’ or maybe change your format to something like:

A(255)B(255)C(23)D(45)

…so you always have a terminator to look for; the ‘)’ in this example.

At the end you can then readStringUntil( ‘\n’ ) so you are ready for the next line (if there is one)

Yours,
TonyWilk

HI,

Thanks for the addendum/terminator to my code (additional ',' ) in A:255, ... D:255 ,

No problems in adding it to the command-line.

Tried it. Works great.

No delays (alt least measurable ) now.

Thanks again.

snestrup2016 AKA Kris

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R