RCSwitch library

I have some working code(below) to control a relay and now I want to add it to some other code but unfortunately the interrupts pins are used by an rotary encoder.

So I was wondering if I could change the RX pin to another spare pin. I seen the interrupt change pin and had a play with it but cant seem to get it working

Here's the working RX code main:

#include "TestLibrary.h"
#define Relay_control 13
TestLib testlib(true);

void setup() {

  testlib.begin(9600);
  pinMode(Relay_control, OUTPUT);
  digitalWrite(Relay_control, LOW);

}

void loop() {

  unsigned long rndNo = testlib.getRandomNumber();

  if (rndNo == 9642805) {//7542805
    digitalWrite(Relay_control, HIGH);
  }
  if (rndNo == 9642804) {//7542804
    digitalWrite(Relay_control, LOW);
  }

}

Cpp file

#include "TestLibrary.h"
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
TestLib::TestLib(bool displayMsg) {
  // Anything you need when instantiating your object goes here
  _msg = displayMsg;
}

// this is our 'begin' function
void TestLib::begin(int baudRate) {
  Serial.begin(baudRate);
  if (_msg) {
    Serial.println("TestLib constructor instantiated (created) successfully.");
  }
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
}

// Pretend this is one or more complex and involved functions you have written
unsigned long TestLib::getRandomNumber() {

  unsigned long specialNumber = mySwitch.getReceivedValue();

  if (mySwitch.available()) {
    specialNumber = mySwitch.getReceivedValue();

    mySwitch.resetAvailable();
  }

  return specialNumber;

}

// Private method for this class
float TestLib::getPi() {
  return 3.1415926;
}

H file

#ifndef tl
#define tl

#if (ARDUINO >=100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class TestLib  {
  public:
    // Constructor
    TestLib(bool displayMsg = false);

    // Methods
    void begin(int baudRate = 9600);
    unsigned long getRandomNumber();

  private:
    bool _msg;
    float getPi();
};
#endif

And this is my attempt of pin change but don't fully understand it.

#include "TestLibrary.h"
#include "PinChangeInt.h" // PinChange Interrupt library
#define Relay_control 13
TestLib testlib(true);
#define PIN3 4


uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20] = {0}; // 20 possible arduino pins
void quicfunc() {
  latest_interrupted_pin = PCintPort::arduinoPin;
  interrupt_count[latest_interrupted_pin]++;
};
// You can assign any number of functions to any number of pins.
// How cool is that?
void pin3func() {
  Serial.print("Pin "); Serial.print(PIN3, DEC); Serial.println("!");
}
void setup() {
  // put your setup code here, to run once:
  pinMode(PIN3, INPUT); digitalWrite(PIN1, LOW);
  PCintPort::attachInterrupt(PIN1, &quicfunc, CHANGE); // add more attachInterrupt code as required
  testlib.begin(9600);
  pinMode(Relay_control, OUTPUT);
  digitalWrite(Relay_control, LOW);
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

void loop() {

  unsigned long rndNo = testlib.getRandomNumber();

  if (rndNo == 9642805) {//7542805
    digitalWrite(Relay_control, HIGH);
  }
  if (rndNo == 9642804) {//7542804
    digitalWrite(Relay_control, LOW);
  }

}

I've changed this line in the CPP file from mySwitch.enableReceive(0) to mySwitch.enableReceive(PIN3)

cant seem to get it working

Can you be more specific, please? What does it actually do, what is it supposed to do? We need a detailed problem report.

The TX code send an on/off signal and the code above works if I attach the receiver pin of the RX to pin 2 of the uno, so it turns the LED on/off.

But i would like to change it to pin 4 and when I press the on/off button on the TX nothing happens when I try to set up and change the interrupt pin with the last lot code at the bottom of my post . I've added an serial print but it shows nothing

#include "TestLibrary.h"
#include "PinChangeInt.h" // PinChange Interrupt library
#define Relay_control 13
TestLib testlib(true);
#define PIN3 4


uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20] = {0}; // 20 possible arduino pins
void quicfunc() {
  latest_interrupted_pin = PCintPort::arduinoPin;
  interrupt_count[latest_interrupted_pin]++;
};
// You can assign any number of functions to any number of pins.
// How cool is that?
void pin3func() {
  Serial.print("Pin "); Serial.print(PIN3, DEC); Serial.println("!");
}
void setup() {
  // put your setup code here, to run once:
  pinMode(PIN3, INPUT); digitalWrite(PIN1, LOW);
  PCintPort::attachInterrupt(PIN1, &quicfunc, CHANGE); // add more attachInterrupt code as required
  testlib.begin(9600);
  pinMode(Relay_control, OUTPUT);
  digitalWrite(Relay_control, LOW);
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

void loop() {

  unsigned long rndNo = testlib.getRandomNumber();

  if (rndNo == 9642805) {//7542805
    digitalWrite(Relay_control, HIGH);
    Serial.println(rndNo);
  }
  if (rndNo == 9642804) {//7542804
    digitalWrite(Relay_control, LOW);
    Serial.println(rndNo);
  }
 //Serial.println(rndNo);
}

and this the cpp file nothing has changed in the H ile

#include "TestLibrary.h"
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
TestLib::TestLib(bool displayMsg) {
  // Anything you need when instantiating your object goes here
  _msg = displayMsg;
}

// this is our 'begin' function
void TestLib::begin(int baudRate) {
  Serial.begin(baudRate);
  if (_msg) {
    Serial.println("TestLib constructor instantiated (created) successfully.");
  }
   mySwitch.enableReceive(PIN3);  // Receiver on inerrupt 0 => that is pin #2
}

// Pretend this is one or more complex and involved functions you have written
unsigned long TestLib::getRandomNumber() {

  unsigned long specialNumber = mySwitch.getReceivedValue();

  if (mySwitch.available()) {
    specialNumber = mySwitch.getReceivedValue();
    
    mySwitch.resetAvailable();
  }

  return specialNumber;

}

// Private method for this class
float TestLib::getPi() {
  return 3.1415926;
}

That library is using external interrupts [from your CPP file]

  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2

On an Uno, you only get interrupt 0 and interrupt 1 which are hardwired to pin2 and pin3

blh64:
That library is using external interrupts [from your CPP file]

  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2

On an Uno, you only get interrupt 0 and interrupt 1 which are hardwired to pin2 and pin3

That's why I cannot get it to work then. I understand that uno only has 2 inerrupt pins

So there is no way that I can get it to work with the uno on another pin ?