acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

SX128x_Ranging.ino (2018B)

      1 /*
      2    RadioLib SX128x Ranging Example
      3 
      4    This example performs ranging exchange between two
      5    SX1280 LoRa radio modules. Ranging allows to measure
      6    distance between the modules using time-of-flight
      7    measurement.
      8 
      9    Only SX1280 and SX1282 support ranging!
     10 
     11    For default module settings, see the wiki page
     12    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx128x---lora-modem
     13 
     14    For full API reference, see the GitHub Pages
     15    https://jgromes.github.io/RadioLib/
     16 */
     17 
     18 // include the library
     19 #include <RadioLib.h>
     20 
     21 // SX1280 has the following connections:
     22 // NSS pin:   10
     23 // DIO1 pin:  2
     24 // NRST pin:  3
     25 // BUSY pin:  9
     26 SX1280 radio = new Module(10, 2, 3, 9);
     27 
     28 // or using RadioShield
     29 // https://github.com/jgromes/RadioShield
     30 //SX1280 radio = RadioShield.ModuleA;
     31 
     32 void setup() {
     33   Serial.begin(9600);
     34 
     35   // initialize SX1280 with default settings
     36   Serial.print(F("[SX1280] Initializing ... "));
     37   int state = radio.begin();
     38   if (state == RADIOLIB_ERR_NONE) {
     39     Serial.println(F("success!"));
     40   } else {
     41     Serial.print(F("failed, code "));
     42     Serial.println(state);
     43     while (true);
     44   }
     45 }
     46 
     47 void loop() {
     48   Serial.print(F("[SX1280] Ranging ... "));
     49 
     50   // start ranging exchange
     51   // range as master:             true
     52   // slave address:               0x12345678
     53   int state = radio.range(true, 0x12345678);
     54 
     55   // the other module must be configured as slave with the same address
     56   /*
     57     int state = radio.range(false, 0x12345678);
     58   */
     59 
     60   if (state == RADIOLIB_ERR_NONE) {
     61     // ranging finished successfully
     62     Serial.println(F("success!"));
     63     Serial.print(F("[SX1280] Distance:\t\t\t"));
     64     Serial.print(radio.getRangingResult());
     65     Serial.println(F(" meters"));
     66 
     67   } else if (state == RADIOLIB_ERR_RANGING_TIMEOUT) {
     68     // timed out waiting for ranging packet
     69     Serial.println(F("timed out!"));
     70 
     71   } else {
     72     // some other error occurred
     73     Serial.print(F("failed, code "));
     74     Serial.println(state);
     75 
     76   }
     77 
     78   // wait for a second before ranging again
     79   delay(1000);
     80 }