0

I start with this:

char delay_chars[10];

then I have a stream of incoming characters that I need to assign like this:

['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'e']

I can do that, but then more characters come in so I need it like this:

[' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'e', ' ']
['l', 'o', 'v', 'e', ' ', 'p', 'i', 'e', ' ', 'a']
['o', 'v', 'e', ' ', 'p', 'i', 'e', ' ', 'a', 'n']
['v', 'e', ' ', 'p', 'i', 'e', ' ', 'a', 'n', 'd']
['e', ' ', 'p', 'i', 'e', ' ', 'a', 'n', 'd', ' ']
[' ', 'p', 'i', 'e', ' ', 'a', 'n', 'd', ' ', 'c']
['p', 'i', 'e', ' ', 'a', 'n', 'd', ' ', 'c', 'a']
['i', 'e', ' ', 'a', 'n', 'd', ' ', 'c', 'a', 'k']
['e', ' ', 'a', 'n', 'd', ' ', 'c', 'a', 'k', 'e']

You get the idea. I don't know how I can remove the first character of a char array (like I do with a String, remove(0, 1)) and I don't know how to add a new element at the end.

If I had to guess, I'd make a loop that will reassign the index of each character one step back or something like that but I'm not sure if that's how I should do it.

  • You can do it this way, but there's a better way that's faster and a lot less work. You want something called a circular buffer. Look that up and see if it doesn't do what you want differently but better. – Delta_G Mar 14 at 15:24
1

If I had to guess, I'd make a loop that will reasign the index of each character one step back or something like that but I'm not sure if that's how I should do it.

Yep, that's pretty much it...

Here's how I do it:

char data[10];

if (Serial.available()) {
    char inch = Serial.read();
    for (int i = 0; i < 9; i++) {
        data[i] = data[i+1];
    }
    data[9] = inch;
}

Every incoming character gets added to the end of the array after everything else has been shuffled down one place.

| improve this answer | |
  • It's pretty frustrating that in C you have to do all array stuff yourself – JingleBells Mar 14 at 14:42
  • 1
    There are functions that can do it for you, but it's not really worth using them for such a simple operation. The for loop could be done with memmove(data, data+1, 9); – Majenko Mar 14 at 14:54
  • @NovaliumCompany By the way, I just corrected a typo. – Majenko Mar 14 at 14:55
  • Ok. I now have another annoying problem... my Arduino Uno seems to start to spit jibberish when I do some stuff... Do you mind helping me? (in a chatroom) – JingleBells Mar 14 at 14:57
  • @NovaliumCompany Jibberish usually means you're overrunning a buffer. Maybe you're printing an array with no terminating NULL character? – Majenko Mar 14 at 16:42
0

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';

  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

You can easily edit the above example code to transfer ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'i', 'e'] instead of ['h', 'i', '\n'].

| improve this answer | |

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.