EDIT: major edit on the post.
I was trying to make a network A → B ->C unidirectional.
After I got Robin2 code I realized I could make a A <-> B,C bidirectional network.
So what I am trying to do now is get radio A to broadcast an analog data to B and C simultaneously so B and C control a servo/motor. So latency is critical here.
after I was able to get this working I am having a new problem: suddenly the motor goes full throttle and I need to restart the ESC and the arduino.
I’m using an arduino Nano chinese clone and a nRF24L01 on each node. in B and C arduino is powered by the ESC 5V port and on the A it’s powered by a 2S lipo.
Usually I’ve seen RC cars going out of control like that when the battery levels are low and they would lose connection to the radio, but it’s happening with all bateries charged.
slave code
// HandController - the slave or the receiver
// http://tmrh20.github.io/RF24/
//~ - CONNECTIONS: nRF24L01 Modules See:
//~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
//~ 1 - GND
//~ 2 - VCC 3.3V !!! NOT 5V
//~ 3 - CE to Arduino pin 9
//~ 4 - CSN to Arduino pin 10
//~ 5 - SCK to Arduino pin 13
//~ 6 - MOSI to Arduino pin 11
//~ 7 - MISO to Arduino pin 12
//~ 8 - UNUSED
#include <SPI.h>
//~ #include <TMRh20nRF24L01.h>
//~ #include <TMRh20RF24.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 9
#define CSN_PIN 10
Servo myservo; // create servo object to control a servo
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t deviceID = 0xE8E8F0F0E1LL; // Define the ID for this slave
int valChange = 1;
int val;
int average;
int xAxis;
int yAxis;
int val3 = 90;
int contador = 0;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataReceived[2];
int ackData[2] = {12,23};
boolean rad = false;
void setup() {
myservo.attach(8);
Serial.begin(9600);
delay(1000);
Serial.println("Receiver 1 Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1,deviceID);
radio.enableAckPayload();
radio.writeAckPayload(1, ackData, sizeof(ackData));
radio.startListening();
}
void loop() {
if ( radio.available() ) {
radio.read( dataReceived, sizeof(dataReceived) );
delay(1);
xAxis = dataReceived[0];
yAxis = dataReceived[1];
radio.writeAckPayload(1, ackData, sizeof(ackData));
ackData[0] += valChange; // this just increments so you can see that new data is being sent
Serial.print("X axis= ");
Serial.print(dataReceived[0]);
Serial.print(" Y axis= ");
Serial.print(dataReceived[1]);
val = map(xAxis, 1023, 0, 0, 179);
val3 = map(val,90,179,90,105);
myservo.write(val3);
Serial.print(" - ");
Serial.println (val3);
contador = (0);
}
else {
contador = (contador +1);
delay(1);
}
if (contador > 300) {
myservo.write(90);
Serial.println("sem sinal");
Serial.println("sem sinal");
Serial.println("sem sinal");
Serial.println("sem sinal");
Serial.println("sem sinal");
contador = (0);
delay(1);
}
}
transmitter code
// TrackControl - the master or the transmitter
// http://tmrh20.github.io/RF24/
//~ - CONNECTIONS: nRF24L01 Modules See:
//~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
//~ 1 - GND
//~ 2 - VCC 3.3V !!! NOT 5V
//~ 3 - CE to Arduino pin 9
//~ 4 - CSN to Arduino pin 10
//~ 5 - SCK to Arduino pin 13
//~ 6 - MOSI to Arduino pin 11
//~ 7 - MISO to Arduino pin 12
//~ 8 - UNUSED
#include <SPI.h>
//~ #include <TMRh20nRF24L01.h>
//~ #include <TMRh20RF24.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define JOYSTICK_X A0
#define JOYSTICK_Y A1
// NOTE: the "LL" at the end of the constant is "LongLong" type
// These are the IDs of each of the slaves
const uint64_t slaveID[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL} ;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend[2];
int joystick[2];
int ackMessg[6];
byte ackMessgLen = 4; // NB this 4 is the number of bytes in the 2 ints that will be recieved
void setup() {
Serial.begin(9600);
Serial.println("Track Control Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
}
//====================
void loop() {
joystick[0] = analogRead(JOYSTICK_X);
joystick[1] = analogRead(JOYSTICK_Y);
radio.openWritingPipe(slaveID[0]); // calls the first slave
bool rslt;
rslt = radio.write( joystick, sizeof(joystick) );
if ( radio.isAckPayloadAvailable() ) {
radio.read(ackMessg,ackMessgLen);
Serial.print("Acknowledge received: ");
Serial.print(ackMessg[0]);
Serial.print(" ");
Serial.println(ackMessg[1]);
}
delay(1);
radio.openWritingPipe(slaveID[1]); // calls the second slave
bool rslt2;
rslt2 = radio.write( joystick, sizeof(joystick) );
if ( radio.isAckPayloadAvailable() ) {
radio.read(ackMessg,ackMessgLen);
Serial.print("Acknowledge received: ");
Serial.print(ackMessg[0]);
Serial.print(" ");
Serial.println(ackMessg[1]);
}
delay(1);
Serial.print("\nRSLT 1 (1 = success) ");
Serial.print(rslt);
Serial.print("RSLT 2 (1 = success) ");
Serial.print(rslt2);
Serial.print("X axis= ");
Serial.print(joystick[0]);
Serial.print("Y axis= ");
Serial.println(joystick[1]);
}
I know it’s messy, but it kinda work most of the time.
PPM_in_example.ino (2.38 KB)
tx_example.ino (2.58 KB)