EasyManua.ls Logo

sparkfun Qwiic - Page 8

sparkfun Qwiic
10 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
#include <Wire.h>
#include "SparkFun_AK975X_Arduino_Library.h" //Use Library Manager or download here: https://github.com/sparkfun/SparkFun_AK975X
_Arduino_Library
AK975X movementSensor; //Hook object to the library
unsigned int upValue; // current proximity reading
unsigned int averageValue; // low-pass filtered proximity reading
signed int fa2; // FA-II value;
signed int fa2Derivative; // Derivative of the FA-II value;
signed int fa2DerivativeLast; // Last value of the derivative (for zero-crossing detection)
signed int sensitivity = 50; // Sensitivity of touch/release detection, values closer to zero increase sensitivity
#define LOOP_TIME 30 // Loop duration in ms. 30ms works well.
//Exponential average weight parameter / cut-off frequency for high-pass filter
//#define EA 0.3 //Very steep
//#define EA 0.1 //Less steep
#define EA 0.05 //Less steep
void setup()
{
Serial.begin(9600);
Wire.begin();
//Turn on sensor
if (movementSensor.begin() == false)
{
Serial.println("Device not found. Check wiring.");
while (1);
}
upValue = movementSensor.getIR3(); //Get one of the latest IR values
averageValue = upValue;
fa2 = 0;
movementSensor.refresh(); //Read dummy register after new data is read
}
void loop()
{
unsigned long startTime = millis();
while (movementSensor.available() == false) delay(1); //Wait for new data
upValue = movementSensor.getIR3(); //Get one of the latest IR values
movementSensor.refresh(); //Read dummy register after new data is read
fa2DerivativeLast = fa2Derivative;
fa2Derivative = (signed int) averageValue - upValue - fa2;
fa2 = (signed int) averageValue - upValue;
//Turn on various variables to see how they respond on the graph
//Serial.print(upValue);
//Serial.print(",");
//Serial.print(fa2);
//Serial.print(",");
Serial.print(fa2Derivative);
Serial.print(",");
//Look to see if the previous fa2Der was below threshold AND current fa2Der is above threshold
//Basically, if the sign of the fa2Ders has switched since last reading then we have an event
if ((fa2DerivativeLast < -sensitivity && fa2Derivative > sensitivity) || (fa2DerivativeLast > sensitivity && fa2Derivative < -
sensitivity)) // zero crossing detected
{
if (fa2 < -sensitivity) // minimum
{
Serial.print(-1000); //Cause red line to indicate entering presence
Serial.print(",");

Related product manuals