how can I copy a character arrays into an other.

Im still working with that lcd tft for the esplora, but in order to print a new message I need to delete the old one by overwriting it with the same message in the background colour that must go as follows:

EsploraTFT.stroke(255, 255, 255);
EsploraTFT.text(old_buf, 10, 10); // delete what is on the screen

EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text(buf, 10, 10); // print the new message.

Now I have a problem that I cannot succesfully transfer the information of buf to old_buf.
I have tried the easy:

old_buf[Len] = buf[Len]; << this gives bugs, single random characters and an extra line skipping

I have tried the hard:

for (int i = 0; i < Len; i++) {
old_buf = buf*; << does not do anything and I dont know why. I know that buf is filled*
* }*
Im using for debugging:
* Serial.println(old_buf); << gives me a white empty rule*
* Serial.println(buf); << prints as it should*
It seems that the for-loop does not copy the values as I want it to.
Who sees the obvious mistake here? :confused:

bask185:
for (int i = 0; i < Len; i++) {
old_buf = buf*; << does not do anything and I dont know why. I know that buf is filled*
* }*

[/quote]
forum does not display the [ i ] but it is there

forum does not display the [ i ] but it is there

It would show if you put the code in code tags. Seventh icon from the right above the smileys in the editor window.

How/where is Len defined ?

 #include <Esplora.h>
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

byte Len;

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

  // initialize the LCD
  EsploraTFT.begin();

  // start out with a white screen
  EsploraTFT.background(255, 255, 255);

}

void loop()
{
  byte var = 1;

  String tekst = "";                    // initialises empty string every time
 
  if (Serial.available()) {          // when i send message
    delay(10);                         // wait for message to get in the buffer before proceeding
    while (Serial.available()) {

      if (var == 1) {
        Len = Serial.available() + 1;    // will be the length for the buffer in the following code
        var = 0;
      }

      char c = Serial.read();             // read a byte as char
      tekst += c;                     // adds the char to the empty string
    }
    char buf[Len];                     // declare character array with size 'Len'
    char old_buf[Len];
    tekst.toCharArray(buf, Len);  // Esplora.tft.text() just won't accept String object, so I go back to character array

    Serial.println(buf);
    Serial.println(old_buf);

    EsploraTFT.stroke(255, 255, 255);
    EsploraTFT.text(old_buf, 10, 10);

    EsploraTFT.stroke(0, 0, 0);
    EsploraTFT.text(buf, 10, 10);    // print the bloody message

    for (int i = 0; i < Len; i++) {
      old_buf[i] = buf[i];
    }
  }
}

UKHeliBob:
It would show if you put the code in code tags. Seventh icon from the right above the smileys in the editor window.

How/where is Len defined ?

Len = Serial.available() + 1;
it is the length of the message, and I need to add one extra for the character array requires it. without +1. it wont display the last character

