Data receiving from an RF433 transmitter fails with ServoTimer2

I'm a beginner to Arduino, and I've recently been trying to send signals from a transmitter circuit attached to a potentiometer to control a servo attached to a receiver circuit. The hardware I'm using is an RXB6 transmitter and receiver pair for the data transmission, and I tried using the Radiohead library along with the ServoTimer2 libraries (not the normal Servo library since Radiohead seems to conflict with that).

However, when I try using servos in my Radiohead project, the receiver seems to stop receiving any and all messages, so I can't use the potentiometer to remotely control the servo.

I've managed to narrow down the problem, and saw that the receiver stops functioning when I attach a pin to my servo - it still works when I just include the ServoTimer2 library or create a ServoTimer2 object.

Here's the minimal code I'm using for the transmitter:

#include <RH_ASK.h>
#include <SPI.h>
#define COOLDOWN 10

RH_ASK driver;

void setup()
{
    Serial.begin(9600);
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    const char* msg = "foo";
    
    driver.send((uint8_t *)msg, 3);
    driver.waitPacketSent();
    Serial.println("sent");

    delay(COOLDOWN);
}

and the code for the receiver:

#include <RH_ASK.h>
#include <SPI.h>
#include <ServoTimer2.h>

#define SERVO_PIN 3

RH_ASK driver;
ServoTimer2 servo;

void setup() {
  Serial.begin(9600);
  if (!driver.init())
    Serial.println("init failed");

  servo.attach(SERVO_PIN);
}

void loop() {
  uint8_t buf[3];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)) {
    Serial.println((String)(char*)buf);
  }
}

This code works fine when I comment out the servo.attach(SERVO_PIN) part, save for an unknown character I seem to get at the end of every output (instead of "foo", I get "foo"). However, when I include that line of code, I start getting no output at all.

Why does this happen, and what should I do to fix this?

We need to know which Arduino(s) you are using. Note that on AVR-based Arduinos, RadioHead can be forced to use Timer2.

There are two problems with this line:

Serial.println((String)(char*)buf);
  1. "buf" is not properly terminated with a zero byte to be treated as a character string. (C-string) Declare it to be of length four or longer and put a zero in buf[3]

  2. Don't use Strings on Arduino, they cause memory fragmentation and crashes. Once you have received ASCII data in "buf" and made sure it is properly terminated, print it with

Serial.println((char *)buf);

Ah, my bad, I had temporarily tried typecasting the result to a String to see if the character would go away. Why do I need to terminate buf with a zero byte, though? Does it have anything to do with null characters? (sorry for the question, I'm still really new to C-ish languages)

And as for my Arduinos, I'm using 2 Arduino Uno R3s. If the problem were to be a problem with the Timers, though, wouldn't the problem have shown itself at compile time? In my case, the code successfully compiles and gets uploaded to the Arduino, but the receiver still doesn't receive any more messages as soon as I include the servo.attach line.

Why do I need to terminate buf with a zero byte, though?

A moment spent with Mr. Google and a search for C-string will explain.

If the problem were to be a problem with the Timers, though, wouldn't the problem have shown itself at compile time?

No. The compiler does not try to second guess legal code.

jremington:
A moment spent with Mr. Google and a search for C-string will explain.

A search brought up something about C strings being null-terminated/having a NUL character at the end. Does a zero at the end suffice as a NUL character for buf, then?

I tried modifying the code in loop() to this, and it worked in removing the box character from my output:

void loop() {
  uint8_t buf[4];
  uint8_t buflen = sizeof(buf);

  if (driver.recv(buf, &buflen)) {
    buf[3] = 0;
    Serial.println((String)(char*)buf);
  }
}

Also, as for the main problem, I tried using the Servo.h library again, since as you said, the problem might lie in the Timers yet again, and it worked! I guess it wasn't working for me back then because I had a couple of other libraries in place + there are lots of forums here talking about how Radiohead conflicts with Servo.h, so I assumed that was the bug in my program as well. But it works with Servo.h without the other libraries.

Thanks for the help!

You are welcome!