Skip to main content
The RadioHead library is packetized.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

You have a few issues here.

if (driver.recv(buf, &buflen)) { ... }

You don't check the value of buflen after calling the recv() method. I didn't readI didn't read the doc of the RH_ASK library you are using. Are you sure it does preserve message boundaries? I let you check. If it only transports bytes (rather than full messages), then you will have to repeatedly recv() until you have a full message.

Edit: Looked at the doc of the RH_ASK library you are using. Are you sureThe communication is indeed packetized, so it does preserveyou don't need to collect the message boundaries? I let you checkchunk by chunk. If it only transports bytes (rather than full messages)It is, then you will have tohowever, repeatedly recv() until you havestill a full message.good idea to check whether the received size is what you expect:

if (driver.recv(buf, &buflen) && buflen == 8) { ... }

sprintf(temp,"%s",(char*) buf);

This is a serious bug, and most probably the reason your program behaves inconsistently. Here buf is not a NUL-terminated string. The sprintf() function will copy bytes from buf into temp until it finds a NUL. It can potentially overwrite the receiving buffer and start clobbering whatever happens to be past temp in your memory. This is called “memory corruption” and can lead to any kind of unpredictable behaviors.

Note that the whole copying you do here serves no useful purpose. You can work from the initial buffer to start with:

char left[5];
memcpy(left, buf, 4);
left[4] = '\0';
char right[5];
memcpy(right, buf+4, 4);
right[4] = '\0';

constrain(leftpower,-255,255);

You are implicitly discarding the value computed by constrain(). You probably mean

leftpower = constrain(leftpower, -255, 255);

You have a few issues here.

if (driver.recv(buf, &buflen)) { ...

You don't check the value of buflen after calling the recv() method. I didn't read the doc of the RH_ASK library you are using. Are you sure it does preserve message boundaries? I let you check. If it only transports bytes (rather than full messages), then you will have to repeatedly recv() until you have a full message.

sprintf(temp,"%s",(char*) buf);

This is a serious bug, and most probably the reason your program behaves inconsistently. Here buf is not a NUL-terminated string. The sprintf() function will copy bytes from buf into temp until it finds a NUL. It can potentially overwrite the receiving buffer and start clobbering whatever happens to be past temp in your memory. This is called “memory corruption” and can lead to any kind of unpredictable behaviors.

Note that the whole copying you do here serves no useful purpose. You can work from the initial buffer to start with:

char left[5];
memcpy(left, buf, 4);
left[4] = '\0';
char right[5];
memcpy(right, buf+4, 4);
right[4] = '\0';

constrain(leftpower,-255,255);

You are implicitly discarding the value computed by constrain(). You probably mean

leftpower = constrain(leftpower, -255, 255);

You have a few issues here.

if (driver.recv(buf, &buflen)) { ... }

You don't check the value of buflen after calling the recv() method. I didn't read the doc of the RH_ASK library you are using. Are you sure it does preserve message boundaries? I let you check. If it only transports bytes (rather than full messages), then you will have to repeatedly recv() until you have a full message.

Edit: Looked at the doc. The communication is indeed packetized, so you don't need to collect the message chunk by chunk. It is, however, still a good idea to check whether the received size is what you expect:

if (driver.recv(buf, &buflen) && buflen == 8) { ... }

sprintf(temp,"%s",(char*) buf);

This is a serious bug, and most probably the reason your program behaves inconsistently. Here buf is not a NUL-terminated string. The sprintf() function will copy bytes from buf into temp until it finds a NUL. It can potentially overwrite the receiving buffer and start clobbering whatever happens to be past temp in your memory. This is called “memory corruption” and can lead to any kind of unpredictable behaviors.

Note that the whole copying you do here serves no useful purpose. You can work from the initial buffer to start with:

char left[5];
memcpy(left, buf, 4);
left[4] = '\0';
char right[5];
memcpy(right, buf+4, 4);
right[4] = '\0';

constrain(leftpower,-255,255);

You are implicitly discarding the value computed by constrain(). You probably mean

leftpower = constrain(leftpower, -255, 255);
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

You have a few issues here.

if (driver.recv(buf, &buflen)) { ...

You don't check the value of buflen after calling the recv() method. I didn't read the doc of the RH_ASK library you are using. Are you sure it does preserve message boundaries? I let you check. If it only transports bytes (rather than full messages), then you will have to repeatedly recv() until you have a full message.

sprintf(temp,"%s",(char*) buf);

This is a serious bug, and most probably the reason your program behaves inconsistently. Here buf is not a NUL-terminated string. The sprintf() function will copy bytes from buf into temp until it finds a NUL. It can potentially overwrite the receiving buffer and start clobbering whatever happens to be past temp in your memory. This is called “memory corruption” and can lead to any kind of unpredictable behaviors.

Note that the whole copying you do here serves no useful purpose. You can work from the initial buffer to start with:

char left[5];
memcpy(left, buf, 4);
left[4] = '\0';
char right[5];
memcpy(right, buf+4, 4);
right[4] = '\0';

constrain(leftpower,-255,255);

You are implicitly discarding the value computed by constrain(). You probably mean

leftpower = constrain(leftpower, -255, 255);