SHARE
TWEET

Untitled

a guest Oct 5th, 2018 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Project: nRF905 AVR/Arduino Library/Driver (Low power ping server example)
  3.  * Author: Zak Kemble, contact@zakkemble.co.uk
  4.  * Copyright: (C) 2017 by Zak Kemble
  5.  * License: GNU GPL v3 (see License.txt)
  6.  * Web: http://blog.zakkemble.co.uk/nrf905-avrarduino-librarydriver/
  7.  */
  8.  
  9. /*
  10.  * Low power ping server
  11.  *
  12.  * Similar to the pin server example
  13.  * Output power is set to the lowest setting, receive sensitivity is lowered.
  14.  *
  15.  * 7 -> CE
  16.  * 8 -> PWR
  17.  * 9 -> TXE
  18.  * 4 -> CD
  19.  * 3 -> DR
  20.  * 2 -> AM
  21.  * 10 -> CSN
  22.  * 12 -> SO
  23.  * 11 -> SI
  24.  * 13 -> SCK
  25.  */
  26.  
  27. #include <nRF905.h>
  28. #include <Servo.h>
  29.  
  30. #define RXADDR 0x586F2E10 // Address of this device
  31. #define TXADDR 0xFE4CA6E5 // Address of device to send to
  32.  
  33. #define PACKET_NONE    0
  34. #define PACKET_OK   1
  35. #define PACKET_INVALID  2
  36.  
  37. int servoPin = 6;
  38.  
  39. Servo servo;
  40.  
  41. int servoAngle = 0;
  42.  
  43. static volatile uint8_t packetStatus;
  44.  
  45. void NRF905_CB_RXCOMPLETE(void)
  46. {
  47.   packetStatus = PACKET_OK;
  48.   nRF905_standby();
  49. }
  50.  
  51. void NRF905_CB_RXINVALID(void)
  52. {
  53.   packetStatus = PACKET_INVALID;
  54.   nRF905_standby();
  55. }
  56.  
  57. void setup()
  58. {
  59.   Serial.begin(115200);
  60.   servo.attach(servoPin);
  61.   Serial.println(F("Server started"));
  62.  
  63.   pinMode(A5, OUTPUT); // LED
  64.  
  65.   // Start up
  66.   nRF905_init();
  67.  
  68.   // Set address of this device
  69.   nRF905_setListenAddress(RXADDR);
  70.  
  71.   // Lowest transmit level -10db
  72.   nRF905_setTransmitPower(NRF905_PWR_n10);
  73.  
  74.   // Reduce receive sensitivity to save a few mA
  75.   nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  76.  
  77.   // Put into receive mode
  78.   nRF905_RX();
  79. }
  80.  
  81. void loop()             // Loop > while / if
  82. {
  83.   static uint32_t pings;
  84.   static uint32_t invalids;
  85.   static uint32_t volts;
  86.  
  87.   Serial.println(F("---- Incoming Message ----"));
  88.  
  89.   // Wait for data
  90.   while(packetStatus == PACKET_NONE);
  91.    
  92.   if(packetStatus != PACKET_OK)
  93.   {
  94.     invalids++;
  95.     Serial.println(F("Invalid packet! Appox on Thee!"));
  96.     //Serial.println("Haven't seen this one yet");
  97.     packetStatus = PACKET_NONE;
  98.     nRF905_RX();
  99.   }
  100.   else // this is what it should do when it works properly
  101.   {
  102.     pings++;
  103.     packetStatus = PACKET_NONE;
  104.  
  105.     // Make buffer for data
  106.     uint8_t buffer[NRF905_MAX_PAYLOAD];
  107.     nRF905_read(buffer, sizeof(buffer));
  108.  
  109.     Serial.println(F("Ten 4, sending reply..."));
  110.  
  111.     // Send back the data, once the transmission has completed go into receive mode
  112.     while(!nRF905_TX(TXADDR, buffer, sizeof(buffer), NRF905_NEXTMODE_RX));
  113.  
  114.     Serial.println(F("Reply sernt"));
  115.  
  116.     // Toggle LED
  117.     static uint8_t ledState;
  118.     digitalWrite(A5, ledState ? HIGH : LOW);
  119.     ledState = !ledState;
  120. // char cats = (buffer, sizeof(buffer));
  121.    int cats; // Print out ping contents
  122.     Serial.print(F("Data from serrrver: "));
  123.     Serial.write(buffer, sizeof(buffer));
  124.     FFF = Serial.write(buffer, sizeof(buffer));
  125.     Serial.println();
  126.     Serial.println("cats >>>");
  127.     Serial.println(cats);
  128.   //  servo.write(buffer, sizeof(buffer));
  129.     delay(500);
  130.   //  servo.write(20);
  131.   //  delay(1000);
  132.  
  133.   }
  134.  
  135.     // Pings & invalids
  136.  
  137. //  Serial.print(F("Totals: "));
  138. //  Serial.print(pings);
  139. //  Serial.print(F(" Pings, "));
  140. //  Serial.print(invalids);
  141. //  Serial.println(F(" Invalid"));
  142. //Serial.println(F("------"));
  143.  
  144.  
  145.  
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top