Move default pins over to Getting Started defaults
[RF24-C.git] / examples / led_remote / led_remote.pde
blob02f89f5e35c7fde0b292926ff896e8f73288477c
1 /*
2  Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7  */
9 /**
10  * Example LED Remote
11  *
12  * This is an example of how to use the RF24 class to control a remote
13  * bank of LED's using buttons on a remote control.
14  *
15  * On the 'remote', connect any number of buttons or switches from
16  * an arduino pin to ground.  Update 'button_pins' to reflect the
17  * pins used.
18  *
19  * On the 'led' board, connect the same number of LED's from an
20  * arduino pin to a resistor to ground.  Update 'led_pins' to reflect
21  * the pins used.  Also connect a separate pin to ground and change
22  * the 'role_pin'.  This tells the sketch it's running on the LED board.
23  *
24  * Every time the buttons change on the remote, the entire state of
25  * buttons is send to the led board, which displays the state.
26  */
28 #include <SPI.h>
29 #include "nRF24L01.h"
30 #include "RF24.h"
31 #include "printf.h"
34 // Hardware configuration
37 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
39 RF24 radio(9,10);
41 // sets the role of this unit in hardware.  Connect to GND to be the 'led' board receiver
42 // Leave open to be the 'remote' transmitter
43 const int role_pin = A4;
45 // Pins on the remote for buttons
46 const uint8_t button_pins[] = { 2,3,4,5,6,7 };
47 const uint8_t num_button_pins = sizeof(button_pins);
49 // Pins on the LED board for LED's
50 const uint8_t led_pins[] = { 2,3,4,5,6,7 };
51 const uint8_t num_led_pins = sizeof(led_pins);
54 // Topology
57 // Single radio pipe address for the 2 nodes to communicate.
58 const uint64_t pipe = 0xE8E8F0F0E1LL;
61 // Role management
63 // Set up role.  This sketch uses the same software for all the nodes in this
64 // system.  Doing so greatly simplifies testing.  The hardware itself specifies
65 // which node it is.
67 // This is done through the role_pin
70 // The various roles supported by this sketch
71 typedef enum { role_remote = 1, role_led } role_e;
73 // The debug-friendly names of those roles
74 const char* role_friendly_name[] = { "invalid", "Remote", "LED Board"};
76 // The role of the current running sketch
77 role_e role;
80 // Payload
83 uint8_t button_states[num_button_pins];
84 uint8_t led_states[num_led_pins];
87 // Setup
90 void setup(void)
92   //
93   // Role
94   //
96   // set up the role pin
97   pinMode(role_pin, INPUT);
98   digitalWrite(role_pin,HIGH);
99   delay(20); // Just to get a solid reading on the role pin
101   // read the address pin, establish our role
102   if ( digitalRead(role_pin) )
103     role = role_remote;
104   else
105     role = role_led;
107   //
108   // Print preamble
109   //
111   Serial.begin(57600);
112   printf_begin();
113   printf("\n\rRF24/examples/led_remote/\n\r");
114   printf("ROLE: %s\n\r",role_friendly_name[role]);
116   //
117   // Setup and configure rf radio
118   //
120   radio.begin();
122   //
123   // Open pipes to other nodes for communication
124   //
126   // This simple sketch opens a single pipes for these two nodes to communicate
127   // back and forth.  One listens on it, the other talks to it.
129   if ( role == role_remote )
130   {
131     radio.openWritingPipe(pipe);
132   }
133   else
134   {
135     radio.openReadingPipe(1,pipe);
136   }
138   //
139   // Start listening
140   //
142   if ( role == role_led )
143     radio.startListening();
145   //
146   // Dump the configuration of the rf unit for debugging
147   //
149   radio.printDetails();
151   //
152   // Set up buttons / LED's
153   //
155   // Set pull-up resistors for all buttons
156   if ( role == role_remote )
157   {
158     int i = num_button_pins;
159     while(i--)
160     {
161       pinMode(button_pins[i],INPUT);
162       digitalWrite(button_pins[i],HIGH);
163     }
164   }
166   // Turn LED's ON until we start getting keys
167   if ( role == role_led )
168   {
169     int i = num_led_pins;
170     while(i--)
171     {
172       pinMode(button_pins[i],OUTPUT);
173       led_states[i] = HIGH;
174       digitalWrite(led_pins[i],led_states[i]);
175     }
176   }
181 // Loop
184 void loop(void)
186   //
187   // Remote role.  If the state of any button has changed, send the whole state of
188   // all buttons.
189   //
191   if ( role == role_remote )
192   {
193     // Get the current state of buttons, and
194     // Test if the current state is different from the last state we sent
195     int i = num_button_pins;
196     bool different = false;
197     while(i--)
198     {
199       uint8_t state = ! digitalRead(button_pins[i]);
200       if ( state != button_states[i] )
201       {
202         different = true;
203         button_states[i] = state;
204       }
205     }
207     // Send the state of the buttons to the LED board
208     if ( different )
209     {
210       printf("Now sending...");
211       bool ok = radio.write( button_states, num_button_pins );
212       if (ok)
213         printf("ok\n\r");
214       else
215         printf("failed\n\r");
216     }
218     // Try again in a short while
219     delay(20);
220   }
222   //
223   // LED role.  Receive the state of all buttons, and reflect that in the LEDs
224   //
226   if ( role == role_led )
227   {
228     // if there is data ready
229     if ( radio.available() )
230     {
231       // Dump the payloads until we've gotten everything
232       bool done = false;
233       while (!done)
234       {
235         // Fetch the payload, and see if this was the last one.
236         done = radio.read( button_states, num_button_pins );
238         // Spew it
239         printf("Got buttons\n\r");
241         // For each button, if the button now on, then toggle the LED
242         int i = num_led_pins;
243         while(i--)
244         {
245           if ( button_states[i] )
246           {
247             led_states[i] ^= HIGH;
248             digitalWrite(led_pins[i],led_states[i]);
249           }
250         }
251       }
252     }
253   }
255 // vim:ai:cin:sts=2 sw=2 ft=cpp