I am trying to control an RC Car using nrf24l01 transceivers, arduino uno, potentiometers on the tx side and servos on the receiver side. Trying to make the throttle work first. Somehow nothing seems to be working. I am also trying to code the trottle servo in such a way that one servo can control both throttle and brake (left - throttle, right - brake) but am not able to write the code for that. Both brake and throttle have different potentiomenters attached which will transmit individual data. Can someone help.
Receiver side code:
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h> // Comes with Arduino IDE
#include <Servo.h> // Regular Servo library creates timer conflict!
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 7 // The pins to be used for CE and SN
#define CSN_PIN 8
int servoPin = 5;
/-----( Declare objects )-----/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 & 8 (Can be changed) */
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
Servo ThrottleServo; // create servo objects to control servos
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // 2.508 Ghz - Above most Wifi Channels
radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
radio.setPALevel(RF24_PA_MAX);
// radio.setPALevel(RF24_PA_MAX);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openReadingPipe(1, pipe);
// Start the radio listening for data
radio.startListening();
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
while(Serial.available()== 0);
int data = Serial.read() - '0';
int pos = map(data, 0, 9, 0, 150);
pos = constrain(pos, 0, 15xc0);
//Turn the servo
ThrottleServo.write(pos);
Serial.flush();
}//--(end main loop )---
Tx side code:
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h> // Comes with Arduino IDE
#include <Servo.h> // Regular Servo library creates timer conflict!
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 7 // The pins to be used for CE and SN
#define CSN_PIN 8
int potPin = 0;
RF24 radio(7, 8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // Above most WiFi frequencies
radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
radio.setPALevel(RF24_PA_MAX);
// radio.setPALevel(RF24_PA_MAX);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(pipe);
}//--(end setup )---
void loop()
{
int val = map(analogRead(potPin), 0, 1023, 0, 255);
char buffer[30];
sprintf(buffer,"%d",val);
radio.write(buffer, 30);
delay(50);
}
NRF connections:
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 7
4 - CSN to Arduino pin 8
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED
