Skip to content

Commit

Permalink
Adding a few basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MaffooClock committed Nov 8, 2023
1 parent d8e051b commit ab560a4
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
72 changes: 72 additions & 0 deletions examples/Basic-DigitalTrigger/Basic-DigitalTrigger.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* DFR_Radar: Basic-DigitalTrigger.ino
*
* This is a basic example of how to instantiate a DFR_Radar object using
* the second UART available on most Arduino boards. This example is almost
* identical to the Basic.ino, except this one uses a digital input to check
* for presence triggering instead of querying over serial -- much faster!
*
* The detection area and sensitivity are set quite low to make it easier
* to test the unit right in front of you (too high and it'll just stay
* triggered, and that's no fun!).
*
* When motion is detected, it will turn on the built-in LED.
*
* Created 8 November 2023
* By Matthew Clark
*/

#include <DFR_Radar.h>

// Serial1 is the hardware UART pins
DFR_Radar sensor( &Serial1 );

// IO2 from sensor is connected to pin 3 on the Arduino
const int TRIGGER_INPUT = 3;

void setup()
{
Serial.begin( 9600 );

// The DFRobot device is factory-set for 115200 baud
Serial1.begin( 115200 );

// Setup the built-in LED
pinMode( LED_BUILTIN, OUTPUT );

// Configure the digital input used to detect presence triggers
pinMode( TRIGGER_INPUT, INPUT );

// Restore to the factory settings -- it's not necessary to do this unless needed
sensor.factoryReset();

// Set a detection range of 0 to 1 meter (19.05m is the maximum)
sensor.setDetectionArea( 0, 1 );

// Lower the sensitivity so that it's easier to test
sensor.setSensitivity( 2 );

// This will cause the LED to turn on 1 second after presence is detected and
// it will stay on for 5 seconds after the sensor no longer detects presence.
// Set both of these to 0 for instant on/off, although you may get short-cycling.
sensor.setOutputLatency( 1, 5 );
}

void loop()
{
/**
* NOTE: if you use any `delay()` here, the
* more of them plus the longer they are,
* the more likely the presence events will
* be missed.
*/

// Query the presence detection status
bool presence = digitalRead( TRIGGER_INPUT );

// If presence == true, turn on the built-in LED.
digitalWrite( LED_BUILTIN, presence );

// Your serial monitor will show constant 1's or 0's depending
Serial.println( presence );
}
76 changes: 76 additions & 0 deletions examples/Basic-SoftwareSerial/Basic-SoftwareSerial.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* DFR_Radar: Basic-SoftwareSerial.ino
*
* This is a basic example of how to instantiate a DFR_Radar object using
* Software Serial.
*
* The detection area and sensitivity are set quite low to make it easier
* to test the unit right in front of you (too high and it'll just stay
* triggered, and that's no fun!).
*
* When motion is detected, it will turn on the built-in LED.
*
* Created 8 November 2023
* By Matthew Clark
*/

#include <DFR_Radar.h>
#include <SoftwareSerial.h>

// Create a software serial port
// First argument is receive, second it transmit
SoftwareSerial sensorSerial( 3, 2 );

// Create a DFR_Radar instance
DFR_Radar sensor( &sensorSerial );

void setup()
{
Serial.begin( 9600 );

// The DFRobot device is factory-set for 115200 baud
sensorSerial.begin( 115200 );

// Restore to the factory settings -- it's not necessary to do this unless needed
sensor.factoryReset();

// Set a detection range of 0 to 1 meter (19.05m is the maximum)
sensor.setDetectionArea( 0, 1 );

// Lower the sensitivity so that it's easier to test
sensor.setSensitivity( 2 );

// This will cause the LED to turn on 1 second after presence is detected and
// it will stay on for 5 seconds after the sensor no longer detects presence.
// Set both of these to 0 for instant on/off, although you may get short-cycling.
sensor.setOutputLatency( 1, 5 );

// The default timeout is 1 second, which could cause
// the `loop()` to be too slow. 100ms seems to work.
sensorSerial.setTimeout( 100 );

// Setup the built-in LED
pinMode( LED_BUILTIN, OUTPUT );
}

void loop()
{
/**
* NOTE: if you use any `delay()` here, the
* more of them plus the longer they are,
* the more likely the presence events will
* be missed.
*/

// Query the presence detection status -- this is
// kind of slow since it communicates via serial.
// This can slow your loop down up to 100ms per
// iteration or more.
bool presence = sensor.checkPresence();

// If presence == true, turn on the built-in LED.
digitalWrite( LED_BUILTIN, presence );

// Your serial monitor will show constant 1's or 0's depending
Serial.println( presence );
}
71 changes: 71 additions & 0 deletions examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* DFR_Radar: Basic.ino
*
* This is a basic example of how to instantiate a DFR_Radar object using
* the second UART available on most Arduino boards.
*
* The detection area and sensitivity are set quite low to make it easier
* to test the unit right in front of you (too high and it'll just stay
* triggered, and that's no fun!).
*
* When motion is detected, it will turn on the built-in LED.
*
* Created 8 November 2023
* By Matthew Clark
*/

#include <DFR_Radar.h>

// Serial1 is the hardware UART pins
DFR_Radar sensor( &Serial1 );

void setup()
{
Serial.begin( 9600 );

// The DFRobot device is factory-set for 115200 baud
Serial1.begin( 115200 );

// Restore to the factory settings -- it's not necessary to do this unless needed
sensor.factoryReset();

// Set a detection range of 0 to 1 meter (19.05m is the maximum)
sensor.setDetectionArea( 0, 1 );

// Lower the sensitivity so that it's easier to test
sensor.setSensitivity( 2 );

// This will cause the LED to turn on 1 second after presence is detected and
// it will stay on for 5 seconds after the sensor no longer detects presence.
// Set both of these to 0 for instant on/off, although you may get short-cycling.
sensor.setOutputLatency( 1, 5 );

// The default timeout is 1 second, which could cause
// the `loop()` to be too slow. 100ms seems to work.
Serial1.setTimeout( 100 );

// Setup the built-in LED
pinMode( LED_BUILTIN, OUTPUT );
}

void loop()
{
/**
* NOTE: if you use any `delay()` here, the
* more of them plus the longer they are,
* the more likely the presence events will
* be missed.
*/

// Query the presence detection status -- this is
// kind of slow since it communicates via serial.
// This can slow your loop down up to 100ms per
// iteration or more.
bool presence = sensor.checkPresence();

// If presence == true, turn on the built-in LED.
digitalWrite( LED_BUILTIN, presence );

// Your serial monitor will show constant 1's or 0's depending
Serial.println( presence );
}
15 changes: 15 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
},
"headers": "DFR_Radar.h",
"examples": [
{
"name": "Basic Example",
"base": "examples/Basic",
"files": [ "Basic.ino" ]
},
{
"name": "Basic Example with Software Serial",
"base": "examples/Basic-SoftwareSerial",
"files": [ "Basic-SoftwareSerial.ino" ]
},
{
"name": "Basic Example with Digital Input",
"base": "examples/Basic-DigitalTrigger",
"files": [ "Basic-DigitalTrigger.ino" ]
}
],
"frameworks": "arduino",
"platforms": "*",
Expand Down

0 comments on commit ab560a4

Please sign in to comment.