Need help formatting output!!!!

Hey guys,

I need help displaying data from a depth sensor in a single line. By default, the depth sensor writes out three different lines of values ( $SDMTW, $SDDBT, $SDDPT)each on a single line( pls see image attached) every time its reading data. I'm trying to change the output so that each of those three values would be on a single line like so:

$SDMTW, 31.4, C02 - $SDDBT, 1.8, F,0.5,M, 0.2, F08 - $SDDPT,0.5,7C
$SDMTW, 31.4, C
02 - $SDDBT, 1.8, F,0.5,M, 0.2, F08 - $SDDPT,0.5,7C
$SDMTW, 31.4, C
02 - $SDDBT, 1.8, F,0.5,M, 0.2, F
08 - $SDDPT,0.5,*7C
ETC.

Any ideas on how i can achieve this ? Thanks a lot you guys.

The code below is what i used the generate the image attached .

 #include <SoftwareSerial.h>
 
 SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
 void setup() {
   Serial.begin(9600);
   depthSerial.begin(4800);
 }
 
 void loop (){
      if (depthSerial.available()>0) {
       Serial.write(depthSerial.read());
      }
 }

Instead of printing directly to serial, build a char array with the result of depthSerial.read(), but ignore, and replace with a space, the EOL character(s) (depends what your device uses) before "$SDMTW" is found. Then you use Serial.write( char_array );

Would love it if you could tell me how go about it, really haven't done like this before now... really appreciate you replying ..

lionel007:
Would love it if you could tell me how go about it, really haven't done like this before now... really appreciate you replying ..

By writing code. Show some effort, don't just ask for the code. When you write code, we can see how you think code works and correct your understandings so that you can get better at writing it yourself. When we write the code, you simply just take it, use it and don't bother learning how it works.

No .. not me.. This is a personal thing I'm actually doing for myself cos I'm really trying hard to learn how this stuff. Like I said earlier I'm really new to arduino and this is the only place i know i can help. I have been on this issue for the past few days and still now show, just had to come on here and hope someone could help me out...

Im not saying you should write the entire program for me, i just need something to start with and im pretty sure i would take it off from there

lionel007:
i just need something to start with

Start by storing the character in a variable instead of displaying on the screen:

 void loop (){
      if (depthSerial.available()>0) {
        inChar = depthSerial.read();
      }
 }

At that point, you can count the number of '$' you see.

