Serial Read, not able to go to new line or place spaces between numbers - solved

EDIT: see post #8

solved with help from the first post, to avoid starting new thread I'm asking a few questions about best serial practices for fast processing

Original:
Hey guys so I'm having so many problems with serial read. I have had it doing numerous things that I want, including:

  • read text
  • read numbers

and it has also done numerous things I did not want:

  • no space between characters
  • no space between words
  • number 13 between numbers
  • unable to use serial.readstringuntil('13')
  • many more

I tried the help page/documentation, and got things a little different, but still not how I want them.

Here's the arduino code:

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
String a;

void setup() {
  // set up the LCD's number of columns and rows:
  Serial.begin(9600);           // set up Serial library at 9600 bps
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
  
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  while(Serial.available()) {

  a= Serial.readString ;// read the incoming data as string

 // String a = String(13, HEX);

  Serial.print(a);
  lcd.print(a);
  
}}

My matlab code has also gone several revisions and my head is just spinning at this point:

% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM8', 'Tag', '');

% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM8','BaudRate',9600,'Terminator','CR')
else
    fclose(obj1);
    obj1 = obj1(1)
end

% Connect to instrument object, obj1.
fopen(obj1);

% Communicating with instrument object, obj1.
%data1 = query(obj1, '*IDN?');

% Disconnect from instrument object, obj1.

totaltests=9999
test=1
pause on % to enable pause function


while test<totaltests
    fprintf(obj1,test)
    test=test+1;
    pause(0.1); %in seconds, but actually doesn't works
end

fclose(obj1);
% Clean up all objects.
delete(obj1);

Where do I go from here?

I can't help with the Matlab code. I can recommend the serial input basics tutorial.

To be fair, it couldn't do any of those things.

In fact that's what makes serial comms so enduring and flexible.
It doesn't care what is being sent - binary data, text, or any other formatted data you want.

If 'serial line' refers to 'physical TX/RX' line of the UART Port, then 'Serial.read()' gets data from 'Serial buffer' and not from 'serial line'.

Delta_G:
I will say thank-you for not taking half a mile of thread to say that.

Slowly learning the art!(+)

+1

GolamMostafa:
If 'serial line' refers to 'physical TX/RX' line of the UART Port, then 'Serial.read()' gets data from 'Serial buffer' and not from 'serial line'.

I was unaware of this distinction. I need immediate as possible execution of data sent over serial, I figured a possible way with the link sent in the first post, and I'll integrate it with the rest of my code, I would just like to start from the fastest possible way to use it. Here's what I got that works:

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
   Serial.begin(9600);
   Serial.println("<Arduino is ready>");
}

void loop() {
   recvWithEndMarker();
   showNewData();
}

void recvWithEndMarker() {
   static byte ndx = 0;
   char endMarker = 'ASCII 13';
   char rc;
   
   while (Serial.available() > 0 && newData == false) {
       rc = Serial.read();

       if (rc != endMarker) {
           receivedChars[ndx] = rc;
           ndx++;
           if (ndx >= numChars) {
               ndx = numChars - 1;
           }
       }
       else {
           receivedChars[ndx] = '\0'; // terminate the string
           ndx = 0;
           newData = true;
       }
   }
}

void showNewData() {
   if (newData == true) {
       Serial.print("This just in ... ");
       Serial.println(receivedChars);
       newData = false;
   }
}

I'm basically using MATLAB to analyze incoming waveforms, and determine the frequency. It will then send this data via serial port to the arduino, which will try and arrive either at the same time as the peak of the wave, slightly before or slightly after.

HZ range is 0.5 to 100 hz. Should be do-able I think. I think that matlab, and my coding there may be the limiting factor. for, if, and While loops, analyzing all the waveforms from multiple channels, etc. may take a long time. I'm not sure, I've never written a program this big in matlab or used these features/functions

I'm going to need so many outputs on the arduino, 48 to be exact. for one I need to control 16 RGB LED's to help visualize the wave forms (not required) and for another function, the output of the wave input to matlab over the serial port, I need 96 channels total if at all possible. I'm switching mosfets and I need one to bring - voltage to one of 48 pins, and one to bring positive voltage to one of 48 pins. Thus the mosfets.

for one I need to control 16 RGB LED's to help visualize

For A LOT of WS281x or similar RGB ‘neopixels’, you only need ONE pin.