I have been working on a remote control car for a few months now and i have had a few errors along the way but that is all fixed and i finally got the code to upload... and it dosen't work.
Here is the controller code:
// RF24 - Version: Latest
#include "RF24.h"
#include <SPI.h>
// 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 joyposY = 512;
int joyposX = 512;
int valX;
int valY;
void setup() {
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
radio.startListening(); //start listening for radio
}
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.stopListening();
if (!radio.write( &data, sizeof(unsigned char) )) {
Serial.println("No acknowledgement of transmission - receiving radio device connected?");
}
radio.startListening();
// But it won't listen for long, 200 milliseconds is enough
unsigned long started_waiting_at = millis();
// Loop here until we get indication that some data is ready for us to read (or we time out)
while ( ! radio.available() ) {
//No response received within our timescale
if (millis() - started_waiting_at > 200 ) {
Serial.println("No response received - timeout");
return;
}
// Now read the data that is waiting for us in the nRF24L01's buffer
unsigned char dataRx;
radio.read( &dataRx, sizeof(unsigned char) );
// Show user what we sent and what we got back
Serial.print("Sent ");
Serial.print(", received: ");
Serial.println(dataRx);
}
}
and this 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 = 4;
Servo myservo2;
int myservo2Pin = 5;
void setup() {
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.openWritingPipe(addresses[0]); //send
radio.openReadingPipe(1, addresses[1]); //recieve
radio.startListening(); //start listening for radio
}
void loop() {
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
radio.begin();
// 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(char));
}
radio.stopListening();
Serial.print("recieved");
}
{
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);
}
}
}
the rx LED ( which indicates if you are receiving data) on the car is not lighting up. I think that may be the main issue but i have no clue how to fix it.
thank you very much for your help.
jethro crooke