if (inChar == '

Then, when you receive a new line, don't print it if numSigns is less than 3. If you do print it, reset numSigns to 0.)
{
 numSigns++;
}


Then, when you receive a new line, don't print it if numSigns is less than 3. If you do print it, reset numSigns to 0.

thanks for help so far ... this is what i've come up with but obviously something is wrong cos its not displaying anything on the serial monitor ... I'm i missing something ??

 #include <SoftwareSerial.h>
 

 
 SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
 void setup() {
   Serial.begin(9600);
   depthSerial.begin(4800);
 }
 
 void loop (){
   
   char c;
   int numSigns = 0;
   
      if (depthSerial.available()>0) {
        
        c = depthSerial.read();
        
        if(c == '

)
        {
          numSigns++;
        }
       
        while(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
            numSigns = 0;
        }
       
     
}

}

lionel007:
thanks for help so far ... this is what i've come up with but obviously something is wrong cos its not displaying anything on the serial monitor ... I'm i missing something ??

So start by asking yourself when does it print something to the serial monitor?

        while(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
            numSigns = 0;
        }

This is the only time there is ever a Serial.write. So what are the conditions that will allow Serial.write to be executed? numSigns must be 3 and the character must be a new line. That means that you will only ever write every third new line to the serial monitor. No other character other than new lines get written to the Serial monitor.

numSigns should also be declared as static or global. If you set it to 0 before you read the next character, like you are now, it will only ever be 0 and 1 because it resets on every loop.

You should be doing a series of if/elseifs that performs actions based on what the character is, and what numSigns is set to.

Lastly, why are you using a while instead of an if? You're just wasting time checking to see if it will run again when it won't.

thanks once again, made some modification but still can't seem to get it ... whats wrong??

 #include <SoftwareSerial.h>
 

 int numSigns = 0;
 SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
 void setup() {
   Serial.begin(9600);
   depthSerial.begin(4800);
 }
 
 void loop (){
   
   char c;

   
      if (depthSerial.available()>0) {
        
        c = depthSerial.read();
        
        if(c == '

)
        {
          numSigns++;
        }
       
        if(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
         
        }
        numSigns = 0;
     
}

}

        if(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
          
        }
        numSigns = 0;

Why did you move the line of code out of the if statement? Now numSigns is set to 0 every time a character is received. How will it ever be more than 0?

And you didn't address the issue of only printing newline characters. This is why it was suggested that you use an if/else if structure. Draw a flowchart to work the logic out if it helps. You could also manually walk through your code, keeping track of the variables through each iteration. One thing that is sure to get your nowhere is making random changes to code without understanding what you're doing, trying it, and posting "nope, still doesn't work" after each attempt.

I'm sorry i thought the statement below checks to see if its at a new line and numSigns = 3 ... if its true then it should go ahead and print

  if(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
            numSigns = 0;
        }

This is what i've got now ...

 #include <SoftwareSerial.h>
 

 int numSigns = 0;
 SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
 void setup() {
   Serial.begin(9600);
   depthSerial.begin(4800);
 }
 
 void loop (){
   
   char c;

   
      if (depthSerial.available()>0) {
        
        c = depthSerial.read();
        
        if(c == '

)
        {
          numSigns++;
          c = depthSerial.read();
        }
     
       
        else if(c == '\n' && numSigns == 3)
        {
            Serial.write(c);
              numSigns = 0;
         
        }
     
     
}

}

lionel007:
I'm sorry i thought the statement below checks to see if its at a new line and numSigns = 3 ... if its true then it should go ahead and print

It does, but what if it's not a new line character? You're not printing any other characters, only new lines.

You should also fix the formatting of your code. Their is way too many blank lines between the various statements. Remove the excess blank lines and use Tools > Auto-Format to fix the indenting

check it the its starts with a $, increment the numSigns, else if its now a new line read it and final if its at last line and numSigns = 3, go ahead and print out the code ... Right???

#include <SoftwareSerial.h>


int numSigns = 0;
SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
void setup() {
  Serial.begin(9600);
  depthSerial.begin(4800);
}

void loop (){

  char c;


  if (depthSerial.available()>0) {

    c = depthSerial.read();

    if(c == '

)
    {
      numSigns++;
      c = depthSerial.read();
    }
    else if( c != '\n')
    {

c = depthSerial.read();
    }

else if(c == '\n' && numSigns == 3)
    {
      Serial.write(c);
      numSigns = 0;

}

}

}

lionel007:
check it the its starts with a $, increment the numSigns, else if its now a new line read it and final if its at last line and numSigns = 3, go ahead and print out the code ... Right???

Nope. Reading serial data shouldn't be conditional upon what the last character was. The only time you should be reading Serial data is when there is Serial data available to be read. This will be like the 3rd post that you've not addressed the issue of only printing new line characters.

is this what you mean ???

#include <SoftwareSerial.h>


int numSigns = 0;
SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
void setup() {
  Serial.begin(9600);
  depthSerial.begin(4800);
}

void loop (){

  char c;


  if (depthSerial.available()>0) {

    c = depthSerial.read();

    if(c == '

)
    {
      numSigns++;
      c = depthSerial.read();
    }
    else if( c == '\n')
    {

c = depthSerial.read();
    }

else if(c == '\n' && numSigns == 3)
    {
      Serial.write(c);
      numSigns = 0;

}

}

}

I've done everything you said .. why am i still not getting this ??? I really need your help

lionel007:
I've done everything you said

No, you haven't. You've failed to address these points:

Reading serial data shouldn't be conditional upon what the last character was. The only time you should be reading Serial data is when there is Serial data available to be read.

you've not addressed the issue of only printing new line characters.

Possibly a simple way to capture data packets that are delimited with a \n\r into a String.

//zoomkat 7-30-13 simple delimited '\n' String parcecapture test code
//from serial port input (via serial monitor with NL&CR enabled)
//and print result out serial port
// \n\r is the data packet delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like $SDMTW, 31.4, C*02
  //followed by \n\r end of packet delimiter
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '\n') {  //looks for end of data packet marker 
      Serial.read(); //gets rid of following \r 
      Serial.println(readString); //prints string to serial port out
      
      //do stuff with captured readString
      
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

I know what you mean but i don't know how to write the code ... thats the truth .. I really appreciate you helping out so far ... really grateful. I don't know how to go about it ..

It may save some time and effort if you explain why you want all the three data packets saved in a single line separated by a hyphen.