#include <SPI.h> //this librarys let microcontrollers communicate with each other quickly over short distances(MISO-send data to master from slave)(MOSI-send data from master to peripherals)(SCK-serial clock,clock pulses which synchronize data transmission generated by the master)
#include <RF24.h>
#include <nRF24L01.h>
#include <printf.h> //MEGA: MOSI(pin51)MISO(pin50)SCK(pin52)(CE CSN any pin)
#include <RF24_config.h>
#define CE_PIN 8
#define CSN_PIN 53
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int left_sw = 22;
int right_sw = 24;
int lifting_sw = 44;
int throwing_sw = 45;
int climbing_sw = 43;
int hitting_sw = 14;
int STOP_sw = 41;
int levelUP_sw = 15;
int levelDOWN_sw = 16;
const int sizeOfArray = 9;
int switchModeArray[] = {left_sw, right_sw, lifting_sw, throwing_sw, climbing_sw, hitting_sw, STOP_sw, levelUP_sw, levelDOWN_sw};
int buttonstateArray[sizeOfArray] = {};
struct controllerData
{
bool levelUP_sw;
bool levelDOWN_sw;
bool left_sw;
bool right_sw;
bool lifting_sw;
bool throwing_sw;
bool climbing_sw;
bool hitting_sw;
bool STOP_sw;
}; controllerData data;
void setup(){
Serial.begin(115200);
Serial.println("Starting");
for (int i=0; i<sizeOfArray; i++){
pinMode(switchModeArray[i], INPUT); //Connect all the switch pins to INPUT
}
for (int i=0; i<sizeOfArray; i++){
pinMode(switchModeArray[i], INPUT_PULLUP); //activate the pullup resistors for each switch
}
radio.begin(); //Begin & Link The Radios
radio.setPALevel(RF24_PA_LOW); //set the power consumption
radio.setDataRate(RF24_250KBPS); //set the speed
radio.setRetries(15, 15); //no clue what this does
radio.openWritingPipe(pipe); //opening the pipe for writing only
}
void loop(){
Serial.println("loop begin");
for (int i = 0; i < sizeOfArray; i++) //read all the switchbuttons
{
buttonstateArray[i] = digitalRead(switchModeArray[i]);
}
data.left_sw = buttonstateArray[0]; //put them in the packet so we can send them out
data.right_sw = buttonstateArray[1];
data.lifting_sw = buttonstateArray[2];
data.throwing_sw = buttonstateArray[3];
data.climbing_sw = buttonstateArray[4];
data.hitting_sw = buttonstateArray[5];
data.STOP_sw = buttonstateArray[6];
data.levelUP_sw = buttonstateArray[7];
data.levelDOWN_sw = buttonstateArray[8];
Serial.print("Buttons Loaded to package");
radio.write( &data, sizeof(unsigned long) );
Serial.println("Loop End");
}
the Serial monitor writes
"Starting
loop begin
Buttons Loaded to package"
and thats it, the lopps doest run any more. why is that? It seems that the loop ends when i try to transmit the data to the other NRF24
radio.setRetries(15, 15); //no clue what this does
You need to study the Nordic datasheet for the nRF24L01+. Those number cause it to wait 4000µsecs between retries and to make 15 retries before giving up - a total of about 60 millisecs.
I believe you will understand the concept of the nRF24 pipes better if you change the name of the variable called pipe in your program to address. That number plays the same role as a house address.
radio.setRetries(15, 15); //no clue what this does
You need to study the Nordic datasheet for the nRF24L01+. Those number cause it to wait 4000µsecs between retries and to make 15 retries before giving up - a total of about 60 millisecs.
I believe you will understand the concept of the nRF24 pipes better if you change the name of the variable called pipe in your program to address. That number plays the same role as a house address.
...R
it seems like one of the transceiver was the problem, i am using that same library, i have a 5Volt regulator and have 2 arduino Megas. i ran the loop twice on each arduino using transceiver A and transceiver B, transceiver B seemed to stop the loop twice for both arduinos.
Have you a 10µF capacitor across Vcc and GND for the nRF24s? It is recommended although I have not needed it with my Uno or Mega. I do need it on my breadboard Atmega328 versions.
Robin2:
Have you a 10µF capacitor across Vcc and GND for the nRF24s? It is recommended although I have not needed it with my Uno or Mega. I do need it on my breadboard Atmega328 versions.
...R
i got it to work but it only works when i have i at MIN. i have a 47 µF capacitor on one of them. it 47 too much?
Robin2:
I don't understand - perhaps there is a typo?
...R
hi again so i bought the nrf24 wiht the antenna, i have 6 servos hooked up to the MEGA, i have the MEGA hooked up to a powerbank. When i put the code as "radio.setPALevel(RF24_PA_MIN);" it works fine.
When i put the code as "radio.setPALevel(RF24_PA_MAX);" it constantly loses connection. do i need more power to the arduino? Will connecting a 9 volt AA batteries give the reciever enough power?
If the high-power TX is close to the RX devices its signal can overwhelm them.
You have not posted a diagram showing how you have the high-power device connected. AFAIK it cannot get enough power from the Arduino 3.3v pin - indeed it is almost insufficient for the pcb-antenna versions of the nRF24.