I have 3 nano's successfully connected by rf24, I am adding a fourth that uses a multiplexer as an input with 16 buttons and am running into an issue.
The fourth nano, works with a single input button (the same as the other 2 nodes). so far so good
When adding the multiplexer code, it stops the rf24 from transmitting (including the single input button), and the multiplexer doesnt work. Greying out the multiplexer common pin
pinMode(g_common_pin, INPUT_PULLUP);
allows the rf24 to transmit again
greying out the rf24 code
to_node = 01;
RF24NetworkHeader header(to_node);
bool ok = network.write(header, &payLoad, sizeof(payLoad));
to_node = 00;
RF24NetworkHeader header1(to_node);
bool ok1 = network.write(header1, &payLoad, sizeof(payLoad));
allows the multiplexer to work correctly.
The two seem to be conflicting, but I cannot work out why. The other nodes are not effected. Does someone have any suggestions?
Code for the 4th nano
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#define IR 2
///////////////multiplexer////////////////////
#include <CD74HC4067.h>
// s0 s1 s2 s3
CD74HC4067 my_mux(4, 5, 6, 9); // create a new CD74HC4067 object with its four control pins
int val = 0; // variable to store the read value
int i = 16; // Multiplexer channel (0-16)
const int g_common_pin = 10; // select a pin to share with the 16 channels of the CD74HC4067
///////////////////////////
//////////////////////////////////////////////
RF24 radio(7, 8);
RF24Network network(radio);
uint16_t to_node = 00; // Master
const uint16_t this_node = 02; // This node
int incomingData, payLoad;
////////////////////////////////////////////////
bool sensor_A, latch_A, previous_state_A = false; // sensor with latch
unsigned long previous_millis;
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
unsigned long buttonPrevMillis = 0; ////these two lines
unsigned long buttonCurrentMillis = 0;
void setup() {
/////////////////////////////////////////////////////
Serial.begin(9600);
SPI.begin();
radio.begin();
radio.setPALevel(RF24_PA_LOW);
network.begin(79, this_node);
radio.setDataRate(RF24_250KBPS);
Serial.print("Ready--"), Serial.print(this_node), Serial.println("--");
//////////////////////////////////////////////////////
// Initializing GPIO
pinMode(IR, INPUT); //sensor
pinMode(g_common_pin, INPUT_PULLUP); //use INPUT_PULLUP if not on a comm unit// set the initial mode of the common pin. common pin for 1st multiplexer
}
void loop() {
///////////////// debounce multiplexer /////////////////////////////
int buttonState[i];
int debounceTime = 500;
for (int i = 0; i < 16; i++)
{
my_mux.channel(i);
val = digitalRead(g_common_pin);
buttonState[i] = val;
if (buttonState[i] == 1) {
buttonCurrentMillis = millis();
}
if ((buttonCurrentMillis - buttonPrevMillis) > debounceTime)//has been in this state for some time
{
if (val != 0) {
Serial.print("###"); Serial.println(i);
payLoad = i;
//switch cases go here//
}
}
buttonPrevMillis = buttonCurrentMillis;
}
//////////////////// ------ Receiving Data------- ///////////////////////////////
network.update();
while (network.available()) {
RF24NetworkHeader header;
network.read(header, &incomingData, sizeof(incomingData));
Serial.print(header.from_node);
Serial.print(" | ");
Serial.println(incomingData);
if (incomingData == 30) { // GREEN
Serial.println("Green");
}
if (incomingData == 31) { // RED
Serial.println("Red");
}
}
////////////////////// ------ IR sensor------- /////////////////////////////
if (digitalRead(IR) == LOW) {
sensor_A = true;
latch_A = true;
previous_millis = millis();
}
else sensor_A = false;
if (latch_A && !sensor_A) { // Delay reset latch after 100 ms
if (millis() - previous_millis >= 300) latch_A = false;
}
////////////////////// ------ IR EVENT ------- //////////////////////////////////
if (previous_state_A != latch_A) {
previous_state_A = latch_A;
if (latch_A) payLoad = 30;
else payLoad = 31;
///////////////////////// ------ Sending data ------- ////////////////////
sendDataViaRf24();
}
}
///////////////////////Functions/////////////////////////////////////////////
void sendDataViaRf24() {
to_node = 01;
RF24NetworkHeader header(to_node);
bool ok = network.write(header, &payLoad, sizeof(payLoad));
to_node = 00;
RF24NetworkHeader header1(to_node);
bool ok1 = network.write(header1, &payLoad, sizeof(payLoad));
}