uas_arduino

Scooter Wireless Transmitter

Mar 10th, 2013
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Required to use SPI. */
  2. #include <SPI.h>
  3.  
  4. /* Libraries to talk to the NRF2401 */
  5. #include "nRF24L01.h"
  6. #include "RF24.h"
  7.  
  8. /* Sets up the RF24 library a different way
  9.    for the Arduino Mega 2560. */
  10. #if defined(__AVR_ATmega2560__)
  11.   RF24 radio(53,48);
  12. #else
  13.   RF24 radio(9,10);
  14. #endif
  15.  
  16. /* Defines the structure of the data
  17.    that will be transmitted. */
  18. struct data {
  19.   byte start;
  20.   byte left;
  21.   byte right;
  22.   byte finish;
  23. };
  24.  
  25. /* Give a friendly name to the struct */
  26. typedef struct data Packet;
  27.  
  28. /* Create an instance of the struct. */
  29. Packet myData =  {0, 0, 0, 0};
  30.  
  31. /* Easy way to set the left and right values. */
  32. void setThrottleData(byte left, byte right){
  33.   myData.left = left;
  34.   myData.right = right;
  35. }
  36.  
  37. void setup(){
  38.   /* Sets up the NRF library */
  39.   radio.begin();
  40.  
  41.   /* MUST BE OPPOSITE THE RECEIVER!!! */
  42.   radio.openWritingPipe(0xF0F0F0F0E1LL);
  43.   radio.openReadingPipe(1,0xF0F0F0F0D2LL);
  44.  
  45.   /* Set the radio up to transmit. */
  46.   radio.stopListening();
  47. }
  48.  
  49. /* Hold the test values to send to the receiver. */
  50. byte left = 0x00;
  51. byte right = 0x00;
  52.  
  53. void loop(){
  54.   /* Increment and decrement for tests */
  55.   setThrottleData(left++,right--);
  56.  
  57.   /* Send the entire packet */
  58.   if(radio.write(&myData, sizeof(Packet)) == false){
  59.     Serial.println("Error sending packet");
  60.   }
  61. }
RAW Paste Data