NRF24L01 interrupts

Right now I am using Arduino UNO based boards and using a NRF24L01 module for controlling the robot, however after some time it stops working, I don't understand the issue. I did not write the code myself. It came with the kit. I will share two codes, one is the code of the car and the other one is joystick code. I am using this forum first time, so sorry if I misuse something wrong. Please help me out.

Car Code

#include <Arduino.h>
#include "RF24.h"
#include <FlexiTimer2.h>
#include <EEPROM.h>
#include <Wire.h>

#define NRF_UPDATE_TIMEOUT    1000 //*1000
#define PIN_SERVO			2

#define MOTOR_DIRECTION     0   //If the direction is reversed, change 0 to 1
#define PIN_DIRECTION_LEFT	4
#define PIN_DIRECTION_RIGHT	3
#define PIN_MOTOR_PWM_LEFT	6
#define PIN_MOTOR_PWM_RIGHT	5

#define PIN_SONIC_TRIG		7
#define	PIN_SONIC_ECHO		8


#define PIN_SPI_CE			9
#define PIN_SPI_CSN			10
#define PIN_SPI_MOSI		11
#define PIN_SPI_MISO		12
#define PIN_SPI_SCK			13

#define PIN_BATTERY			A0
#define PIN_BUZZER			A0


#define MOTOR_PWM_DEAD	    5

#define BAT_VOL_STANDARD	7.0

enum RemoteData
{
	JOYSTICK_X = 2,
	JOYSTICK_Y = 3,
	JOYSTICK_Z = 4,
	S1 = 5,
	S2 = 6,
	S3 = 7
};
enum RemoteMode {
	ON_ON_ON = 0,
	ON_ON_OFF = 1,		//Servo calibration mode.
	ON_OFF_ON = 2,
	ON_OFF_OFF = 3,		//Ultrasonic obstacle avoidance mode.
	OFF_ON_ON = 4,
	OFF_ON_OFF = 5,		//Line tracking mode.
	OFF_OFF_ON = 6,		//
	OFF_OFF_OFF = 7			//Remote control mode.
};
enum RemoteModeSwitchState {
	MODE_SWITCHING_IS_INITIALIZING = 0,
	MODE_SWITCHING_IS_PROCESSING = 1,
	MODE_SWITCHING_IS_CONFIRMING = 2,
	MODE_SWITCHING_WAS_FINISHED = 3
};


float batteryVoltage = 0;
bool isBuzzered = false;
u32 lastNrfUpdateTime = 0;


RF24 radio(PIN_SPI_CE, PIN_SPI_CSN);
const byte addresses[6] = "Free1";
int nrfDataRead[8];
bool nrfComplete = false;


void setup() {
  pinsSetup();
  nrf24L01Setup();
  if (!nrf24L01Setup()) {
    alarm(4, 2);
  }
}

void pinsSetup() {
	pinMode(PIN_DIRECTION_LEFT, OUTPUT);
	pinMode(PIN_MOTOR_PWM_LEFT, OUTPUT);
	pinMode(PIN_DIRECTION_RIGHT, OUTPUT);
	pinMode(PIN_MOTOR_PWM_RIGHT, OUTPUT);

	pinMode(PIN_SONIC_TRIG, OUTPUT);// set trigPin to output mode
	pinMode(PIN_SONIC_ECHO, INPUT); // set echoPin to input mode
	setBuzzer(false);
}

bool nrf24L01Setup() {
  // NRF24L01
  if (radio.begin()) {                      // initialize RF24
    radio.setPALevel(RF24_PA_MAX);      // set power amplifier (PA) level
    radio.setDataRate(RF24_1MBPS);      // set data rate through the air
    radio.setRetries(0, 15);            // set the number and delay of retries
    radio.openReadingPipe(1, addresses);// open a pipe for reading
    radio.startListening();             // start monitoringtart listening on the pipes opened

    FlexiTimer2::set(20, 1.0 / 1000, checkNrfReceived); // call every 20 1ms "ticks"
    FlexiTimer2::start();

    return true;
  }
  return false;
}

void loop() {
  if (getNrf24L01Data()) {
    clearNrfFlag();
    updateCarActionByNrfRemote();
    
    lastNrfUpdateTime = millis();
  }
  if (millis() - lastNrfUpdateTime > NRF_UPDATE_TIMEOUT) {
    lastNrfUpdateTime = millis();
    resetNrfDataBuf();
    updateCarActionByNrfRemote();
  }
}

void checkNrfReceived() {
  delayMicroseconds(1000);
  if (radio.available()) {             // if receive the data
    while (radio.available()) {         // read all the data
      radio.read(nrfDataRead, sizeof(nrfDataRead));   // read data
    }
    nrfComplete = true;
    return;
  }
  nrfComplete = false;
}

void motorRun(int speedl, int speedr) {//
	int dirL = 0, dirR = 0;
  if (speedl > 0) {
    dirL = 1 ^ MOTOR_DIRECTION;
  } else {
    dirL = 0 ^ MOTOR_DIRECTION;
    speedl = -speedl;
  }

  if (speedr > 0) {
    dirR = 0 ^ MOTOR_DIRECTION;
  } else {
    dirR = 1 ^ MOTOR_DIRECTION;
    speedr = -speedr;
  }

	speedl = constrain(speedl, 0, 255);
	speedr = constrain(speedr, 0, 255);

	if (abs(speedl) < MOTOR_PWM_DEAD && abs(speedr) < MOTOR_PWM_DEAD) {
		speedl = 0;
		speedr = 0;
	}
	digitalWrite(PIN_DIRECTION_LEFT, dirL);
	digitalWrite(PIN_DIRECTION_RIGHT, dirR);
	analogWrite(PIN_MOTOR_PWM_LEFT, speedl);
	analogWrite(PIN_MOTOR_PWM_RIGHT, speedr);
}


void updateCarActionByNrfRemote() { // 
  int x = nrfDataRead[2] - 512;
  int y = nrfDataRead[3] - 512;
  int pwmL, pwmR;
  if (y < -10) {  // back
    pwmL = (y - x) / 2;
    pwmR = (y - x) / 2;
  } 
  else if (x < -20) { // right
    pwmL = (-x + y) / 2;
    pwmR = (x - y) / 2;
  }
  else if (x > 20) { // left
    pwmL = (-x - y) / 2;
    pwmR = (x + y) / 2;
  }
  else if (y > 10){ // forward
    pwmL = (y + x) / 2;
    pwmR = (y + x) / 2;
  }
  else { // stop
    pwmL = 0;
    pwmR = 0;
  }
  motorRun(pwmL, pwmR);

  Serial.println();
  if (nrfDataRead[4] == 0) {
    setBuzzer(true);
  } else {
    setBuzzer(false);
  }

}

bool getBatteryVoltage() { // 
	if (!isBuzzered) {
		pinMode(PIN_BATTERY, INPUT);
		int batteryADC = analogRead(PIN_BATTERY);
		if (batteryADC < 614)		// 3V/12V ,Voltage read: <2.1V/8.4V
		{
			batteryVoltage = batteryADC / 1023.0 * 5.0 * 4;
			return true;
		}
	}
	return false;
}
void setBuzzer(bool flag) {
	isBuzzered = flag;
	pinMode(PIN_BUZZER, flag);
	digitalWrite(PIN_BUZZER, flag);
}
void alarm(u8 beat, u8 repeat) {
	beat = constrain(beat, 1, 9);
	repeat = constrain(repeat, 1, 255);
	for (int j = 0; j < repeat; j++) {
		for (int i = 0; i < beat; i++) {
			setBuzzer(true);
			delay(100);
			setBuzzer(false);
			delay(100);
		}
		delay(500);
	}
}
void resetCarAction() {
	motorRun(0, 0);
	setBuzzer(false);
}

bool getNrf24L01Data(){
  return nrfComplete;
}

