Hello,
I have been working with 2 NRF24 modules I created and have been seeing relative success; however, I have one issue that has been giving me some trouble. Right now, I am writing two simple programs for two NRF24 devices to get the core functionality correct; a receiver program and a transmitter program. I can successfully get the two to communicate, but I have not been able to reduce the payload size lower than 32 bytes. I would like to optimize communications by sending payloads only as long as the messages.
The message is a 1 element byte array that cycles from 17 to 35. These values are sent to the receiver and used to drive the PWM duty cycle for a servo motor.
The "setPayloadSize" function accepts values from 1 -32 (bytes), corresponding to the payload length needed to deliver the message. By my understanding, I am only sending 1 byte, so I should be able to set the payload size to as little as 1 byte in order to transmit correctly. However, I am unable to send messages unless the payload size is not specified (I believe the default payload length is 32) or is set to 32.
Any thoughts or suggestions on this subject would be greatly appreciated. Thanks!
Transmitter - Using ManiacBug's NRF24 Library
Loaded onto Arduino Uno Rev 3
//CMK
//Wireless Serial Servo Command (Transmitter)
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//declare 8 bit variable pwm (0 - 255)
byte pwm[1];
//Declare an instance of RF24 library (device is tx)
RF24 tx(9,10);
//Declare the device address
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void) {
//Begin Serial Communications at 9600 baud rate
Serial.begin(9600);
//Enable NRF24 Chip
tx.begin();
//Use the address "pipe" when transmitting
tx.openWritingPipe(pipe);
tx.setPALevel(RF24_PA_HIGH);
tx.setDataRate(RF24_250KBPS);
//Transmit does not work when size of payload is reduced - PROBLEM IS HERE
tx.setPayloadSize(32);
//initalize pwm to servo center position
pwm[0] = 26;
}
void loop(void) {
//send the pwm DC, $ points to pwm as a variable
tx.write(pwm, 1);
//Cycle Through PWM values of 17 - 35
if (pwm[0] < 35){
pwm[0] = pwm[0] + 1;
}
else {
pwm[0] = 17;
}
//Needed; otherwise delay messes with transmit
tx.powerDown();
delay (100);
tx.powerUp();
}
Receiver Code - Using ManicBug's NRF24 Library
Loaded onto Arduino Rev3
#include <SPI.h>
#include <stdint.h>
#include "nRF24L01.h"
#include "RF24.h"
//Declare a radio
RF24 rx(9,10);
//receive messages from only this addrice
const uint64_t pipe = 0xE8E8F0F0E1LL;
//Define receiving me
byte pwm[1];
void setup(void) {
//Setup serial comms at 2400 baud rate
Serial.begin(2400);
TCCR0B = TCCR0B & B11111000 | B00000101; // set timer 0 divisor to 1024 for PWM frequency of 61.04 Hz
//Initialize receiver
rx.begin();
rx.openReadingPipe(1,pipe);
rx.startListening();
rx.setDataRate(RF24_250KBPS);
//Unable to set lower than 32 bytes - PROBLEM IS HERE
rx.setPayloadSize(32);
}
void loop(void) {
if (rx.available()){
bool done = false;
while (!done){
done = rx.read(pwm,1);
Serial.println(pwm[0]);
}
}
else {
Serial.println("No radio available");
}
rx.powerDown();
analogWrite(5,pwm[0]);
rx.powerUp();
}