Code Example


/*
 * RF24L01_Receiver.ino by Tucker Ballantyne on 5/5/2016
 * 
 * I downloaded the RF24.h library from:
 * https://arduino-info.wikispaces.com/nRF24L01-RF24-Examples
 * The RF24L01 must use Teensy SPI port pins
 * RF24 wired to teensy as:
 * Teensy     RF24
 * pin 13     SCK
 * pin 12     MISO
 * pin 11     MOSI
 * pin 10     CSN
 * pin 9      CE
 * 3.3V       VCC      3.3 volts is VERY important
 * GND        GND
 * 
 * 
 * Copyright Tucker Ballantyne 2016.  All Rights Reserved.
 * No part of these contents may be reproduced, copied, modified or adapted,
 * without the prior written consent of the author, unless when used for
 * educational and non-profit purposes.?
 */

#include <SPI.h>
#include <RF24.h>
#include <Servo.h>
//********************************************************
//   Radio Setup
//********************************************************
RF24 radio(9, 10); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
const uint64_t writing_pipe = 0xF0F0F0F0E2LL;
const uint64_t reading_pipe = 0xF0F0F0F0E2LL;
//********************************************************
//   Servo Setup
//********************************************************
Servo LServo;
Servo RServo;

const int leftServo = 2;
const int rightServo = 3;

const int left_forward_fast = 2000;       // CCW Fast
const int left_forward_slow = 1650;
const int left_stop = 1500;               // Center position
const int left_backward_slow = 1350;
const int left_backward_fast = 1000;      // CW Fast

const int right_forward_fast = 1100;      // CW Fast
const int right_forward_slow = 1350;
const int right_stop = 1500;              // Center position
const int right_backward_slow = 1625;
const int right_backward_fast = 2000;      // CCW Fast
//********************************************************
//    Setup
//********************************************************
void setup()
{
  Serial.begin(9600);
  radio.begin(); //starts the radio
  radio.setRetries(1, 0); //after failing to receive, the delay and number of retries
  radio.setCRCLength(RF24_CRC_16);
  radio.setAutoAck(false);
  radio.setPayloadSize(sizeof(int)); //maximum size of data that can be sent over the radio
  radio.openReadingPipe(1, reading_pipe); //tells the radio what signal to read data from
  radio.startListening(); //indicates that this radio is a receiver
  //  while(!Serial);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  LServo.attach(2);
  RServo.attach(3);
  blinkOK();
}

void blinkOK() {
  for (int i = 0; i < 3; i++) {
    digitalWrite(13, HIGH);
    delay(500);
    digitalWrite(13, LOW);
    delay(500);
  }
}
#define RECEIVE_DELAY_MICROS 3300
//********************************************************
//   Collect and print Data
//********************************************************
void transfer_all_payloads() {
  int dataTot;
  radio.read(& dataTot, sizeof(int));
  int data1 = dataTot / 1000000;
  int data2 = (dataTot - data1 * 1000000) / 1000;
  int data3 = dataTot - data1 * 1000000 - data2 * 1000;
  //  Serial.println("Sens1= "+(String)data1 + ", sens2=" + (String)data2 + ", sens3=" + (String)data3);
  int mapData3 = map(data3, 300, 600, 1500, 2000);
  int mapData2;
  int mapData1;
  if (mapData3 > 1800) {
    mapData1 = map(data1, 300, 600, 1500, 2000);
    mapData2 = map(data2, 300, 600, 1500, 1000);
  }

  if (mapData3 < 1800) {
    mapData1 = map(data1, 300, 600, 1500, 1000);
    mapData2 = map(data2, 300, 600, 1500, 2000);
  }

  LServo.writeMicroseconds(mapData1);  // Set so that stearing would be horz stick
  RServo.writeMicroseconds(mapData2);  // Set so that stearing would be horz stick
}
//********************************************************
//    Main Loop
//********************************************************
void loop()
{
  //  Serial.print("Received ");
  if (radio.available()) {
    transfer_all_payloads();//   Collect and print Data
  }
}