void clearNrfFlag() {
  nrfComplete = 0;
}

void resetNrfDataBuf() {
  nrfDataRead[0] = 0;
  nrfDataRead[1] = 0;
  nrfDataRead[2] = 512;
  nrfDataRead[3] = 512;
  nrfDataRead[4] = 1;
  nrfDataRead[5] = 1;
  nrfDataRead[6] = 1;
  nrfDataRead[7] = 1;
}

u8 updateNrfCarMode() {
  // nrfDataRead [5 6 7] --> 111
  return ((nrfDataRead[5] == 1 ? 1 : 0) << 2) | ((nrfDataRead[6] == 1 ? 1 : 0) << 1) | ((nrfDataRead[7] == 1 ? 1 : 0) << 0);
}

Joystick code

// NRF24L01
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);                // define the object to control NRF24L01
const byte addresses[6] = "Free1";// define communication address which should correspond to remote control
// wireless communication
int dataWrite[8];                 // define array used to save the write data
// pin
const int 
          // pot1Pin = A0,           // define POT1 Potentiometer
          // pot2Pin = A1,           // define POT2 Potentiometer
          joystickXPin = A2,      // define pin for direction X of joystick
          joystickYPin = A3,      // define pin for direction Y of joystick
          joystickZPin = 7,       // define pin for direction Z of joystick
          s1Pin = 4,              // define pin for S1
          s2Pin = 3,              // define pin for S2
          s3Pin = 2,              // define pin for S3
          // led1Pin = 6,            // define pin for LED1 which is close to POT1 and used to indicate the state of POT1
          // led2Pin = 5,            // define pin for LED2 which is close to POT2 and used to indicate the state of POT2
          led3Pin = 8;            // define pin for LED3 which is close to NRF24L01 and used to indicate the state of NRF24L01

void setup() {
  // NRF24L01
  radio.begin();                      // initialize RF24
  radio.setPALevel(RF24_PA_MAX);      // set power amplifier (PA) level
  radio.setDataRate(RF24_1MBPS);      // set data rate through the air
  radio.setRetries(0, 15);            // set the number and delay of retries
  radio.openWritingPipe(addresses);   // open a pipe for writing;// open a pipe for reading
  radio.stopListening();              // stop listening for incoming messages
  
// pin
  pinMode(joystickZPin, INPUT);       // set led1Pin to input mode
  pinMode(s1Pin, INPUT);              // set s1Pin to input mode
  pinMode(s2Pin, INPUT);              // set s2Pin to input mode
  pinMode(s3Pin, INPUT);              // set s3Pin to input mode
  // pinMode(led1Pin, OUTPUT);           // set led1Pin to output mode
  // pinMode(led2Pin, OUTPUT);           // set led2Pin to output mode
  pinMode(led3Pin, OUTPUT);           // set led3Pin to output mode
}

void loop()
{
  // put the values of rocker, switch and potentiometer into the array
  // dataWrite[0] = analogRead(pot1Pin); // save data of Potentiometer 1
  // dataWrite[1] = analogRead(pot2Pin); // save data of Potentiometer 2
  dataWrite[2] = analogRead(joystickXPin);  // save data of direction X of joystick
  dataWrite[3] = analogRead(joystickYPin);  // save data of direction Y of joystick
  dataWrite[4] = digitalRead(joystickZPin); // save data of direction Z of joystick
  dataWrite[5] = digitalRead(s1Pin);        // save data of switch 1
  dataWrite[6] = digitalRead(s2Pin);        // save data of switch 2
  dataWrite[7] = digitalRead(s3Pin);        // save data of switch 3

  // write radio data
  if (radio.writeFast(&dataWrite, sizeof(dataWrite)))
  {
    digitalWrite(led3Pin, HIGH);
  }
  else {
    digitalWrite(led3Pin, LOW);
  }
  delay(20);
}

Now share the annotated schematic if the radio is not being powered by the Arduino. If it is power it with the Arduino use another source.

You want to see this?

thanks for your reply

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