Rf24 and multiplexer issue - solved

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));

}






Pin 10 is the default chip select (SS) pin for the SPI library.

From the SPI library reference:

Note about Slave Select (SS) pin on AVR based boards

All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.

RTFM

Thanks for that!
So pin 10 shouldn’t be used at all? Or use is fine, so long as it’s OUTPUT only? I have another nano planned that uses the pwm on 10 for a motor driver.

As long as pin 10 is set to be an OUTPUT you can use it. As far as using pin 10 for PWM, I can't say for sure. Is a pin being used as PWM still equivalent to pinMode OUTPUT in this case? I don't know. Easy enough to test, though.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.