Difference between revisions of "Nrf24L01-2.4GHz-ExampleSketches"

From ArduinoInfo
Jump to: navigation, search
m
m (Text replacement - "http://https://" to "https://")
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Nrf24L01-2.4GHz-ExampleSketches=  
+
=Nrf24L01-2.4GHz-ExampleSketches= __NOTOC__
  
 
These are example Arduino software sketches that show how to use the nRF24L01 radio modules.
 
These are example Arduino software sketches that show how to use the nRF24L01 radio modules.
Start with our overall [[arduino-info/Nrf24L01-2.4GHz-HowTo|nRF24L01 Information and How-To HERE]] to make sure you connect the nRF24L01 modules correctly. Consider using one of the [[http://yourduino.com/sunshop2/index.php?l=product_detail&p=467|"base modules"]] to get stable 3.3V DC power to your modules. Otherwise consider soldering capacitors across Vcc and Gnd on your modules. See the How-To..
+
Start with our overall [[Nrf24L01-2.4GHz-HowTo|nRF24L01 Information and How-To HERE]] to make sure you connect the nRF24L01 modules correctly. Consider using one of the [[http://yourduino.com/sunshop2/index.php?l=product_detail&p=467|"base modules"]] to get stable 3.3V DC power to your modules. Otherwise consider soldering capacitors across Vcc and Gnd on your modules. See the How-To..
 +
 
 +
Below are the different examples as they are written and tested here. Click on the names below. There are also many examples that come with the RF24 library and you can see them [https://github.com/nRF24/RF24/tree/master/examples RIGHT HERE]
  
Below are the different examples as they are written and tested here. Click on the names below. There are also many examples that come with the RF24 library and you can [[http://tmrh20.github.io/RF24/examples.html|find them HERE]]
 
 
----
 
----
 
#[[Nrf24L01-2.4GHz-ExampleSketches#bm1|BARE MINIMUM EXAMPLE]]</span>
 
#[[Nrf24L01-2.4GHz-ExampleSketches#bm1|BARE MINIMUM EXAMPLE]]</span>
# <span style="font-size: 1.3em; line-height: 1.5;">[[Nrf24L01-2.4GHz-ExampleSketches#js1|TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS]]</span>
+
#[[Nrf24L01-2.4GHz-ExampleSketches#js1|TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS]]
 
<span id="bm1"> </span>
 
<span id="bm1"> </span>
 
==Bare Minimum example:==  
 
==Bare Minimum example:==  
Line 14: Line 15:
 
==nRF24L01-SimpleTransmit-01==  
 
==nRF24L01-SimpleTransmit-01==  
  
[[media type="custom" key="28252767"]]
+
<shtml hash="e44227b28bffb659d1f8dd4c4bcbd43bf737754b23462a9b174f1d4f6fc408f8"><pre>
 +
<font color="#95A5A6">/* YourDuinoStarter Example: Simple nRF24L01 Transmit</font>
 +
<font color="#95A5A6">  - WHAT IT DOES: Transmits simple fixed data with nRF24L01 radio</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  Start with radios about 4 feet apart.</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  - CONNECTIONS: nRF24L01 Modules See:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://arduinoinfo.mywikis.net/wiki/Nrf24L01-2.4GHz-HowTo</font></u>
 +
<font color="#95A5A6">  Uses the RF24 Library by TMRH2o here:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u>
 +
<font color="#95A5A6">  1 - GND</font>
 +
<font color="#95A5A6">  2 - VCC 3.3V !!! NOT 5V</font>
 +
<font color="#95A5A6">  3 - CE to Arduino pin 7</font>
 +
<font color="#95A5A6">  4 - CSN to Arduino pin 8</font>
 +
<font color="#95A5A6">  5 - SCK to Arduino pin 13</font>
 +
<font color="#95A5A6">  6 - MOSI to Arduino pin 11</font>
 +
<font color="#95A5A6">  7 - MISO to Arduino pin 12</font>
 +
<font color="#95A5A6">  8 - UNUSED</font>
 +
 
 +
<font color="#95A5A6">  V1.02 02/06/2016</font>
 +
<font color="#95A5A6">  Questions: terry@yourduino.com */</font>
 +
 
 +
<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Comes with Arduino IDE</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
 +
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
 +
<font color="#434F54">//None yet</font>
 +
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
 +
<font color="#434F54">// (Create an instance of a radio, specifying the CE and CS pins. )</font>
 +
<font color="#000000">RF24</font> <font color="#000000">myRadio</font> <font color="#000000">(</font><font color="#000000">7</font><font color="#434F54">,</font> <font color="#000000">8</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// "myRadio" is the identifier you will use in following methods</font>
 +
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
 +
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// Create address for 1 pipe.</font>
 +
<font color="#00979C">int</font> <font color="#000000">dataTransmitted</font><font color="#000000">;</font>  <font color="#434F54">// Data that will be Transmitted from the transmitter</font>
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
 +
<font color="#000000">{</font>
 +
  <font color="#434F54">// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"RF24/Simple Transmit data Test"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Questions: terry@yourduino.com"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">dataTransmitted</font> <font color="#434F54">=</font> <font color="#000000">100</font><font color="#000000">;</font> <font color="#434F54">// Arbitrary known data to transmit. Change it to test...</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Start up the physical nRF24L01 Radio</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Above most Wifi Channels</font>
 +
  <font color="#434F54">// Set the PA Level low to prevent power supply related issues since this is a</font>
 +
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_MIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#434F54">//  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power</font>
 +
 
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">openWritingPipe</font><font color="#000000">(</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Use the first entry in array 'addresses' (Only 1 right now)</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
 +
<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
 +
<font color="#000000">{</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">write</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">dataTransmitted</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">dataTransmitted</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">//  Transmit the data</font>
 +
 
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Data Transmitted = "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">dataTransmitted</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">" No Acknowledge expected"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">dataTransmitted</font> <font color="#434F54">=</font> <font color="#000000">dataTransmitted</font> <font color="#434F54">+</font> <font color="#000000">1</font><font color="#000000">;</font>  <font color="#434F54">// Send different data next time</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>
 +
 
 +
 
 +
<font color="#434F54">//*********( THE END )***********</font>
 +
 
 +
 
 +
</pre></shtml>
 +
 
 
==nRF24L01-SimpleReceive-01==  
 
==nRF24L01-SimpleReceive-01==  
  
[[media type="custom" key="28252771"]]
+
<shtml hash="0f095342e2635f1ca9aa6e422a950849c0cbd8b7bdac4a014d7edcfd0e24459b"><pre>
 +
<font color="#95A5A6">/* YourDuinoStarter Example: Simple nRF24L01 Receive</font>
 +
<font color="#95A5A6">  - WHAT IT DOES: Receives simple fixed data with nRF24L01 radio</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  Start with radios about 4 feet apart.</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  - CONNECTIONS: nRF24L01 Modules See:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://arduinoinfo.mywikis.net/wiki/Nrf24L01-2.4GHz-HowTo</font></u>
 +
<font color="#95A5A6">  Uses the RF24 Library by TMRH2o here:</font>
 +
<font color="#95A5A6">  </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u>
 +
<font color="#95A5A6">  1 - GND</font>
 +
<font color="#95A5A6">  2 - VCC 3.3V !!! NOT 5V</font>
 +
<font color="#95A5A6">  3 - CE to Arduino pin 7</font>
 +
<font color="#95A5A6">  4 - CSN to Arduino pin 8</font>
 +
<font color="#95A5A6">  5 - SCK to Arduino pin 13</font>
 +
<font color="#95A5A6">  6 - MOSI to Arduino pin 11</font>
 +
<font color="#95A5A6">  7 - MISO to Arduino pin 12</font>
 +
<font color="#95A5A6">  8 - UNUSED</font>
 +
 
 +
<font color="#95A5A6">  V1.02 02/06/2016</font>
 +
<font color="#95A5A6">  Questions: terry@yourduino.com */</font>
 +
 
 +
<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Comes with Arduino IDE</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
 +
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
 +
<font color="#434F54">//None yet</font>
 +
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
 +
<font color="#434F54">// (Create an instance of a radio, specifying the CE and CS pins. )</font>
 +
<font color="#000000">RF24</font> <font color="#000000">myRadio</font> <font color="#000000">(</font><font color="#000000">7</font><font color="#434F54">,</font> <font color="#000000">8</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// "myRadio" is the identifier you will use in following methods</font>
 +
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
 +
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// Create address for 1 pipe.</font>
 +
<font color="#00979C">int</font> <font color="#000000">dataReceived</font><font color="#000000">;</font>  <font color="#434F54">// Data that will be received from the transmitter</font>
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
 +
<font color="#000000">{</font>
 +
  <font color="#434F54">// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"RF24/Simple Receive data Test"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Questions: terry@yourduino.com"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Start up the physical nRF24L01 Radio</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Above most Wifi Channels</font>
 +
  <font color="#434F54">// Set the PA Level low to prevent power supply related issues since this is a</font>
 +
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_MIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#434F54">//  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power</font>
 +
 
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">openReadingPipe</font><font color="#000000">(</font><font color="#000000">1</font><font color="#434F54">,</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Use the first entry in array 'addresses' (Only 1 right now)</font>
 +
  <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
 +
<font color="#000000">{</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font> <font color="#434F54">// Check for incoming data from transmitter</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#5E6D03">while</font> <font color="#000000">(</font><font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font>  <font color="#434F54">// While there is data ready</font>
 +
    <font color="#000000">{</font>
 +
      <font color="#000000">myRadio</font><font color="#434F54">.</font><font color="#D35400">read</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">dataReceived</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Get the data payload (You must have defined that already!)</font>
 +
    <font color="#000000">}</font>
 +
    <font color="#434F54">// DO something with the data, like print it</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#00979C">"Data received = "</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#000000">dataReceived</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font> <font color="#434F54">//END Radio available</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>
 +
 
 +
<font color="#434F54">//None yet</font>
 +
<font color="#434F54">//*********( THE END )***********</font>
 +
 
 +
 
 +
</pre></shtml>
 
----
 
----
==[[#js1]]==
+
<span id="js1"> </span>
 
==TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS==  
 
==TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS==  
 
These examples include a full round-trip error checking and acknowledge system. They use many of the features of the nRF24L01 Radios and the RF24 Library by TMRH2O such as setting the channel frequency and the power output level. They print the activity and data received on the Serial Monitor at 115200 speed.
 
These examples include a full round-trip error checking and acknowledge system. They use many of the features of the nRF24L01 Radios and the RF24 Library by TMRH2O such as setting the channel frequency and the power output level. They print the activity and data received on the Serial Monitor at 115200 speed.
 
**OPTIONS:** //**Hardware**// - This version defaults to using fixed data with no required Joystick or Servos, for test purposes. There is a variable in both sketches called 'hasHardware' which is set to 'false'. When you have joystick and/or servo hardware connected then set hasHardware = true;
 
**OPTIONS:** //**Hardware**// - This version defaults to using fixed data with no required Joystick or Servos, for test purposes. There is a variable in both sketches called 'hasHardware' which is set to 'false'. When you have joystick and/or servo hardware connected then set hasHardware = true;
**OPTIONS: //Radio RF Power Level//** - If you get your radios communicating OK and you think you have good 3.3V power, you can set the RF Power Amplifier to "RF24_PA_MAX". It is initially set to "RF24_PA_LOW" to be less sensitive to poor 3.3V power. See [[arduino-info/Nrf24L01-2.4GHz-HowTo#PP|This Page ]]about nRY24L01 problems, power solutions. We strongly recommend the 'base modules' to get reliable information.
+
**OPTIONS: //Radio RF Power Level//** - If you get your radios communicating OK and you think you have good 3.3V power, you can set the RF Power Amplifier to "RF24_PA_MAX". It is initially set to "RF24_PA_LOW" to be less sensitive to poor 3.3V power. See [[arduino-info/Nrf24L01-2.4GHz-HowTo#PP|This Page ]]about nRF24L01 problems, power solutions. We strongly recommend the 'base modules' to get reliable information.
 
**SERVOS:** Servos draw a lot of current and will **not** work when the Arduino is powered from USB. You must use some external 5V power supply for the servos. The YourDuino RoboRED pictured on the How-to page has a built-in 5V 2A power supply that can run 2 servos when the External power connector is supplied with 9 to 12V.
 
**SERVOS:** Servos draw a lot of current and will **not** work when the Arduino is powered from USB. You must use some external 5V power supply for the servos. The YourDuino RoboRED pictured on the How-to page has a built-in 5V 2A power supply that can run 2 servos when the External power connector is supplied with 9 to 12V.
 
===SKETCH: JOYSTICK DATA TRANSMITTED TO ANOTHER ARDUINO===  
 
===SKETCH: JOYSTICK DATA TRANSMITTED TO ANOTHER ARDUINO===  
 
(Cut and paste into a blank Arduino IDE window)
 
(Cut and paste into a blank Arduino IDE window)
[[media type="custom" key="28256273"]]
+
 
 +
<shtml hash="7eeaf8b072e72946862f5cf96f0d0c77082c74ec9c80d724593fd0612c0e0187"><pre>
 +
<font color="#95A5A6">/* YourDuinoStarter Example: nRF24L01 Radio Remote control: Joystick to Servos</font>
 +
<font color="#95A5A6">  - WHAT IT DOES</font>
 +
<font color="#95A5A6">  Joystick on this Arduino communicates by nRF25L01 Radio to</font>
 +
<font color="#95A5A6">  a second Arduino with an nRF24L01 radio and 2 pan-tilt servos</font>
 +
<font color="#95A5A6">  SEE: The variable 'hasHardware'. You can test without Joystick and later set hasHardware = true;</font>
 +
 
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  - CONNECTIONS:</font>
 +
<font color="#95A5A6">  - nRF24L01 Radio Module: See </font><u><font color="#95A5A6">https://arduinoinfo.mywikis.net/wiki/Nrf24L01-2.4GHz-HowTo</font></u>
 +
<font color="#95A5A6">  1 - GND</font>
 +
<font color="#95A5A6">  2 - VCC 3.3V !!! NOT 5V</font>
 +
<font color="#95A5A6">  3 - CE to Arduino pin 7</font>
 +
<font color="#95A5A6">  4 - CSN to Arduino pin 8</font>
 +
<font color="#95A5A6">  5 - SCK to Arduino pin 13</font>
 +
<font color="#95A5A6">  6 - MOSI to Arduino pin 11</font>
 +
<font color="#95A5A6">  7 - MISO to Arduino pin 12</font>
 +
<font color="#95A5A6">  8 - UNUSED</font>
 +
<font color="#95A5A6">  - V2.12 02/08/2016</font>
 +
<font color="#95A5A6">  - Uses the RF24 Library by TMRH20 and Maniacbug: </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u><font color="#95A5A6"> (Download ZIP)</font>
 +
<font color="#95A5A6">  Questions: terry@yourduino.com */</font>
 +
 
 +
<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Comes with Arduino IDE</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"printf.h"</font> <font color="#434F54">// Needed for "printDetails" Takes up some memory</font>
 +
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
 +
<font color="#5E6D03">#define</font>  <font color="#000000">CE_PIN</font>  <font color="#000000">7</font>  <font color="#434F54">// The pins to be used for CE and SN</font>
 +
<font color="#5E6D03">#define</font>  <font color="#000000">CSN_PIN</font> <font color="#000000">8</font>
 +
 
 +
<font color="#5E6D03">#define</font> <font color="#000000">JOYSTICK_X</font>  <font color="#000000">A0</font>  <font color="#434F54">// The Joystick potentiometers connected to Arduino Analog inputs</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">JOYSTICK_Y</font>  <font color="#000000">A1</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">JOYSTICK_SW</font>  <font color="#000000">A2</font>  <font color="#434F54">// The Joystick push-down switch, will be used as a Digital input</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
 +
<font color="#95A5A6">/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 &amp; 8 (Can be changed) */</font>
 +
<font color="#000000">RF24</font> <font color="#000000">radio</font><font color="#000000">(</font><font color="#000000">CE_PIN</font><font color="#434F54">,</font> <font color="#000000">CSN_PIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
 +
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#434F54">,</font> <font color="#00979C">"2Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// These will be the names of the "Pipes"</font>
 +
 
 +
<font color="#00979C">unsigned</font> <font color="#00979C">long</font> <font color="#000000">timeNow</font><font color="#000000">;</font>  <font color="#434F54">// Used to grab the current time, calculate delays</font>
 +
<font color="#00979C">unsigned</font> <font color="#00979C">long</font> <font color="#000000">started_waiting_at</font><font color="#000000">;</font>
 +
<font color="#00979C">boolean</font> <font color="#000000">timeout</font><font color="#000000">;</font>      <font color="#434F54">// Timeout? True or False</font>
 +
 
 +
<font color="#434F54">// Allows testing of radios and code without Joystick hardware. Set 'true' when joystick connected</font>
 +
<font color="#00979C">boolean</font> <font color="#000000">hasHardware</font> <font color="#434F54">=</font> <font color="#00979C">false</font><font color="#000000">;</font>
 +
<font color="#434F54">//boolean hasHardware = true;</font>
 +
 
 +
<font color="#95A5A6">/**</font>
 +
<font color="#95A5A6">  Create a data structure for transmitting and receiving data</font>
 +
<font color="#95A5A6">  This allows many variables to be easily sent and received in a single transmission</font>
 +
<font color="#95A5A6">  See </font><u><font color="#95A5A6">http://www.cplusplus.com/doc/tutorial/structures/</font></u>
 +
<font color="#95A5A6">*/</font>
 +
<font color="#5E6D03">struct</font> <font color="#000000">dataStruct</font> <font color="#000000">{</font>
 +
  <font color="#00979C">unsigned</font> <font color="#00979C">long</font> <font color="#000000">_micros</font><font color="#000000">;</font>  <font color="#434F54">// to save response times</font>
 +
  <font color="#00979C">int</font> <font color="#000000">Xposition</font><font color="#000000">;</font>          <font color="#434F54">// The Joystick position values</font>
 +
  <font color="#00979C">int</font> <font color="#000000">Yposition</font><font color="#000000">;</font>
 +
  <font color="#00979C">bool</font> <font color="#000000">switchOn</font><font color="#000000">;</font>          <font color="#434F54">// The Joystick push-down switch</font>
 +
<font color="#000000">}</font> <font color="#000000">myData</font><font color="#000000">;</font>                <font color="#434F54">// This can be accessed in the form:  myData.Xposition  etc.</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
 +
<font color="#000000">{</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// MUST reset the Serial Monitor to 115200 (lower right of window )</font>
 +
  <font color="#434F54">// NOTE: The "F" in the print statements means "unchangable data; save in Flash Memory to conserve SRAM"</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"YourDuino.com Example: Send joystick data by nRF24L01 radio to another Arduino"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">printf_begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Needed for "printDetails" Takes up some memory</font>
 +
  <font color="#D35400">pinMode</font><font color="#000000">(</font><font color="#000000">JOYSTICK_SW</font><font color="#434F54">,</font> <font color="#00979C">INPUT_PULLUP</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Pin A2 will be used as a digital input</font>
 +
 
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>          <font color="#434F54">// Initialize the nRF24L01 Radio</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Above most WiFi frequencies</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setDataRate</font><font color="#000000">(</font><font color="#000000">RF24_250KBPS</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Fast enough.. Better range</font>
 +
 
 +
  <font color="#434F54">// Set the Power Amplifier Level low to prevent power supply related issues since this is a</font>
 +
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
 +
  <font color="#434F54">// PALevelcan be one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_LOW</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#434F54">//  radio.setPALevel(RF24_PA_MAX);</font>
 +
 
 +
  <font color="#434F54">// Open a writing and reading pipe on each radio, with opposite addresses</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">openWritingPipe</font><font color="#000000">(</font><font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">openReadingPipe</font><font color="#000000">(</font><font color="#000000">1</font><font color="#434F54">,</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">1</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#434F54">// Start the radio listening for data</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#434F54">//  radio.printDetails(); //Uncomment to show LOTS of debugging information</font>
 +
<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
 +
<font color="#000000">{</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">stopListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>                                    <font color="#434F54">// First, stop listening so we can talk.</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font><font color="#000000">hasHardware</font><font color="#000000">)</font>  <font color="#434F54">// Set in variables at top</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#95A5A6">/*********************( Read the Joystick positions )*************************/</font>
 +
    <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Xposition</font> <font color="#434F54">=</font> <font color="#D35400">analogRead</font><font color="#000000">(</font><font color="#000000">JOYSTICK_X</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Yposition</font> <font color="#434F54">=</font> <font color="#D35400">analogRead</font><font color="#000000">(</font><font color="#000000">JOYSTICK_Y</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">switchOn</font>  <font color="#434F54">=</font> <font color="#434F54">!</font><font color="#D35400">digitalRead</font><font color="#000000">(</font><font color="#000000">JOYSTICK_SW</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Invert the pulldown switch</font>
 +
  <font color="#000000">}</font>
 +
  <font color="#5E6D03">else</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Xposition</font> <font color="#434F54">=</font> <font color="#000000">256</font><font color="#000000">;</font>  <font color="#434F54">// Send some known fake data</font>
 +
    <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Yposition</font> <font color="#434F54">=</font> <font color="#000000">512</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font>
 +
 
 +
  <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">_micros</font> <font color="#434F54">=</font> <font color="#D35400">micros</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Send back for timing</font>
 +
 
 +
 
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Now sending  -  "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font><font color="#434F54">!</font><font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">write</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">myData</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">)</font> <font color="#000000">{</font>            <font color="#434F54">// Send data, checking for error ("!" means NOT)</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Transmit failed "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font>
 +
 
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>                                    <font color="#434F54">// Now, continue listening</font>
 +
 
 +
  <font color="#000000">started_waiting_at</font> <font color="#434F54">=</font> <font color="#D35400">micros</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>              <font color="#434F54">// timeout period, get the current microseconds</font>
 +
  <font color="#000000">timeout</font> <font color="#434F54">=</font> <font color="#00979C">false</font><font color="#000000">;</font>                            <font color="#434F54">//  variable to indicate if a response was received or not</font>
 +
 
 +
  <font color="#5E6D03">while</font> <font color="#000000">(</font> <font color="#434F54">!</font> <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">)</font> <font color="#000000">{</font>                            <font color="#434F54">// While nothing is received</font>
 +
    <font color="#5E6D03">if</font> <font color="#000000">(</font><font color="#D35400">micros</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#434F54">-</font> <font color="#000000">started_waiting_at</font> <font color="#434F54">&gt;</font> <font color="#000000">200000</font> <font color="#000000">)</font> <font color="#000000">{</font>          <font color="#434F54">// If waited longer than 200ms, indicate timeout and exit while loop</font>
 +
      <font color="#000000">timeout</font> <font color="#434F54">=</font> <font color="#00979C">true</font><font color="#000000">;</font>
 +
      <font color="#5E6D03">break</font><font color="#000000">;</font>
 +
    <font color="#000000">}</font>
 +
  <font color="#000000">}</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">timeout</font> <font color="#000000">)</font>
 +
  <font color="#000000">{</font> <font color="#434F54">// Describe the results</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Response timed out -  no Acknowledge."</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font>
 +
  <font color="#5E6D03">else</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#434F54">// Grab the response, compare, and send to Serial Monitor</font>
 +
    <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">read</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">myData</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">timeNow</font> <font color="#434F54">=</font> <font color="#D35400">micros</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
    <font color="#434F54">// Show it</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Sent "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">timeNow</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">", Got response "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">_micros</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">", Round-trip delay "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">timeNow</font> <font color="#434F54">-</font> <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">_micros</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">" microseconds "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#000000">}</font>
 +
 
 +
  <font color="#434F54">// Send again after delay. When working OK, change to something like 100</font>
 +
  <font color="#D35400">delay</font><font color="#000000">(</font><font color="#000000">100</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>
 +
 
 +
<font color="#434F54">// NONE YET</font>
 +
<font color="#434F54">//*********( THE END )***********</font>
 +
 
 +
 
 +
</pre></shtml>
 +
 
 +
 
 
----
 
----
 
===SKETCH: JOYSTICK DATA RECEIVED FROM ANOTHER ARDUINO===  
 
===SKETCH: JOYSTICK DATA RECEIVED FROM ANOTHER ARDUINO===  
 
(Cut and paste into a blank Arduino IDE window)
 
(Cut and paste into a blank Arduino IDE window)
[[media type="custom" key="28256275"]]
+
 
 +
<shtml hash="81329f8a9995d0f82efe852c80092531f5338f2cd09ae8db7511ce44cec3f449"><pre>
 +
<font color="#95A5A6">/* YourDuinoStarter Example: nRF24L01 Radio remote control of servos by joystick</font>
 +
<font color="#95A5A6">  - WHAT IT DOES</font>
 +
<font color="#95A5A6">  Joystick on other Arduino communicates by nRF25L01 Radio to</font>
 +
<font color="#95A5A6">  this Arduino with 2 pan-tilt servos</font>
 +
<font color="#95A5A6">  SEE: The variable 'hasHardware'. You can test without servos and later set hasHardware = true;</font>
 +
<font color="#95A5A6">        You NEED separate Servo power, not USB. YourDuino RoboRED has built in 2A power for servos</font>
 +
<font color="#95A5A6">  - SEE the comments after "//" on each line below</font>
 +
<font color="#95A5A6">  - CONNECTIONS:</font>
 +
<font color="#95A5A6">  - nRF24L01 Radio Module: See </font><u><font color="#95A5A6">https://arduinoinfo.mywikis.net/wiki/Nrf24L01-2.4GHz-HowTo</font></u>
 +
<font color="#95A5A6">  1 - GND</font>
 +
<font color="#95A5A6">  2 - VCC 3.3V !!! NOT 5V</font>
 +
<font color="#95A5A6">  3 - CE to Arduino pin 7</font>
 +
<font color="#95A5A6">  4 - CSN to Arduino pin 8</font>
 +
<font color="#95A5A6">  5 - SCK to Arduino pin 13</font>
 +
<font color="#95A5A6">  6 - MOSI to Arduino pin 11</font>
 +
<font color="#95A5A6">  7 - MISO to Arduino pin 12</font>
 +
<font color="#95A5A6">  8 - UNUSED</font>
 +
 
 +
<font color="#95A5A6">  - V2.12 02/08/2016</font>
 +
<font color="#95A5A6">  - Uses the RF24 Library by TMRH20 and Maniacbug: </font><u><font color="#95A5A6">https://github.com/TMRh20/RF24</font></u><font color="#95A5A6"> (Download ZIP)</font>
 +
<font color="#95A5A6">  Questions: terry@yourduino.com */</font>
 +
 
 +
<font color="#95A5A6">/*-----( Import needed libraries )-----*/</font>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><b><font color="#D35400">SPI</font></b><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Comes with Arduino IDE</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"RF24.h"</font>  <font color="#434F54">// Download and Install (See above)</font>
 +
<font color="#5E6D03">#include</font> <font color="#00979C">"printf.h"</font> <font color="#434F54">// Needed for "printDetails" Takes up some memory</font>
 +
<font color="#434F54">// NEED the SoftwareServo library installed</font>
 +
<font color="#434F54">// </font><u><font color="#434F54">http://playground.arduino.cc/uploads/ComponentLib/SoftwareServo.zip</font></u>
 +
<font color="#5E6D03">#include</font> <font color="#434F54">&lt;</font><font color="#000000">SoftwareServo</font><font color="#434F54">.</font><font color="#000000">h</font><font color="#434F54">&gt;</font>  <font color="#434F54">// Regular Servo library creates timer conflict!</font>
 +
<font color="#95A5A6">/*-----( Declare Constants and Pin Numbers )-----*/</font>
 +
<font color="#5E6D03">#define</font>  <font color="#000000">CE_PIN</font>  <font color="#000000">7</font>  <font color="#434F54">// The pins to be used for CE and SN</font>
 +
<font color="#5E6D03">#define</font>  <font color="#000000">CSN_PIN</font> <font color="#000000">8</font>
 +
 
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoHorizontalPIN</font> <font color="#000000">3</font>  <font color="#434F54">//Pin Numbers for servos and laser/LED</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoVerticalPIN</font>  <font color="#000000">5</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">LaserPIN</font>          <font color="#000000">6</font>
 +
 
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoMIN_H</font>  <font color="#000000">30</font>  <font color="#434F54">// Don't go to very end of servo travel</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoMAX_H</font>  <font color="#000000">150</font> <font color="#434F54">// which may not be all the way from 0 to 180. </font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoMIN_V</font>  <font color="#000000">30</font>  <font color="#434F54">// Don't go to very end of servo travel</font>
 +
<font color="#5E6D03">#define</font> <font color="#000000">ServoMAX_V</font>  <font color="#000000">150</font> <font color="#434F54">// which may not be all the way from 0 to 180</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare objects )-----*/</font>
 +
<font color="#95A5A6">/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 &amp; 8 (Can be changed) */</font>
 +
<font color="#000000">RF24</font> <font color="#000000">radio</font><font color="#000000">(</font><font color="#000000">CE_PIN</font><font color="#434F54">,</font> <font color="#000000">CSN_PIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
<font color="#000000">SoftwareServo</font> <font color="#000000">HorizontalServo</font><font color="#000000">;</font>
 +
<font color="#000000">SoftwareServo</font> <font color="#000000">VerticalServo</font><font color="#000000">;</font>  <font color="#434F54">// create servo objects to control servos</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare Variables )-----*/</font>
 +
<font color="#00979C">byte</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">]</font><font color="#000000">[</font><font color="#000000">6</font><font color="#000000">]</font> <font color="#434F54">=</font> <font color="#000000">{</font><font color="#00979C">"1Node"</font><font color="#434F54">,</font> <font color="#00979C">"2Node"</font><font color="#000000">}</font><font color="#000000">;</font> <font color="#434F54">// These will be the names of the "Pipes"</font>
 +
 
 +
<font color="#434F54">// Allows testing of radios and code without servo hardware. Set 'true' when servos connected</font>
 +
<font color="#00979C">boolean</font> <font color="#000000">hasHardware</font> <font color="#434F54">=</font> <font color="#00979C">false</font><font color="#000000">;</font>  <font color="#434F54">// Allows testing of radios and code without Joystick hardware.</font>
 +
<font color="#434F54">//boolean hasHardware = true;</font>
 +
 
 +
<font color="#00979C">int</font> <font color="#000000">HorizontalJoystickReceived</font><font color="#000000">;</font> <font color="#434F54">// Variable to store received Joystick values</font>
 +
<font color="#00979C">int</font> <font color="#000000">HorizontalServoPosition</font><font color="#000000">;</font>    <font color="#434F54">// variable to store the servo position</font>
 +
 
 +
<font color="#00979C">int</font> <font color="#000000">VerticalJoystickReceived</font><font color="#000000">;</font>  <font color="#434F54">// Variable to store received Joystick values</font>
 +
<font color="#00979C">int</font> <font color="#000000">VerticalServoPosition</font><font color="#000000">;</font>      <font color="#434F54">// variable to store the servo positio</font>
 +
 
 +
<font color="#95A5A6">/**</font>
 +
<font color="#95A5A6">  Create a data structure for transmitting and receiving data</font>
 +
<font color="#95A5A6">  This allows many variables to be easily sent and received in a single transmission</font>
 +
<font color="#95A5A6">  See </font><u><font color="#95A5A6">http://www.cplusplus.com/doc/tutorial/structures/</font></u>
 +
<font color="#95A5A6">*/</font>
 +
<font color="#5E6D03">struct</font> <font color="#000000">dataStruct</font> <font color="#000000">{</font>
 +
  <font color="#00979C">unsigned</font> <font color="#00979C">long</font> <font color="#000000">_micros</font><font color="#000000">;</font>  <font color="#434F54">// to save response times</font>
 +
  <font color="#00979C">int</font> <font color="#000000">Xposition</font><font color="#000000">;</font>          <font color="#434F54">// The Joystick position values</font>
 +
  <font color="#00979C">int</font> <font color="#000000">Yposition</font><font color="#000000">;</font>
 +
  <font color="#00979C">bool</font> <font color="#000000">switchOn</font><font color="#000000">;</font>          <font color="#434F54">// The Joystick push-down switch</font>
 +
<font color="#000000">}</font> <font color="#000000">myData</font><font color="#000000">;</font>                <font color="#434F54">// This can be accessed in the form:  myData.Xposition  etc.</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">setup</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** SETUP: RUNS ONCE ******/</font>
 +
<font color="#000000">{</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">115200</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// MUST reset the Serial Monitor to 115200 (lower right of window )</font>
 +
  <font color="#434F54">// NOTE: The "F" in the print statements means "unchangable data; save in Flash Memory to conserve SRAM"</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"YourDuino.com Example: Receive joystick data by nRF24L01 radio from another Arduino"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"and control servos if attached (Check 'hasHardware' variable"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">printf_begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Needed for "printDetails" Takes up some memory</font>
 +
  <font color="#95A5A6">/*-----( Set up servos )-----*/</font>
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font><font color="#000000">hasHardware</font><font color="#000000">)</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#000000">HorizontalServo</font><font color="#434F54">.</font><font color="#D35400">attach</font><font color="#000000">(</font><font color="#000000">ServoHorizontalPIN</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// attaches the servo to the servo object</font>
 +
    <font color="#000000">VerticalServo</font><font color="#434F54">.</font><font color="#D35400">attach</font><font color="#000000">(</font><font color="#000000">ServoVerticalPIN</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font>
 +
 
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">begin</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>          <font color="#434F54">// Initialize the nRF24L01 Radio</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setChannel</font><font color="#000000">(</font><font color="#000000">108</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// 2.508 Ghz - Above most Wifi Channels</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setDataRate</font><font color="#000000">(</font><font color="#000000">RF24_250KBPS</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434F54">// Fast enough.. Better range</font>
 +
 
 +
  <font color="#434F54">// Set the Power Amplifier Level low to prevent power supply related issues since this is a</font>
 +
  <font color="#434F54">// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.</font>
 +
  <font color="#434F54">// PALevelcan be one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">setPALevel</font><font color="#000000">(</font><font color="#000000">RF24_PA_LOW</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#434F54">//  radio.setPALevel(RF24_PA_MAX);</font>
 +
 
 +
  <font color="#434F54">// Open a writing and reading pipe on each radio, with opposite addresses</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">openWritingPipe</font><font color="#000000">(</font><font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">1</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">openReadingPipe</font><font color="#000000">(</font><font color="#000000">1</font><font color="#434F54">,</font> <font color="#000000">addresses</font><font color="#000000">[</font><font color="#000000">0</font><font color="#000000">]</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
  <font color="#434F54">// Start the radio listening for data</font>
 +
  <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>
 +
<font color="#434F54">//  radio.printDetails(); //Uncomment to show LOTS of debugging information</font>
 +
<font color="#000000">}</font><font color="#434F54">//--(end setup )---</font>
 +
 
 +
 
 +
<font color="#00979C">void</font> <font color="#5E6D03">loop</font><font color="#000000">(</font><font color="#000000">)</font>  <font color="#95A5A6">/****** LOOP: RUNS CONSTANTLY ******/</font>
 +
<font color="#000000">{</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font>
 +
  <font color="#000000">{</font>
 +
 
 +
    <font color="#5E6D03">while</font> <font color="#000000">(</font><font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">available</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">)</font>  <font color="#434F54">// While there is data ready to be retrieved from the receive pipe</font>
 +
    <font color="#000000">{</font>
 +
      <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">read</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">myData</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font>            <font color="#434F54">// Get the data</font>
 +
    <font color="#000000">}</font>
 +
 
 +
    <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">stopListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>                              <font color="#434F54">// First, stop listening so we can transmit</font>
 +
    <font color="#000000">radio</font><font color="#434F54">.</font><font color="#D35400">write</font><font color="#000000">(</font> <font color="#434F54">&amp;</font><font color="#000000">myData</font><font color="#434F54">,</font> <font color="#5E6D03">sizeof</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font>              <font color="#434F54">// Send the received data back.</font>
 +
    <font color="#000000">radio</font><font color="#434F54">.</font><font color="#000000">startListening</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font>                              <font color="#434F54">// Now, resume listening so we catch the next packets.</font>
 +
 
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"Packet Received - Sent response "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>  <font color="#434F54">// Print the received packet data</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">_micros</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">"uS X= "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Xposition</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">" Y= "</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">print</font><font color="#000000">(</font><font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Yposition</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#5E6D03">if</font> <font color="#000000">(</font> <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">switchOn</font> <font color="#434F54">==</font> <font color="#000000">1</font><font color="#000000">)</font>
 +
    <font color="#000000">{</font>
 +
      <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">" Switch ON"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">}</font>
 +
    <font color="#5E6D03">else</font>
 +
    <font color="#000000">{</font>
 +
      <b><font color="#D35400">Serial</font></b><font color="#434F54">.</font><font color="#D35400">println</font><font color="#000000">(</font><font color="#D35400">F</font><font color="#000000">(</font><font color="#00979C">" Switch OFF"</font><font color="#000000">)</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">}</font>
 +
 
 +
  <font color="#000000">}</font> <font color="#434F54">// END radio available</font>
 +
 
 +
  <font color="#5E6D03">if</font> <font color="#000000">(</font><font color="#000000">hasHardware</font><font color="#000000">)</font>
 +
  <font color="#000000">{</font>
 +
    <font color="#95A5A6">/*-----( Calculate servo position values, send to the servos )-----*/</font>
 +
    <font color="#000000">SoftwareServo</font><font color="#434F54">:</font><font color="#434F54">:</font><font color="#000000">refresh</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">;</font><font color="#434F54">//refreshes servo to keep them updating</font>
 +
    <font color="#000000">HorizontalJoystickReceived</font>  <font color="#434F54">=</font> <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Xposition</font><font color="#000000">;</font>  <font color="#434F54">// Get the values received</font>
 +
    <font color="#000000">VerticalJoystickReceived</font>    <font color="#434F54">=</font> <font color="#000000">myData</font><font color="#434F54">.</font><font color="#000000">Yposition</font><font color="#000000">;</font>
 +
 
 +
    <font color="#434F54">// scale it to use it with the servo (value between MIN and MAX)</font>
 +
    <font color="#000000">HorizontalServoPosition</font>  <font color="#434F54">=</font> <font color="#D35400">map</font><font color="#000000">(</font><font color="#000000">HorizontalJoystickReceived</font><font color="#434F54">,</font> <font color="#000000">0</font><font color="#434F54">,</font> <font color="#000000">1023</font><font color="#434F54">,</font> <font color="#000000">ServoMIN_H</font> <font color="#434F54">,</font> <font color="#000000">ServoMAX_H</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">VerticalServoPosition</font>    <font color="#434F54">=</font> <font color="#D35400">map</font><font color="#000000">(</font><font color="#000000">VerticalJoystickReceived</font><font color="#434F54">,</font>  <font color="#000000">0</font><font color="#434F54">,</font> <font color="#000000">1023</font><font color="#434F54">,</font> <font color="#000000">ServoMIN_V</font> <font color="#434F54">,</font> <font color="#000000">ServoMAX_V</font><font color="#000000">)</font><font color="#000000">;</font>
 +
 
 +
    <font color="#434F54">// tell servos to go to position</font>
 +
    <font color="#000000">HorizontalServo</font><font color="#434F54">.</font><font color="#D35400">write</font><font color="#000000">(</font><font color="#000000">HorizontalServoPosition</font><font color="#000000">)</font><font color="#000000">;</font>
 +
    <font color="#000000">VerticalServo</font><font color="#434F54">.</font><font color="#D35400">write</font><font color="#000000">(</font><font color="#000000">VerticalServoPosition</font><font color="#000000">)</font><font color="#000000">;</font>
 +
  <font color="#000000">}</font> <font color="#434F54">// END hasHardware</font>
 +
<font color="#000000">}</font><font color="#434F54">//--(end main loop )---</font>
 +
 
 +
<font color="#95A5A6">/*-----( Declare User-written Functions )-----*/</font>
 +
 
 +
<font color="#434F54">// NONE YET</font>
 +
<font color="#434F54">//*********( THE END )***********</font>
 +
 
 +
 
 +
</pre></shtml>
 +
 
 
----
 
----
 
zz
 
zz

Latest revision as of 17:20, 4 February 2019

Nrf24L01-2.4GHz-ExampleSketches

These are example Arduino software sketches that show how to use the nRF24L01 radio modules. Start with our overall nRF24L01 Information and How-To HERE to make sure you connect the nRF24L01 modules correctly. Consider using one of the ["base modules"] to get stable 3.3V DC power to your modules. Otherwise consider soldering capacitors across Vcc and Gnd on your modules. See the How-To..

Below are the different examples as they are written and tested here. Click on the names below. There are also many examples that come with the RF24 library and you can see them RIGHT HERE


  1. BARE MINIMUM EXAMPLE</span>
  2. TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS

Bare Minimum example:

This is not a very good example for practical use, but it details the few minimum things you need to do to create radio sketches that work for nRF24L01 Transmit and Receive. Copy and paste these examples into a BLANK window on your Arduino IDE. VERIFY to check them out. You NEED the RF24 library downloaded and installed. See the How-To.

nRF24L01-SimpleTransmit-01

Error: Invalid hash.

nRF24L01-SimpleReceive-01

Error: Invalid hash.

TRANSMIT JOYSTICK POSITION DATA TO ANOTHER ARDUINO WITH 2 SERVOS

These examples include a full round-trip error checking and acknowledge system. They use many of the features of the nRF24L01 Radios and the RF24 Library by TMRH2O such as setting the channel frequency and the power output level. They print the activity and data received on the Serial Monitor at 115200 speed.

    • OPTIONS:** //**Hardware**// - This version defaults to using fixed data with no required Joystick or Servos, for test purposes. There is a variable in both sketches called 'hasHardware' which is set to 'false'. When you have joystick and/or servo hardware connected then set hasHardware = true;
    • OPTIONS: //Radio RF Power Level//** - If you get your radios communicating OK and you think you have good 3.3V power, you can set the RF Power Amplifier to "RF24_PA_MAX". It is initially set to "RF24_PA_LOW" to be less sensitive to poor 3.3V power. See This Page about nRF24L01 problems, power solutions. We strongly recommend the 'base modules' to get reliable information.
    • SERVOS:** Servos draw a lot of current and will **not** work when the Arduino is powered from USB. You must use some external 5V power supply for the servos. The YourDuino RoboRED pictured on the How-to page has a built-in 5V 2A power supply that can run 2 servos when the External power connector is supplied with 9 to 12V.

SKETCH: JOYSTICK DATA TRANSMITTED TO ANOTHER ARDUINO

(Cut and paste into a blank Arduino IDE window)

Error: Invalid hash.



SKETCH: JOYSTICK DATA RECEIVED FROM ANOTHER ARDUINO

(Cut and paste into a blank Arduino IDE window)

Error: Invalid hash.

zz