Advertisement
shepherdingelectrons

Arduino Mirf receiver

Jun 11th, 2019
3,638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <Mirf.h>
  4. #include <nRF24L01.h>
  5. #include <MirfHardwareSpiDriver.h>
  6.  
  7. struct radio_packet
  8. {
  9.   uint8_t buttons1;
  10.   uint8_t buttons2;
  11.   uint8_t leftX;
  12.   uint8_t leftY;
  13.   uint8_t rightX;
  14.   uint8_t rightY;
  15. }radioData;
  16.  
  17. uint8_t rx_address[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};
  18. uint8_t tx_address[5] = {0xD7, 0xD7, 0xD7, 0xD7, 0xD7};
  19.  
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.  
  24.   Serial.println("Setup radio...");
  25.  
  26.   // put your setup code here, to run once:
  27.   Mirf.spi = &MirfHardwareSpi;
  28.  
  29.   Mirf.cePin = 5; // PD5 = 5 (was 8)
  30.   Mirf.csnPin = 17; // PC3 = 17 (was 7)
  31.  
  32.   Mirf.init();
  33.   Mirf.payload = sizeof(radio_packet);
  34.   Mirf.channel = 2;
  35.   Mirf.config(); //powers up RX by default
  36.  
  37.   Mirf.configRegister(EN_AA, (1 << ENAA_P0)| (1 << ENAA_P1));
  38.   Mirf.configRegister(RF_SETUP, 0b1111); // 2 MBps rather than 2Mps (for reasons of signal
  39.  
  40.   Mirf.setRADDR(rx_address);
  41.  
  42.   Mirf.configRegister(SETUP_RETR, 0b0000 | 0b1111); // MAX auto re-transmit time, max 15 retries (MAX)
  43.  
  44.   Mirf.setTADDR(tx_address);          
  45. }
  46.  
  47. void flushTx(void)
  48. {
  49.     Mirf.csnLow();                    // Pull down chip select
  50.     Mirf.spi->transfer( FLUSH_TX );     // Write cmd to flush tx fifo
  51.     Mirf.csnHi();                    // Pull up chip select
  52. }
  53.  
  54. void loop() {
  55.   static long mytimer=0;
  56.   static uint8_t index=0,sending=0; // start not sending
  57.   uint16_t packet=0;
  58.   long mytime=millis();
  59.   struct radio_packet old_packet;
  60.  
  61.   while(1)
  62.   {
  63.   if (Mirf.dataReady())
  64.   {
  65.      Mirf.getData((uint8_t*) &radioData); // transfer all data (payload = 1 in this case)
  66.  
  67.      if (radioData.buttons1!=old_packet.buttons1) Serial.println("Buttons 1 changed!");
  68.      if (radioData.buttons2!=old_packet.buttons2) Serial.println("Buttons 2 changed!");
  69.  
  70.      old_packet=radioData;
  71.      /*Serial.println("****************************************");
  72.      Serial.println(radioData.buttons1,BIN);
  73.      Serial.println(radioData.buttons2,BIN);
  74.      Serial.print(radioData.leftX);Serial.print(","); Serial.print(radioData.leftY);Serial.print(",");
  75.      Serial.print(radioData.rightX);Serial.print(","); Serial.println(radioData.rightY);*/
  76.      packet++;
  77.   }
  78.   if (millis()-mytime>=1000)
  79.   {
  80.     Serial.print("Packets per second:"); Serial.println(packet);
  81.       packet=0;
  82.       mytime=millis();
  83.   }
  84.   }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement