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)