I have read everything I can find, and tried to correct this myself for longer then I'd like to admit so any help is greatly appreciated. I am using two NRF24L01 modules with the objective of controlling a small robot car with two motors. The NRF modules communicate fine. I have confirmed this many times via the monitor. The issue here, is that upon receiving the instruction to call upon the 'move' function to activate the motors, the motors do activate, however following that, communication is lost with the NRF module and the program essentially freezes in that state (the motors keep running and it seems to keep looping). I have tried both switch case and if statements, but it seems to happen either way. I have also used this same 'move' function in the past for a bluetooth controlled robot, and it works fine. Therefore I assume this must have something to do with the NRF module, however I am at a loss. I have duplicated this build also on a bread board and the same communication loss/freeze occurs (not a soldering issue). The motor controller is a sparkfun tb6612fng, connected to an aftermarket arduino nano, 9v power supply. The Transmitter sends a single character, in this case the number '1' to call the 'move' function. In the below code, once the '1' is received, the motors begin to run, and then I continue to get the "No Data" line from the getData function, repeatedly with no ability to stop the program.
Please let me know if I have missed anything important. Thanks in advance for any assistance.
Receiver Code:
#include "nRF24L01.h"
#include "printf.h"
#include "RF24.h"
#include <SPI.h>
#define CE_PIN 9
#define CSN_PIN 10
// NRF Details
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
bool newData = false;
// Integers for motor commands
int STBY = 8; //standby
char command;
//Motor A
int PWMA = 5; //Speed control
int AIN1 = 6; //Direction
int AIN2 = 7; //Direction
//Motor B
int PWMB = 3; //Speed control
int BIN1 = 2; //Direction
int BIN2 = 4; //Direction
void setup() {
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
}
void loop() {
//stop();
getData();
if (newData == true ) {
newData = false;
switch (command) {
case '1':
move(1, 255, 0); //motor 1, full speed, left
move(2, 255, 1); //motor 2, full speed, right
Serial.println("Forward Movement");
break;
}
}
}
void getData() {
if ( radio.available() ) {
char dataReceived;
radio.read( &dataReceived, sizeof(dataReceived) );
command = dataReceived;
newData = true;
Serial.print("Data received ");
Serial.println(dataReceived);
}
else {
Serial.print("No Data");
}
}
void move(int motor, int speed, int direction) {
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if (direction == 1) {
inPin1 = HIGH;
inPin2 = LOW;
}
if (motor == 1) {
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
} else {
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void stop() {
digitalWrite(STBY, LOW);
}