NFR24 Help

Hi,

I am trying to radio control 2 motors via a joystick, transmitting the data via a NFR24 as transceiver and reciever. I am currently getting all the input data from from the joystick at the transceiver, doing my calculations to alter motor speed and direction dependant on the joystick position. I am then sending this over to the reciever which controls an H bridge. At the moment I have some LEDs to indicate which motors are powered and to what level.

The issue I have is quite frustrating. Half the code works and the LED’s light up as they should, but for 4 out of the 9 switch cases, the NFR24 stops receiving data.

I have checked and data is still being sent. It appears the arduino gets stuck in switch cases and the radio stops receiving. Included is the code below. The cases which do not work are 2, 4, 7 and 8.

Any thoughts or suggestions on this?

Thanks,
Paul.

const int motor1Pin = 4; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 2; // H-bridge leg 2 (pin 7, 2A)
const int motor1_1Pin = 9; //H-bridge leg 3 (pin15, 4A)
const int motor2_2Pin = 7; //H-bridge leg4 (pin10, 3A)

const int speedL = 3; // H-bridge enable pin 1 LEFT MOTOR
const int speedR = 5; // H-bridge enable pin 9 RIGHT MOTOR

#include <SPI.h> // Comes with Arduino IDE
#include “RF24.h” // Download and Install (See above)
#include “printf.h” // Needed for “printDetails” Takes up some memory

#define CE_PIN 7 // The pins to be used for CE and SN
#define CSN_PIN 8

/-----( Declare objects )-----/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 & 8 (Can be changed) */
RF24 radio(CE_PIN, CSN_PIN);

/-----( Declare Variables )-----/
byte addresses[6] = {“1Node”, “2Node”}; // These will be the names of the “Pipes”

// Allows testing of radios and code without servo hardware. Set ‘true’ when servos connected
boolean hasHardware = true; // Allows testing of radios and code without Joystick hardware.
//boolean hasHardware = true;

struct dataStruct {
unsigned long _micros; // to save response times
int Xposition; // The Joystick position values
int Yposition;
int Dir; // which if statement to enter
} myData; // This can be accessed in the form: myData.Xposition etc.

void setup() {
// set all the other pins you’re using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor1_1Pin, OUTPUT);
pinMode(motor2_2Pin, OUTPUT);

pinMode(speedL, OUTPUT);
pinMode(speedR, OUTPUT);

Serial.begin(115200); // MUST reset the Serial Monitor to 115200 (lower right of window )
// NOTE: The “F” in the print statements means “unchangable data; save in Flash Memory to conserve SRAM”
Serial.println(F(“YourDuino.com Example: Receive joystick data by nRF24L01 radio from another Arduino”));
Serial.println(F(“and control servos if attached (Check ‘hasHardware’ variable”));
printf_begin(); // Needed for “printDetails” Takes up some memory

radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // 2.508 Ghz - Above most Wifi Channels
radio.setDataRate(RF24_250KBPS); // Fast enough… Better range

// Set the Power Amplifier Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
// PALevelcan be one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
//radio.setPALevel(RF24_PA_LOW);
radio.setPALevel(RF24_PA_LOW);

// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);

// Start the radio listening for data
radio.startListening();
//radio.printDetails(); //Uncomment to show LOTS of debugging information
}//–(end setup )—

void loop() {

if ( radio.available())
{

while (radio.available()) // While there is data ready to be retrieved from the receive pipe
{
radio.read( &myData, sizeof(myData) ); // Get the data
}

radio.stopListening(); // First, stop listening so we can transmit
radio.write( &myData, sizeof(myData) ); // Send the received data back.
radio.startListening(); // Now, resume listening so we catch the next packets.

Serial.print(F(“Packet Received - Sent response “)); // Print the received packet data
Serial.print(myData._micros);
Serial.print(”\n”);
Serial.print(F(“uS X= “));
Serial.print(myData.Xposition);
Serial.print(”\n”);
Serial.print(F(" Y= “));
Serial.print(myData.Yposition);
Serial.print(”\n");
Serial.print(F(“if loop= “));
Serial.print(myData.Dir);
Serial.print(”\n”);

} // END radio available

int speedLval;
int speedRval;
int Direction;

if (hasHardware) { //get the values recieved.
speedLval = myData.Xposition;
speedRval = myData.Yposition;
Direction = myData.Dir;

}

switch (Direction) {
case 0:
//do nothing - joystick at rest
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 1:
//TURN ON SPOT ANTICLOCKWISE
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1_1Pin, LOW);
digitalWrite(motor2_2Pin, HIGH);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 2:
//TURN ON SPOT CLOCKWISE
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor1_1Pin, HIGH);
digitalWrite(motor2_2Pin, LOW);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 3:
//FORWARDS
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor1_1Pin, LOW);
digitalWrite(motor2_2Pin, HIGH);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 4:
//BACKWARDS
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1_1Pin, HIGH);
digitalWrite(motor2_2Pin, LOW);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 5:
// FRONT LEFT
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor1_1Pin, LOW);
digitalWrite(motor2_2Pin, HIGH);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 6:
//FRONT RIGHT
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor1_1Pin, LOW);
digitalWrite(motor2_2Pin, HIGH);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 7:
//BACK LEFT
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1_1Pin, HIGH);
digitalWrite(motor2_2Pin, LOW);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
case 8:
//BACK RIGHT
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1_1Pin, HIGH);
digitalWrite(motor2_2Pin, LOW);
analogWrite(speedL, speedLval);
analogWrite(speedR, speedRval);
break;
}
}

//else {
//analogWrite(speedL, 0);
//analogWrite(speedR, 0);
//digitalWrite(ledPin, LOW);

So it was a hardware issue in the end. I had my wires crossed and was sending a 'low' signal switching off the reciever.

I am not sure how to delete this post, but if a moderator sees this please delete.

Paultn12:
So it was a hardware issue in the end. I had my wires crossed and was sending a ‘low’ signal switching off the reciever.

I am not sure how to delete this post, but if a moderator sees this please delete.

It is usual NOT to delete posts as that allows other people to benefit from your experience.

For the future pleas post your program using the code button </>

so it looks like this

. See How to use the Forum

…R
Simple nRF24L01+ Tutorial