I have a set-up for remotely controlling 2 high torque DC motors. I am using two Arduino Megas, and wireless communication is achieved using NRF24L101 module.
The problem is: I can communicate between the two arduinos, only when the receiver unit is connected to computer via USB, AND serial monitor is running.
Otherwise, it works sporadically.
I have attached my circuit diagram, and codes.
Did I ground everything properly?
Is my code at fault?
Receiver Unit

Transmitter Unit

Receiver Unit Code
#include <nRF24L01.h>
#include <RF24.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <QMC5883LCompass.h>
#include <SdFat.h>
#include <SPI.h>
#define CE_PIN 7
#define CSN_PIN 8
#define SS 53
//new SPI pins
const uint8_t SD_CS_PIN = 31;
const uint8_t SOFT_MOSI_PIN = 33;
const uint8_t SOFT_MISO_PIN = 35;
const uint8_t SOFT_SCK_PIN = 37;
SoftSpiDriver<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> softSpi;
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)
SdFat SD;
static const int RXPin = 19, TXPin = 18;
static const uint32_t GPSBaud = 9600;
byte Index = 0;
QMC5883LCompass compass; //create Compass object
// For stats that happen every 5 seconds
unsigned long last = 0UL;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
float msg[2];
byte leds = 0;
//This setup is to control the speed of a DC motor by a potentiometer using l298n driver
//We read the value from the analog input, calibrate it then inject to the module
//Refer to "L298nPot2DirAutoMan" sketch for more information
int ConA = 3;// Don't forget this is a PWM DI/DO
int in1 = 25; //Declaring where our module is wired
int in2 = 24;
int ConB = 4;// Don't forget this is a PWM DI/DO
int in3 = 22; //Declaring where our module is wired
int in4 = 23;
int motorSpeedA = 0;
int motorSpeedB = 0;
int LEFT = 0;
int RIGHT = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
compass.init();
compass.setCalibration(-1138, 870, -700, 1312, -1, 1026);
radio.begin();
radio.openReadingPipe(0, thisSlaveAddress);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CONFIG)) {
SD.initErrorHalt();
}
else {
Serial.println("initialization done.");
}
pinMode(SD_CS_PIN, OUTPUT);
pinMode(CSN_PIN, OUTPUT);
pinMode(SS, OUTPUT);
digitalWrite(SS,HIGH);
// Attach the L298n to MEGA
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(4, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(3, OUTPUT);
}
void loop()
{
if(radio.available()) //checks whether any data have arrived at the address of the modem
radio.read(&msg, sizeof(msg));
int xAxis = (msg[0]);
int yAxis = (msg[1]);
if (xAxis < 470)
{
digitalWrite(in1, HIGH); //Switch between this HIGH and LOW to change direction
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); //Switch between this HIGH and LOW to change direction
digitalWrite(in4, LOW);
motorSpeedA = map(xAxis, 470, 0, 0, 255);
motorSpeedB = map(xAxis, 470, 0, 0, 255);
}
else if (xAxis > 550)
{
digitalWrite(in1, LOW); //Switch between this HIGH and LOW to change direction
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW); //Switch between this HIGH and LOW to change direction
digitalWrite(in4, HIGH);
motorSpeedA = map(xAxis, 550, 1023, 0, 255);
motorSpeedB = map(xAxis, 550, 1023, 0, 255);
}
if (yAxis < 470)
{
//Turn Left, decrease left motor speed, increase right motor speed
LEFT = map(yAxis, 470, 0, 0, 255);
motorSpeedA = motorSpeedA - LEFT;
motorSpeedB = motorSpeedB + LEFT;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
else if (yAxis > 550)
{
//Turn Right, increase left motor speed, decrease right motor speed
RIGHT = map(yAxis, 550, 1023, 0, 255);
motorSpeedA = motorSpeedA + RIGHT;
motorSpeedB = motorSpeedB - RIGHT;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
analogWrite(ConA,motorSpeedA);// Then inject it to our motorA
analogWrite(ConB,motorSpeedB);// Then inject it to our motorB
Serial.print(motorSpeedA);
Serial.print(motorSpeedB);
Serial.println();
}
Transmitter Unit Code
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
boolean newData = false;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int motor_Pin1 = A1;
int motor_Pin2 = A2;
float msg[2];
byte leds = 0;
void setup() {
radio.begin();
radio.openWritingPipe(slaveAddress);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
radio.stopListening();
msg[0] = analogRead(motor_Pin1);
msg[1] = analogRead(motor_Pin2);
radio.write(&msg, sizeof(msg));
}
