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.
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.
15 * On the 'remote', connect any number of buttons or switches from
16 * an arduino pin to ground. Update 'button_pins' to reflect the
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.
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.
34 // Hardware configuration
37 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
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);
57 // Single radio pipe address for the 2 nodes to communicate.
58 const uint64_t pipe = 0xE8E8F0F0E1LL;
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
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
83 uint8_t button_states[num_button_pins];
84 uint8_t led_states[num_led_pins];
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) )
113 printf("\n\rRF24/examples/led_remote/\n\r");
114 printf("ROLE: %s\n\r",role_friendly_name[role]);
117 // Setup and configure rf radio
123 // Open pipes to other nodes for communication
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 )
131 radio.openWritingPipe(pipe);
135 radio.openReadingPipe(1,pipe);
142 if ( role == role_led )
143 radio.startListening();
146 // Dump the configuration of the rf unit for debugging
149 radio.printDetails();
152 // Set up buttons / LED's
155 // Set pull-up resistors for all buttons
156 if ( role == role_remote )
158 int i = num_button_pins;
161 pinMode(button_pins[i],INPUT);
162 digitalWrite(button_pins[i],HIGH);
166 // Turn LED's ON until we start getting keys
167 if ( role == role_led )
169 int i = num_led_pins;
172 pinMode(button_pins[i],OUTPUT);
173 led_states[i] = HIGH;
174 digitalWrite(led_pins[i],led_states[i]);
187 // Remote role. If the state of any button has changed, send the whole state of
191 if ( role == role_remote )
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;
199 uint8_t state = ! digitalRead(button_pins[i]);
200 if ( state != button_states[i] )
203 button_states[i] = state;
207 // Send the state of the buttons to the LED board
210 printf("Now sending...");
211 bool ok = radio.write( button_states, num_button_pins );
215 printf("failed\n\r");
218 // Try again in a short while
223 // LED role. Receive the state of all buttons, and reflect that in the LEDs
226 if ( role == role_led )
228 // if there is data ready
229 if ( radio.available() )
231 // Dump the payloads until we've gotten everything
235 // Fetch the payload, and see if this was the last one.
236 done = radio.read( button_states, num_button_pins );
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;
245 if ( button_states[i] )
247 led_states[i] ^= HIGH;
248 digitalWrite(led_pins[i],led_states[i]);
255 // vim:ai:cin:sts=2 sw=2 ft=cpp