123 lines
2.3 KiB
C++
123 lines
2.3 KiB
C++
/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
|
|
- WHAT IT DOES: Reads Analog values on A0, A1 and transmits
|
|
them over a nRF24L01 Radio Link to another transceiver.
|
|
- SEE the comments after "//" on each line below
|
|
- CONNECTIONS: nRF24L01 Modules See:
|
|
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
|
|
1 - GND
|
|
2 - VCC 3.3V !!! NOT 5V
|
|
3 - CE to Arduino pin 9
|
|
4 - CSN to Arduino pin 10
|
|
5 - SCK to Arduino pin 13
|
|
6 - MOSI to Arduino pin 11
|
|
7 - MISO to Arduino pin 12
|
|
8 - UNUSED
|
|
-
|
|
Analog Joystick or two 10K potentiometers:
|
|
GND to Arduino GND
|
|
VCC to Arduino +5V
|
|
X Pot to Arduino A0
|
|
Y Pot to Arduino A1
|
|
|
|
- V1.00 11/26/13
|
|
Based on examples at http://www.bajdi.com/
|
|
Questions: terry@yourduino.com */
|
|
|
|
/*-----( Import needed libraries )-----*/
|
|
#include <SPI.h>
|
|
#include "nRF24L01.h"
|
|
#include "RF24.h"
|
|
#include "printf.h"
|
|
|
|
|
|
/*-----( Declare Constants and Pin Numbers )-----*/
|
|
#define CE_PIN 9
|
|
#define CSN_PIN 10
|
|
|
|
|
|
// NOTE: the "LL" at the end of the constant is "LongLong" type
|
|
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
|
|
|
|
|
|
/*-----( Declare objects )-----*/
|
|
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
|
|
/*-----( Declare Variables )-----*/
|
|
uint8_t rgb[4];
|
|
int counter = 0;
|
|
int inc = 1;
|
|
int flag = 0;
|
|
|
|
void setup() /****** SETUP: RUNS ONCE ******/
|
|
{
|
|
Serial.begin(57600);
|
|
delay(1000);
|
|
printf_begin();
|
|
radio.begin();
|
|
radio.setPayloadSize(4);
|
|
radio.setAutoAck(0);
|
|
radio.openWritingPipe(pipe);
|
|
radio.printDetails();
|
|
|
|
|
|
}//--(end setup )---
|
|
|
|
|
|
void loop() /****** LOOP: RUNS CONSTANTLY ******/
|
|
{
|
|
|
|
// FADE LED
|
|
/*
|
|
counter = counter + inc;
|
|
if(counter >= 255)
|
|
inc = -1;
|
|
else if(counter <= 0)
|
|
inc = 1;
|
|
|
|
rgb[0] = 1;
|
|
for(int i=1; i<4; i++)
|
|
rgb[i] = counter;
|
|
|
|
//Serial.println(counter);
|
|
|
|
radio.startWrite( rgb, sizeof(rgb) );
|
|
delay(2);
|
|
*/
|
|
|
|
|
|
//STROBO LED
|
|
if (flag)
|
|
flag = 0;
|
|
else
|
|
flag = 1;
|
|
|
|
rgb[0] = 1;
|
|
for(int i=1; i<4; i++)
|
|
rgb[i] = 50*flag;
|
|
|
|
|
|
//Serial.println(counter);
|
|
|
|
radio.writeFast( rgb, sizeof(rgb) );
|
|
delay(100);
|
|
|
|
|
|
/*
|
|
bool ok = radio.write( rgb, sizeof(rgb) );
|
|
if (ok)
|
|
printf("ok\n\r");
|
|
else
|
|
printf("failed\n\r");
|
|
*/
|
|
|
|
|
|
|
|
}//--(end main loop )---
|
|
|
|
/*-----( Declare User-written Functions )-----*/
|
|
|
|
//NONE
|
|
//*********( THE END )***********
|
|
|
|
|
|
|