That code is nonsense.

  if (Serial.available()) {          // when i send message
    delay(10);                         // wait for message to get in the buffer before proceeding
    while (Serial.available()) {

      if (var == 1) {
        Len = Serial.available() + 1;    // will be the length for the buffer in the following code
        var = 0;
      }

      char c = Serial.read();             // read a byte as char
      tekst += c;                     // adds the char to the empty string
    }

If you sent properly delimited data, you could read and store the data, as it arrives, as fast as it arrives, without hoping that it all gets there before you read any of, while you've stuck your thumb up your ass.

You KNOW how much data you read. You've pissed away resources using the String class. The tekst instance KNOWS how many characters it contains. Ask it. Don't expect to capture the length in Len. What are you going to to when more data arrives while you are reading data, and the value in Len doesn't match the number of characters in tekst?

You are not copying the NULL terminator into old_buf. Why not?

Since you are not, old_buf is NOT a string, and you should NOT be passing it to functions that expect strings.

PaulS:
That code is nonsense.

  if (Serial.available()) {          // when i send message

delay(10);                        // wait for message to get in the buffer before proceeding
    while (Serial.available()) {

if (var == 1) {
        Len = Serial.available() + 1;    // will be the length for the buffer in the following code
        var = 0;
      }

char c = Serial.read();            // read a byte as char
      tekst += c;                    // adds the char to the empty string
    }



If you sent properly delimited data, you could read and store the data, as it arrives, as fast as it arrives, without hoping that it all gets there before you read any of, while you've stuck your thumb up your ass.

You KNOW how much data you read. You've pissed away resources using the String class. The tekst instance KNOWS how many characters it contains. Ask it. Don't expect to capture the length in Len. What are you going to to when more data arrives while you are reading data, and the value in Len doesn't match the number of characters in tekst?

You are not copying the NULL terminator into old_buf. Why not?

Since you are not, old_buf is NOT a string, and you should NOT be passing it to functions that expect strings.

thank you for you negative response, il explain something about the code:

  if (Serial.available()) {      
    delay(10);                          
    while (Serial.available()) {
      char c = Serial.read();             
      tekst += c;                     
    }

first of all, the nonsense code is ment to receive message from variale length, for instance I can send: "F U" or I can send: "F*ck you and your negativity"

I can also put a 1ms delay int he while loop itself and ditch the 10ms. Do you even know what happens if you dont use a delay here??

The code will jump into the while loop, the code reads a character, the code adds the character to the empty string and than! the code checks the while condition again, but!! the code ran through the while loop so incredibly fast the 2nd byte has not reached the buffer yet, and the code will jump out of the while loop, having read one whole single byte, despite I send many more bytes. afterall the baudrate is only set on 9600 bps.

The most efficient way using delays like this, is to set the baud rate to 115200bps and than you need a delay of only 40microseconds in the while loop itself. like this:

 if (Serial.available()) {          // when i send message
    while (Serial.available()) {
 
      delayMicroseconds(40);

      tekst += char(Serial.read());             // read a byte as char and adds it to the string

    }

It would be even better to use a timer so you can check every millisecond if Serial.available() has incremented and if not, you have a full message in the buffer.

damn PC wasn't done with me prev post, when it got posted :smiley:

anyway thank you for the

You are not copying the NULL terminator into old_buf. Why not?

you could have said: you also need to copy the NULL terminator. but OK.

and if it makes you happy, I will rewrite the receiving part of the code so I can put read characters directly in a char array im already thinking:

  if (Serial.available()) {          // when i send message
    
     while (Serial.available()) {
       delay(1);                     
       charArray[i] = char(Serial.read());
       i++
       }

      i = 0;
}
      

    }

this better??

Do you even know what happens if you dont use a delay here??

I think you will find that Paul knows exactly what will happen, but if you write the code correctly you don't need a delay() at all because you process each character as it arrives no matter what the baud rate is.

first of all, the nonsense code is ment to receive message from variale length, for instance I can send: "F U" or I can send: "F*ck you and your negativity"

Not at all necessary.

In the spirit of even handedness.

LarryD:
Not at all necessary.

And this was?

PaulS:
while you've stuck your thumb up your ass.

Good for the goose and all that?

Wrong + Wrong = Right :sob:

I think you mean Wrong + Wrong != Right

OK people, please keep it civil.
Edit responses if you (I do) think it necessary.
(That was a hint)

Now I remember why I like to use strings again, you can empty them easily. I find character arrays hard to empty. If I send a long word, it prints, but if I send a shorter word after, the last letters of the first word wont get reset.
My string on the other hand get's 'flushed' every time :slight_smile:

I fixed that problem but atm I have that problem that Serial.read() gives me this ÿ charachter as I use a for loop which runs 32 times to get all characters and fill my 32 byte large array -_-. I get the words like I want, they are only followed by lots of ÿ

now my esplora is no longer visable on the com-ports.... :cry:

Now I remember why I like to use strings again, you can empty them easily. I find character arrays hard to empty. I

It's rarely necessary to empty a character array, instead, simply indicate that it's empty by setting the first element to zero, aka '\0'

I think you mean Wrong + Wrong != Right

That's the way it should be but, I think the OP thought 2 Wrongs did make it Right :wink:

.