Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Mirf.h>
- #include <nRF24L01.h>
- #include <MirfHardwareSpiDriver.h>
- struct radio_packet
- {
- uint8_t buttons1;
- uint8_t buttons2;
- uint8_t leftX;
- uint8_t leftY;
- uint8_t rightX;
- uint8_t rightY;
- }radioData;
- uint8_t rx_address[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};
- uint8_t tx_address[5] = {0xD7, 0xD7, 0xD7, 0xD7, 0xD7};
- void setup() {
- Serial.begin(115200);
- Serial.println("Setup radio...");
- // put your setup code here, to run once:
- Mirf.spi = &MirfHardwareSpi;
- Mirf.cePin = 5; // PD5 = 5 (was 8)
- Mirf.csnPin = 17; // PC3 = 17 (was 7)
- Mirf.init();
- Mirf.payload = sizeof(radio_packet);
- Mirf.channel = 2;
- Mirf.config(); //powers up RX by default
- Mirf.configRegister(EN_AA, (1 << ENAA_P0)| (1 << ENAA_P1));
- Mirf.configRegister(RF_SETUP, 0b1111); // 2 MBps rather than 2Mps (for reasons of signal
- Mirf.setRADDR(rx_address);
- Mirf.configRegister(SETUP_RETR, 0b0000 | 0b1111); // MAX auto re-transmit time, max 15 retries (MAX)
- Mirf.setTADDR(tx_address);
- }
- void flushTx(void)
- {
- Mirf.csnLow(); // Pull down chip select
- Mirf.spi->transfer( FLUSH_TX ); // Write cmd to flush tx fifo
- Mirf.csnHi(); // Pull up chip select
- }
- void loop() {
- static long mytimer=0;
- static uint8_t index=0,sending=0; // start not sending
- uint16_t packet=0;
- long mytime=millis();
- struct radio_packet old_packet;
- while(1)
- {
- if (Mirf.dataReady())
- {
- Mirf.getData((uint8_t*) &radioData); // transfer all data (payload = 1 in this case)
- if (radioData.buttons1!=old_packet.buttons1) Serial.println("Buttons 1 changed!");
- if (radioData.buttons2!=old_packet.buttons2) Serial.println("Buttons 2 changed!");
- old_packet=radioData;
- /*Serial.println("****************************************");
- Serial.println(radioData.buttons1,BIN);
- Serial.println(radioData.buttons2,BIN);
- Serial.print(radioData.leftX);Serial.print(","); Serial.print(radioData.leftY);Serial.print(",");
- Serial.print(radioData.rightX);Serial.print(","); Serial.println(radioData.rightY);*/
- packet++;
- }
- if (millis()-mytime>=1000)
- {
- Serial.print("Packets per second:"); Serial.println(packet);
- packet=0;
- mytime=millis();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement