Hi everyone,
I am trying to use nRF24L01 radios to send data between two Arduino boards. I would like to send one array of data from one uno which is attached to the computer (I will call him the receiver) to another uno (I will call him the transmitter), I would also like to send an array of data from 'the transmitter' uno back to the computer based uno.
The transmitter has attached 2 analogue sensors and 2 buttons, the data of which is sent to the receiver.
The receiver needs to send 3 pieces of information to the transmitter to set the controls for a haptic motor, which I want to be able to change dynamically.
I have the set-up working up to the point of two-way communication, in that I have the transmitter sending what I need to the receiver. I am now trying to send the other data back to the transmitter and have become stuck.
I have lots of nRF24 libraries including mirf, maniacbug and tmrh240 and I have tried scouring forums and examples to help me achieve the above but to no avail.
I thought this last post on here was promising but I couldn't quite make it happen.
I have tried using the above linked forum post to create the skeleton of what i need (might be offering up an xy problem here so sorry if i am!)
So here is the code I adapted from that but all I get are zeros.
The receiver code
/*
http://www.bajdi.com
Receive and transmit analog values RECEIVER
https://forum.arduino.cc/index.php?topic=97026.0
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int joystick[4];
int current[3]; // Array with the 4 motor current values, to sent to remote control
int apin[] = {
A0, A1, A2 }; // analog pin array
RF24 radio(9,10);
const uint64_t pipes[2] = {
0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
void setup(void)
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
}
void loop(void)
{
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &joystick, sizeof(joystick) );
}
}
radio.stopListening();
// 4 analog readings from motor controller = motor current
for (int i=0; i<3; i++)
{
current[i] = analogRead( apin[i] );
}
bool ok = radio.write( ¤t, sizeof(current) );
radio.startListening();
Serial.print(joystick[0]);
Serial.print(" ");
Serial.println(joystick[1]);
}
The transmitter
/*
http://www.bajdi.com
Receive and transmit analog values TRANSMITTER
https://forum.arduino.cc/index.php?topic=97026.0
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int current[3];
int joystick[4]; // Array with the 4 motor current values, to sent to remote control
int apin[] = {
A0, A1, A2, A3 }; // analog pin array
RF24 radio(9,10);
const uint64_t pipes[2] = {
0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
void setup(void)
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
}
void loop(void)
{
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( ¤t, sizeof(current) );
}
}
radio.stopListening();
// 4 analog readings from motor controller = motor current
for (int i=0; i<4; i++)
{
joystick[i] = analogRead (apin[i]) ;
}
bool ok = radio.write( &joystick, sizeof(joystick) );
radio.startListening();
Serial.print(current[0]);
Serial.print(" ");
Serial.println(current[1]);
}
Any help would be very greatly appreciated!
Thanks,
Asha