arduino-LoRa icon indicating copy to clipboard operation
arduino-LoRa copied to clipboard

SX1276 CAD

Open Mathannbala opened this issue 1 year ago • 1 comments

Can i check if the CAD callback example is working? I changed the pins since I'm using a TTGO T-beam board and i believe i do not need to make any other changes to test this CAD but i do not see anything happening.. Hope someone can assist me on this.

Mathannbala avatar Mar 28 '24 08:03 Mathannbala

I've been using the following code for CAD. It is derived from the example.

// An approach to CAD.
// Using Sandeep's baseline LoRa library:
// https://github.com/sandeepmistry/arduino-LoRa
#include <LoRa.h>
#ifdef ARDUINO_SAMD_MKRWAN1300
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
#endif

// Flags signal detection.
// OnCadDone returns false when no signal is detected.
// Returns true if a signal is detected.
uint8_t signalDetectionFlag = 02;

void setup()
{
  Serial.begin(9600);
  while (!Serial) Wait(1000);

  Serial.println("LoRa CAD Test");

  if (!LoRa.begin(915E6))
  {
    Serial.println("Starting LoRa failed!");
    Wait(1000);
    exit(1);
  }
  else Serial.println("LoRa transceiver initialized");

  // register the channel activity detection callback
  LoRa.onCadDone(onCadDone);

  // Ready
  Serial.println("Ready");
}

void loop()
{
  // try next activity detection
  signalDetectionFlag = 01;         // CAD not completed
  while(signalDetectionFlag)
  {
    signalDetectionFlag = 02;
    LoRa.channelActivityDetection();
    while(signalDetectionFlag == 02) Wait(100);
    if(signalDetectionFlag)         // signal detected
    {
      Serial.println(" detection confirmed\n");
    }
    else if( ! signalDetectionFlag) // no signal detected
    {
      Serial.println("  could send something\n");
    }
  }
  //Serial.println("This cycle completed");
  Wait(1000);
}

void onCadDone(boolean signalDetected)
{
  signalDetectionFlag = (uint8_t)signalDetected;
  if (signalDetected)
  {
    Serial.print("*** Signal Detected: "); Serial.println(signalDetected);
  }
  else
  {
    Serial.print("*** No Signal Detected: "); Serial.println(signalDetected);
  }
}

// Wait for a specific number of milliseconds.
// delay() is blocking so we do not use that.
// This approach does not use hardware-specific timers.
void Wait(long milliseconds)
{
  long beginTime = millis();
  uint8_t doSomething = 00;
  while ((millis() - beginTime) <= milliseconds) doSomething++;
}

SoothingMist avatar Apr 03 '25 20:04 SoothingMist