RadioHead transmission problems

Hi Guys,

Have been testing RF links and started by using the VirtualWire library. Found there to be a lot of ‘dropped packets’ if you will, ie receiver didn’t receive the transmitted data 100% of the time and read it was suggested to move to RadioHead.

Issue now is I do not get any comms at all

Receiver:

#include <ServoTimer2.h>
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif

// FOR NANO

/* Connections:
 * Pin 3 - ESC output
 * Pin 5 - RX input
 * Pin 7 - SW output
 */

int zeroPos;
int potValue = 0;

char potArray[3];
  
ServoTimer2 ESC;

RH_ASK driver(2000, 10, 5);
  
void setup() {
  delay(1000);
  //Serial.begin(9600);
  
  #ifdef RH_HAVE_SERIAL
    Serial.begin(9600);   // Debugging only
  #endif
      if (!driver.init())
  #ifdef RH_HAVE_SERIAL
           Serial.println("init failed");
  #else
          ;
  #endif
  
	ESC.attach(3); // ESC OUTPUT - PIN 3
  pinMode(7, OUTPUT); // SW OUTPUT - PIN 7
  digitalWrite(7, HIGH); // Set relay to closed
}

void loop() {
	uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  
  if (driver.recv(buf, &buflen)) {
		for(int i = 0; i < buflen; i++) {
      potArray[i] = char(buf[i]);
    }
    potArray[buflen] = '\0';
    potValue = atoi(potArray);
	} else {
    potValue = 0;
	}
  Serial.println(potValue);
  ESC.write(potValue);
  delay(50);
}

Transmitter:

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

// FOR PRO MICRO

/* Connections:
 * Pin 3 - TX output
 * Pin 9 - LED output
 * Pin 18/A0 - Pot input
 * Pin 19/A1 - Precise input
 * Pin 20/A2 - Cruise input
 */

int zeroPos;
int cruise;
int precise;
int potValue;

int rangeMode = 0; // Simple (0) or Precise (1) control mode
int firstRun = 1;

byte result[2];

RH_ASK driver(2000, 3, 10);
  
void setup() {
  //analogReference(INTERNAL);
  //Serial.begin(9600);
  #ifdef RH_HAVE_SERIAL
    Serial.begin(9600);   // Debugging only
  #endif
      if (!driver.init())
  #ifdef RH_HAVE_SERIAL
           Serial.println("init failed");
  #else
          ;
  #endif
	
  pinMode(A1, INPUT_PULLUP); // PRECISE MODE - PIN 19
  pinMode(A2, INPUT_PULLUP); // CRUISE MODE - PIN 20
  pinMode(5, INPUT_PULLUP); // PTT ENABLE
  pinMode(9, OUTPUT); // LED - PIN 9
    
  delay(1000);
  
	zeroPos = analogRead(A0); // THROTTLE - PIN 18
  Serial.print("Zero Pos: ");
  Serial.println(zeroPos);
  Serial.println("--------------");
  
  precise = analogRead(A1);
  if (precise <= 100) { // HOLD BUTTON ON START TO ACTIVATE
    rangeMode = 1;
    Serial.println("PRECISE MODE");
  } else {
    Serial.println("SIMPLE MODE");
  }  
}

void loop() {
	cruise = analogRead(A2);
  potValue = analogRead(A0);
  
	char stringValue[4];
 
  if (rangeMode == 1 && potValue >= 64 && firstRun == 1) { // Precise mode, if throttle low
    Serial.println("Reduce throttle input");
  } else {
    firstRun = 0;
  }
  
  if (cruise <= 100 && potValue >= zeroPos-20 && potValue <= zeroPos+10) {
    Serial.println("CRUISE");
    potValue = 60;
  } else {
    if (rangeMode == 0 && potValue < zeroPos) {
      potValue = 0;
    } else if (rangeMode == 0 && potValue > zeroPos) {         
      potValue = map(potValue, zeroPos, 1023, 0, 180);
    } else if (rangeMode == 1) {
      potValue = map(potValue, 0, 1023, 0, 180);
    } else {
      potValue = 0;
    }
  }
  itoa(potValue, stringValue, 10);
  Serial.println(stringValue);
  driver.send((uint8_t *)stringValue, strlen(stringValue));
  driver.waitPacketSent();
}

Have not changed any hardware, variables or functionality, just implemented the RH ASK library. I will note that examples do not work for me, as in I change the necessary settings in the examples, but yet nothing is output to the receiver’s serial terminal… but as VW worked I believe this must be a sw bug.

Any help is appreciated.

Thanks!

Looked to be a pin mapping issue - calling the wrong pins in the driver constructor. Had some other issues but managed to fix them too!

I always set the three pins to something, because otherwise they will be set to the default pins which might be in use by other things. When I don't use one of the three, then I assign it to a unused pin and write that in comments in the sketch.

You forgot to set the pttPin, so that became pin 10 by default. You already used pin 10.
In my opinion that is not well explained. In the examples I see that the pttPin is sometimes set to pin 0. I think that is wrong :confused: because pin 0 is a real pin.

The RadioHead uses a lot of memory, the VirtualWire should still work.

The VirtualWire and RadioHead libraries are often used with binary data instead of readable ASCII text. You could transmit the two bytes of a integer.