Help ... Need help adding data to buffer

Hey you guys,

I’m trying to get data from a depth sensor and sent to it but before i can send it to a file, i need to first of all add the captured and put it a buffer… Any ideas on how to achieve this … ?

The code below is what i used to get the data from the depth and it works perfectly fine… Now all i need is how to the data and put it in buffer … Thanks

#include <SoftwareSerial.h>
SoftwareSerial depthSerial = SoftwareSerial(8,7,true);
int counter = 0;

void setup() {
  Serial.begin(9600);
  depthSerial.begin(4800);

}

void loop() {
  if (depthSerial.available()>0)  {
    char c = depthSerial.read();
    String check = String(c);
     
          if(c == '\n'){ 
                Serial.print(", ");
                counter++;
                    if(counter == 3)
                    {
                      Serial.write(c);
                      counter = 0;
                    }
          }
      
          else{
            Serial.write(c);
          }
    
  }
}

You will need to define a char array like this:

#define MAX_BUFFER_LENGTH 4 // change this to how many bytes you'd like to have in the buffer
char buffer[MAX_BUFFER_LENGTH];

int pos = 0; // current position in the buffer

Push value to the buffer:

buffer[pos++] = value; // copies a value into buffer & increments pos

You need to make sure that you don't write outside the bounds of the buffer. One way to do this is to wrap back to beginning when the buffer gets full:

pos = pos % MAX_BUFFER_LENGTH;

Hope this helps!

and sent to it

Say what? What is "it"?

but before i can send it to a file

Same question.

i need to first of all add the captured and put it a buffer.

Use a butterfly net. If you are trying to capture butterflies. Otherwise, use some English to describe what you are talking about. We aren't all smoking the same stuff you are.

something like this

#include <SoftwareSerial.h>

SoftwareSerial depthSerial = SoftwareSerial(8,7,true);

int counter = 0;
String buffer;

void setup() 
{
  Serial.begin(9600);
  depthSerial.begin(4800);
}

void loop() 
{
  if (depthSerial.available() > 0)  
  {
    char c = depthSerial.read();

    if (c == '\n')
    { 
      buffer += ", ";
      counter++;
      if (counter == 3)
      {
        Serial.println(buffer);  // now the buffer is full, do something with it.
        counter = 0;
        buffer = "";
      }
    }
    else
    {
      buffer += c;
    }

  }
}

Please use CTRL-T before copy/save/posting code → much more readable, thank you