0

I am currently using Arduino Mega to use a LED matrix and a NRF24L01 transceiver.

for LED matrix, I use MD_MAX72xx library. While for the transceiver I use the RF24 library.

I am already able to interface Arduino using two different LED matrix, and two matrix board is already using the SPI.

But when I try to add the RF24 library, and radio.begin(); the LED matrix isn't working properly anymore.

Here is my whole code:

#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Servo.h>

#include <nRF24L01.h>
#include <RF24.h>

#define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }

#define BUF_SIZE      75  // text buffer size
#define CHAR_SPACING  3   // pixels between characters
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

const int buzzer_pin = 26;
const int servo_pin = 28;

unsigned long buzz_time;
unsigned long sys_time;
String buzz_type = "off";
int buzz_count = 0;
char charr[50];

Servo myservo;

RF24 radio(30, 32); // CE, CSN
const byte address[6] = "GLN71";
//const byte address[5] = {'R','x','A','A','A'};

struct LineDefinition
{
  MD_MAX72XX  mx;
  char    message[BUF_SIZE];
  boolean newMessageAvailable;    // true if new message arrived
};

struct LineDefinition  Line[] =
{
  { MD_MAX72XX(HARDWARE_TYPE, 51, 52, 53, MAX_DEVICES), "MAX", true },
  { MD_MAX72XX(HARDWARE_TYPE, 51, 52,  49, MAX_DEVICES), "MAX", true }
};

#define MAX_LINES   (sizeof(Line)/sizeof(LineDefinition))

void printText(uint8_t lineID, uint8_t modStart, uint8_t modEnd, char *pMsg)
// Print the text string to the LED matrix modules specified.
// Message area is padded with blank columns after printing.
{
  uint8_t   state = 0;
  uint8_t   curLen;
  uint16_t  showLen;
  uint8_t   cBuf[8];
  int16_t   col = ((modEnd + 1) * COL_SIZE) - 1;

  Line[lineID].mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);

  do     // finite state machine to print the characters in the space available
  {
    switch(state)
    {
      case 0: // Load the next character from the font table
        // if we reached end of message, reset the message pointer
        if (*pMsg == '\0')
        {
          showLen = col - (modEnd * COL_SIZE);  // padding characters
          state = 2;
          break;
        }

        // retrieve the next character form the font file
        showLen = Line[lineID].mx.getChar(*pMsg++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
        curLen = 0;
        state++;
        // !! deliberately fall through to next state to start displaying

      case 1: // display the next part of the character
        Line[lineID].mx.setColumn(col--, cBuf[curLen++]);

        // done with font character, now display the space between chars
        if (curLen == showLen)
        {
          showLen = CHAR_SPACING;
          state = 2;
        }
        break;

      case 2: // initialize state for displaying empty columns
        curLen = 0;
        state++;
        // fall through

      case 3: // display inter-character spacing or end of message padding (blank columns)
        Line[lineID].mx.setColumn(col--, 0);
        curLen++;
        if (curLen == showLen)
          state = 0;
        break;

      default:
        col = -1;   // this definitely ends the do loop
    }
  } while (col >= (modStart * COL_SIZE));

  Line[lineID].mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
}

void buzz_start(){
    buzz_count = 5;
    buzz_time = millis();
}

void buzz_call(){
  if(buzz_count > 0){
    if(millis() >= buzz_time){
      if(buzz_type == "off"){
        tone(buzzer_pin, 1000);
        buzz_type = "on";
      }else{
        noTone(buzzer_pin);
        buzz_type = "off";
        buzz_count--;
      }
      buzz_time = millis() + 300;
    }
  }
}

void setup()
{
  Serial.begin(57600);
  pinMode(buzzer_pin, OUTPUT);

  myservo.attach(servo_pin);

  tone(buzzer_pin, 1000);
  Serial.println("BEGIN");





  for (uint8_t i=0; i<MAX_LINES; i++){
    Line[i].mx.begin();
  }

  radio.begin();
  radio.openReadingPipe(20, address);
  radio.setPALevel(RF24_250KBPS);
  radio.startListening();

  String charrr = "STOP";
  charrr.toCharArray(charr, 50);
  printText(1, 0, MAX_DEVICES-1, charr);

  myservo.write(90);
  delay(500);
  myservo.write(180);

  noTone(buzzer_pin);
  sys_time = millis();
}

int counter = 10;
int disp = 0;
void loop()
{
  String charrr = String(counter);
  charrr.toCharArray(charr, 50);
  printText(0, 0, MAX_DEVICES-1, charr);
  if(counter == 0){
    counter = 9;
    if(disp == 0){
     disp = 1; 
     String charrr = "  GO";
     charrr.toCharArray(charr, 50);
     printText(1, 0, MAX_DEVICES-1, charr);
     myservo.write(90);
     buzz_start();
    }else{
     disp = 0;
     String charrr = "STOP";
     charrr.toCharArray(charr, 50);
     printText(1, 0, MAX_DEVICES-1, charr);
     buzz_start();
     myservo.write(180);
    }
  }
  if(millis() >= sys_time){
    counter--;
    sys_time = millis() + 1000;  
  }

  buzz_call();

}

I think the problem here is not connection, but with the initalization of the RF24, and radio.begin();

1

I looked into the source code of the MD_MAX72XX library. The constructor with SPI pins starts software SPI.

Use the MD_MAX72XX::MD_MAX72XX(moduleType_t mod, uint8_t csPin, uint8_t numDevices): constructor to work with SPI hardware/library with all devices.

struct LineDefinition  Line[] =
{
  { MD_MAX72XX(HARDWARE_TYPE, 53, MAX_DEVICES), "MAX", true },
  { MD_MAX72XX(HARDWARE_TYPE, 49, MAX_DEVICES), "MAX", true }
}; 

Or if the MD_MAX72XX worked for you with software SPI, then use some other pins then hardware SPI pins to make hardware SPI free for the NRF24L01.

struct LineDefinition  Line[] =
{
  { MD_MAX72XX(HARDWARE_TYPE, SW_SPI_SI, SW_SPI_CLK, MAX1_CS, MAX_DEVICES), "MAX", true },
  { MD_MAX72XX(HARDWARE_TYPE, SW_SPI_SI, SW_SPI_CLK, MAX2_CS, MAX_DEVICES), "MAX", true }
};
  • are you suggesting that, to initialize the transciever (rf24), instead of radio.begin();.... I should use MD_MAX72XX::MD_MAX72XX(moduleType_t mod, uint8_t csPin, uint8_t numDevices): ? – weyhei Mar 24 at 8:23
  • Thanks! That did the trick. I changed the SI, and CLK. { MD_MAX72XX(HARDWARE_TYPE, 11, 8, 53, MAX_DEVICES), "MAX", true }, { MD_MAX72XX(HARDWARE_TYPE, 11, 8, 49, MAX_DEVICES), "MAX", true } Then the pins 50,51,52 are now used by the NRF24 – weyhei Mar 24 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.