Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
5.4 KiB

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