I have been working on a remote control car using two nrf24lo1 modules (with adaptor boards) 2 DS04-NFC servo motors 2 Arduino Nanos and a joystick.
It has taken at least three or four months now and it still will not work. I have made the code as best as I can but it still won't do anything.
Here is the controller code:
// RF24 - Version: Latest
#include "RF24.h"
#include <SPI.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node","2Node"};
// Define the Joystick Connections
int joyY = 0;
int joyX = 1;
int valX;
int valY;
void setup() {
serial.begin();
Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
radio.begin(); //start radio communication
radio.setPALevel(RF24_PA_LOW); //power level
radio.setDataRate(RF24_2MBPS); //make it quick
radio.setChannel(124); // Use a channel unlikely to be used by Wifi
radio.openWritingPipe(addresses[1]); //send
radio.openReadingPipe(1, addresses[0]); //recieve
}
void loop() {
valX = analogRead(joyX);
valY = analogRead(joyY);
uint8_t data[2]; // defines size of array
data[0] = valX; // puts the value from the variable valX into the first element
// note elements numbered from 0
// also assumes that valX is defined as uint8_t or byte
data[1] = valY;
radio.write( &data, sizeof(data) );
// Show user what we sent
Serial.print("Sent: ");
Serial.print(valX);
Serial.print(valY);
}
and here is the car code:
// RF24 - Version: Latest
#include "RF24.h"
#include <SPI.h>
// Servo - Version: Latest
#include <Servo.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node","2Node"};
int valX;
int valY;
Servo myservo1;
int myservo1Pin = 3;
Servo myservo2;
int myservo2Pin = 4;
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
myservo1.attach(myservo1Pin);
myservo2.attach(myservo2Pin);
radio.begin(); //start radio communication
radio.setPALevel(RF24_PA_LOW); //power level
radio.setDataRate(RF24_2MBPS); //make it quick
radio.setChannel(124); // Use a channel unlikely to be used by Wifi
radio.openReadingPipe(1, addresses[1]); //recieve
radio.startListening(); //start listening for radio
}
void loop() {
// This is what we receive from the other device (the transmitter)
unsigned char data;
// Is there any data for us to get?
if ( radio.available()) {
// Go and read the data and put it into that variable
while (radio.available()) {
radio.read( &data, sizeof(unsigned int));
}
radio.stopListening();
Serial.print("recieved");
Serial.print(data);
}
{
if (valX < 460) // turn left
{
valY = map(valY, 0, 1023, 0, 180);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY-valX/2); // servo 1 slows
myservo2.write(180-valY);
delay(15);
}
else if (valX > 564) // turn right
{
valY = map(valY, 0, 1023, 0, 180);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY);
myservo2.write((180-valY)-valX/2); // servo 2 slows
delay(15);
}
}
radio.startListening();
}
I used Ralf Bacon's code from his video as a base ( #73 nRF24L01 Send (and receive) data with your Arduino! - YouTube )
Any help would be appreciated so much!
thank you
Jethro Crooke