Double data

I friends!

I new in this hole thing of Arduino and I’ve been stuck for several days in a step of a project that involves RF24.
My programming skills are weak but thanks to my friend GOOGLE I have improved a bit!

This is the code:

#include <RTClib.h>
#include <SPI.h>
#include "RF24.h"


RF24 myRadio (7, 8);
byte addresses[][6] = {"0"};

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"DOM", "SEG", "TER", "QUA", "QUI", "SEX", "SAB"};



void setup()
{
  Serial.begin(115200);
  delay(1000);

  myRadio.begin();
  myRadio.setChannel(115);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ;
  myRadio.openWritingPipe( addresses[0]);
  delay(1000);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
}

void loop()
{
  DateTime now = rtc.now();
  char hour [10];
  char date [19];
  
  char data [19];

  sprintf(date, "%02u-%02u-%04u ", now.day(), now.month(), now.year());


  sprintf(hour, "%02u:%02u:%02u ", now.hour(), now.minute(), now.second());

  strcat(date,hour );
  
  memcpy(data, date, sizeof(data));
  
  Serial.println(date); 
  
  Serial.println(data);


  myRadio.write( &data, sizeof(data)); 

  delay(2000);

}

I receive a output like this:

07-02-2019 21:41:38 
07-02-2019 21:41:3807-02-2019 21:41:38

And i want to receive like this:

07-02-2019 21:41:38 
07-02-2019 21:41:38

Why does this occur? What can I do to get what I want?

Thank you!

Why does this occur?

We have no idea why you want to receive data like that. It is YOUR requirement.

If you meant something else by "And i want to receive like this:", certainly a different stream of words is in order.

We can only see your sending code, which wastes memory. We can't see your receiving code.

Thanks PaulS for reply.

Sorry, you're right.

I just want to receive the "data" array. The other array (date) is to see what memcpy function is copying and its not the problem. But i dont understand why the data are repeated. I now i can send "date" via RF24 but I want to do it this way to train my programming skills.

The first thing I'd do is to dispense with the anonymous printing. Always print something in front of, and after, in the case of strings, the data being printed. You can find lots of bugs just doing this.

The second thing I'd is to just use one array, populated all at once.

sprintf(data, "%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

This avoids the problems that might be introduced copy data incorrectly.

The third thing I'd do, or second, if you don't like the second one, is to CAREFULLY count how much you are writing into each array. I've done that, and I see what (part of) the problem is.

The fourth thing I'd do is to post the receiving code, on the off chance that the problem isn't on the sending end.

PaulS:
The first thing I’d do is to dispense with the anonymous printing. Always print something in front of, and after, in the case of strings, the data being printed. You can find lots of bugs just doing this.

You are saying that I should remove all “Serial.println”? I just use to test the code.My goal is to print on an LCD

PaulS:
The second thing I’d is to just use one array, populated all at once.

sprintf(data, "%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

This avoids the problems that might be introduced copy data incorrectly.

Thank you very much. I think sending it will be much better.

PaulS:
The third thing I’d do, or second, if you don’t like the second one, is to CAREFULLY count how much you are writing into each array. I’ve done that, and I see what (part of) the problem is.

I’m sorry but I do not understand. I think I’m writing the right space

PaulS:
The fourth thing I’d do is to post the receiving code, on the off chance that the problem isn’t on the sending end.

This is the receive code (when I send the variable date rather than data)

#include <SPI.h>
#include "RF24.h"

RF24 myRadio (7, 8);

byte addresses[][6] = {"0"};


void setup()
{
  Serial.begin(115200);
  delay(1000);

  myRadio.begin();
  myRadio.setChannel(115);
  myRadio.setPALevel(RF24_PA_MAX);
  myRadio.setDataRate( RF24_250KBPS ) ;
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}


void loop()
{
  char date [10];
  char hour [8];

  char data [19];

  int targetIndex = 11;

  if ( myRadio.available())
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }

    memcpy(date, data, sizeof(date));

    for (int x = 0; x < 8; x++)
    {
      hour[x] = data[targetIndex];
      targetIndex++;
    }

    Serial.println(data);
    Serial.println(date);
    Serial.println(hour);

  }
}

You are saying that I should remove all “Serial.println”?

No. I’m saying you should add MORE print() statements.

Serial.print("date: [");
Serial.print(date);
Serial.println("]");

takes only a few seconds longer to type, but conveys a LOT more information.

I’m sorry but I do not understand. I think I’m writing the right space

You may be right. I don’t like sizing arrays just barely big enough. Nor do I like using odd size arrays.

You are sending the data incorrectly, though.

  myRadio.write( &data, sizeof(data));

The second argument is the number of bytes to send. The number of bytes to send is strlen(data), NOT sizeof(data). If data is full, the values will be the same. If not, the values will be different, and you will be sending garbage, with no way to know that you did that.

    Serial.println(data);
    Serial.println(date);
    Serial.println(hour);

If you stopped with the anonymous printing, you’d understand why these three lines are producing two lines of output.

date and hour are NOT strings, because you did not NULL terminate the arrays after populating the arrays. So, you should not be passing them to a function that expects a string. When you do, you have no reason to complain when the function does something you don’t expect (as happened).

PaulS: You are sending the data incorrectly, though.

  myRadio.write( &data, sizeof(data));

The second argument is the number of bytes to send. The number of bytes to send is strlen(data), NOT sizeof(data). If data is full, the values will be the same. If not, the values will be different, and you will be sending garbage, with no way to know that you did that.

Weird. All the examples I found sent the information in this way.

PaulS: date and hour are NOT strings, because you did not NULL terminate the arrays after populating the arrays. So, you should not be passing them to a function that expects a string. When you do, you have no reason to complain when the function does something you don't expect (as happened).

I did not quite understand what he meant. So you warn me to add zero when I am concatenating a string?

So you warn me to add zero when I am concatenating a string?

You need to append a NULL to the string. The best, IMHO, way is to append the NULL each time you put a character in the array.

    for (int x = 0; x < 8; x++)
    {
      hour[x] = data[targetIndex];
      hour[x+1] = '\0';       // Append a NULL
      targetIndex++;
    }

Keep in mind that your array only has room for 8 characters. It needs room for 9.

Do the same for the date array (make it bigger, append the NULL).

Thanks for your help and explanations PaulS!

I'll make some modifications in my code based on your explanations.

Thank you very much