first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
/** The input pins to enable the interrupt on, connected to INT1 and INT2 on the ADXL. */
#define INPUT_PIN_INT1 (5) // Uno = (2)
#define INPUT_PIN_INT2 (6) // Uno = (3)
/**
* This struct is used to count the number of times that specific interrutps
* have been fired by the ADXL and detected on the MCU. They will increment
* by one for each event associated with the specified interrupt 'bit'.
*/
struct adxl_int_stats {
uint32_t data_ready;
uint32_t single_tap;
uint32_t double_tap;
uint32_t activity;
uint32_t inactivity;
uint32_t freefall;
uint32_t watermark;
uint32_t overrun;
uint32_t total;
};
/** Global stats block, incremented inside the interrupt handler(s). */
struct adxl_int_stats g_int_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/** Global counter to track the numbers of unused interrupts fired. */
uint32_t g_ints_fired = 0;
/** Global variable to determine which interrupt(s) are enabled on the ADXL343. */
int_config g_int_config_enabled = { 0 };
/** Global variables to determine which INT pin interrupt(s) are mapped to on the ADXL343. */
int_config g_int_config_map = { 0 };
/** Interrupt service routine for INT1 events. */
void int1_isr(void)
{
/* By default, this sketch routes the OVERRUN interrupt to INT1. */
g_int_stats.overrun++;
g_int_stats.total++;
g_ints_fired++;
/* TODO: Toggle an LED! */
}
/** Interrupt service routine for INT2 events. */
void int2_isr(void)
{
/* By default, this sketch routes the DATA_READY interrupt to INT2. */
g_int_stats.data_ready++;
g_int_stats.total++;
g_ints_fired++;
/* TODO: Toggle an LED! */
}
/** Configures the HW interrupts on the ADXL343 and the target MCU. */
void config_interrupts(void)
{
/* NOTE: Once an interrupt fires on the ADXL you can read a register
* to know the source of the interrupt, but since this would likely
* happen in the 'interrupt context' performing an I2C read is a bad
* idea since it will block the device from handling other interrupts
* in a timely manner.
*
* The best approach is to try to make use of only two interrupts on
* two different interrupt pins, so that when an interrupt fires, based
* on the 'isr' function that is called, you already know the int source.
*/
/* Attach interrupt inputs on the MCU. */
pinMode(LED_BUILTIN, OUTPUT);
pinMode(INPUT_PIN_INT1, INPUT);
pinMode(INPUT_PIN_INT2, INPUT);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT2), int2_isr, RISING);
/* Enable interrupts on the accelerometer. */
g_int_config_enabled.bits.overrun = true; /* Set the INT1 */
g_int_config_enabled.bits.watermark = false;
g_int_config_enabled.bits.freefall = false;
g_int_config_enabled.bits.inactivity = false;
g_int_config_enabled.bits.activity = false;
g_int_config_enabled.bits.double_tap = false;
g_int_config_enabled.bits.single_tap = false;
g_int_config_enabled.bits.data_ready = true; /* Set to INT2 */
accel.enableInterrupts(g_int_config_enabled);
/* Map specific interrupts to one of the two INT pins. */
g_int_config_map.bits.overrun = ADXL343_INT1;
g_int_config_map.bits.watermark = ADXL343_INT1;
g_int_config_map.bits.freefall = ADXL343_INT1;
g_int_config_map.bits.inactivity = ADXL343_INT1;
g_int_config_map.bits.activity = ADXL343_INT1;
g_int_config_map.bits.double_tap = ADXL343_INT1;
g_int_config_map.bits.single_tap = ADXL343_INT1;
g_int_config_map.bits.data_ready = ADXL343_INT2;
accel.mapInterrupts(g_int_config_map);
}
void setup(void)
{
Serial.begin(9600);
while (!Serial);
Serial.println("ADXL343 Interrupt Tester"); Serial.println("");
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL343_RANGE_16_G);
// displaySetRange(ADXL343_RANGE_8_G);
// displaySetRange(ADXL343_RANGE_4_G);
// displaySetRange(ADXL343_RANGE_2_G);
/* Configure the HW interrupts. */
config_interrupts();
Serial.println("ADXL343 init complete. Waiting for INT activity.");
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
delay(10);
while (g_ints_fired) {
Serial.println("INT detected!");
Serial.print("\tOVERRUN Count: "); Serial.println(g_int_stats.overrun, DEC);
Serial.print("\tDATA_READY Count: "); Serial.println(g_int_stats.data_ready, DEC);
/* Decrement the unhandled int counter. */
g_ints_fired--;
}
}

View File

@@ -0,0 +1,79 @@
/*
* Connect two identical ADXL_343 sensor breakout boards
* to a single Arduino. By connecting SDO on one of the
* sensors to Vcc the I2C address of this sensor changes
* from the default (0x53) to the alternative address (0x1D).
* The address is passed during begin().
*
* This example returns raw sensor output of x, y and z acceleration
* for both sensors over serial at 115200 baud.
*
* Example by Rolf Hut based on sensorTest example
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
/* Create two sensor objects and assign unique IDs */
Adafruit_ADXL343 accel1 = Adafruit_ADXL343(12345);
Adafruit_ADXL343 accel2 = Adafruit_ADXL343(12346);
void setup(void)
{
Serial.begin(115200);
while (!Serial);
Serial.println("Two Accelerometers on one Arduino"); Serial.println("");
/* Initialise the first sensors, this uses the default address */
if(!accel1.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 nr1 detected ... Check your wiring!");
while(1);
}
/* Initialise the second sensors, this uses the alternative address */
/* of 0x1D. On this sensor SDO must be connected to VCC */
if(!accel2.begin(0x1D))
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 nr2 detected ... Check your wiring!");
while(1);
}
/* Set the range and data rate to whatever is appropriate for your project */
/* See the sensortest example for more details */
accel1.setRange(ADXL343_RANGE_2_G);
accel2.setRange(ADXL343_RANGE_2_G);
accel1.setDataRate(ADXL343_DATARATE_1600_HZ);
accel2.setDataRate(ADXL343_DATARATE_1600_HZ);
/* Display some basic information on these sensors */
accel1.printSensorDetails();
accel2.printSensorDetails();
Serial.println("");
}
void loop(void)
{
/* Get new sensor events */
sensors_event_t event1;
sensors_event_t event2;
accel1.getEvent(&event1);
accel2.getEvent(&event2);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print(event1.acceleration.x); Serial.print(",");
Serial.print(event1.acceleration.y); Serial.print(",");
Serial.print(event1.acceleration.z); Serial.print(",");
Serial.print(event2.acceleration.x); Serial.print(",");
Serial.print(event2.acceleration.y); Serial.print(",");
Serial.println(event2.acceleration.z);
}

View File

@@ -0,0 +1,93 @@
/* This example shows how to use the trimmer offset registers to account for any error in the sensor
and 'zero' out the flat reading to be 0, 0, 1g. note this is unique to each sensor so it'll have
to be repeated and 'saved' for each ADXL you use!
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
#define ADXL343_SCK 13
#define ADXL343_MISO 12
#define ADXL343_MOSI 11
#define ADXL343_CS 10
/* Assign a unique ID to this sensor at the same time */
/* Uncomment following line for default Wire bus */
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
/* NeoTrellis M4, etc. */
/* Uncomment following line for Wire1 bus */
//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);
/* Uncomment for SPI */
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_SCK, ADXL343_MISO, ADXL343_MOSI, ADXL343_CS, 12345);
void setup(void)
{
Serial.begin(115200);
while (!Serial);
Serial.println("Offsets Test"); Serial.println("");
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL343_RANGE_16_G);
/* Display some basic information on this sensor */
accel.printSensorDetails();
Serial.println("");
// init offsets to zero
accel.setTrimOffsets(0, 0, 0);
Serial.println("Hold accelerometer flat to set offsets to 0, 0, and -1g...");
delay(1000);
int16_t x, y, z;
x = accel.getX();
y = accel.getY();
z = accel.getZ();
Serial.print("Raw X: "); Serial.print(x); Serial.print(" ");
Serial.print("Y: "); Serial.print(y); Serial.print(" ");
Serial.print("Z: "); Serial.print(z); Serial.print(" ");Serial.println(" counts");
// the trim offsets are in 'multiples' of 8, we want to round, so we add 4
accel.setTrimOffsets(-(x+4)/8,
-(y+4)/8,
-(z-250+4)/8); // Z should be '250' at 1g (4mg per bit)
int8_t x_offset, y_offset, z_offset;
accel.getTrimOffsets(&x_offset, &y_offset, &z_offset);
Serial.print("Current trim offsets: ");
Serial.print(x_offset); Serial.print(", ");
Serial.print(y_offset); Serial.print(", ");
Serial.println(z_offset);
Serial.println();
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");
Serial.println("m/s^2 ");
Serial.print("Raw X: "); Serial.print(accel.getX()); Serial.print(" ");
Serial.print("Y: "); Serial.print(accel.getY()); Serial.print(" ");
Serial.print("Z: "); Serial.print(accel.getZ()); Serial.print(" ");
Serial.println(" counts");
Serial.println();
delay(500);
}

View File

@@ -0,0 +1,148 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
#define ADXL343_SCK 13
#define ADXL343_MISO 12
#define ADXL343_MOSI 11
#define ADXL343_CS 10
/* Assign a unique ID to this sensor at the same time */
/* Uncomment following line for default Wire bus */
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
/* NeoTrellis M4, etc. */
/* Uncomment following line for Wire1 bus */
//Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &Wire1);
/* Uncomment for software SPI */
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_SCK, ADXL343_MISO, ADXL343_MOSI, ADXL343_CS, 12345);
/* Uncomment for hardware SPI */
//Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_CS, &SPI, 12345);
void displayDataRate(void)
{
Serial.print ("Data Rate: ");
switch(accel.getDataRate())
{
case ADXL343_DATARATE_3200_HZ:
Serial.print ("3200 ");
break;
case ADXL343_DATARATE_1600_HZ:
Serial.print ("1600 ");
break;
case ADXL343_DATARATE_800_HZ:
Serial.print ("800 ");
break;
case ADXL343_DATARATE_400_HZ:
Serial.print ("400 ");
break;
case ADXL343_DATARATE_200_HZ:
Serial.print ("200 ");
break;
case ADXL343_DATARATE_100_HZ:
Serial.print ("100 ");
break;
case ADXL343_DATARATE_50_HZ:
Serial.print ("50 ");
break;
case ADXL343_DATARATE_25_HZ:
Serial.print ("25 ");
break;
case ADXL343_DATARATE_12_5_HZ:
Serial.print ("12.5 ");
break;
case ADXL343_DATARATE_6_25HZ:
Serial.print ("6.25 ");
break;
case ADXL343_DATARATE_3_13_HZ:
Serial.print ("3.13 ");
break;
case ADXL343_DATARATE_1_56_HZ:
Serial.print ("1.56 ");
break;
case ADXL343_DATARATE_0_78_HZ:
Serial.print ("0.78 ");
break;
case ADXL343_DATARATE_0_39_HZ:
Serial.print ("0.39 ");
break;
case ADXL343_DATARATE_0_20_HZ:
Serial.print ("0.20 ");
break;
case ADXL343_DATARATE_0_10_HZ:
Serial.print ("0.10 ");
break;
default:
Serial.print ("???? ");
break;
}
Serial.println(" Hz");
}
void displayRange(void)
{
Serial.print ("Range: +/- ");
switch(accel.getRange())
{
case ADXL343_RANGE_16_G:
Serial.print ("16 ");
break;
case ADXL343_RANGE_8_G:
Serial.print ("8 ");
break;
case ADXL343_RANGE_4_G:
Serial.print ("4 ");
break;
case ADXL343_RANGE_2_G:
Serial.print ("2 ");
break;
default:
Serial.print ("?? ");
break;
}
Serial.println(" g");
}
void setup(void)
{
Serial.begin(115200);
while (!Serial);
Serial.println("Accelerometer Test"); Serial.println("");
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL343_RANGE_16_G);
// accel.setRange(ADXL343_RANGE_8_G);
// accel.setRange(ADXL343_RANGE_4_G);
// accel.setRange(ADXL343_RANGE_2_G);
/* Display some basic information on this sensor */
accel.printSensorDetails();
displayDataRate();
displayRange();
Serial.println("");
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 ");
delay(500);
}

View File

@@ -0,0 +1,71 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
/** The input pin to enable the interrupt on, connected to INT1 on the ADXL. */
#define INPUT_PIN_INT1 (5) // SAMD21/SAMD51 = 5 for interrupt pin
uint32_t g_tap_count = 0;
int_config g_int_config_enabled = { 0 };
int_config g_int_config_map = { 0 };
/** Interrupt service routine for INT1 events. This will be called when a single tap is detected. */
void int1_isr(void)
{
g_tap_count++;
}
void setup(void)
{
Serial.begin(9600);
while (!Serial);
Serial.println("ADXL343 Single Tap INT Tester"); Serial.println("");
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL343 ... check your connections */
Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL343_RANGE_16_G);
/* Configure the HW interrupts. */
pinMode(INPUT_PIN_INT1, INPUT);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING);
/* Enable single tap interrupts on the accelerometer. */
g_int_config_enabled.bits.single_tap = true;
accel.enableInterrupts(g_int_config_enabled);
/* Map single tap interrupts to INT1 pin. */
g_int_config_map.bits.single_tap = ADXL343_INT1;
accel.mapInterrupts(g_int_config_map);
/* Reset tap counter. */
g_tap_count = 0;
Serial.println("ADXL343 init complete. Waiting for single tap INT activity.");
}
void loop(void)
{
/* Get a new sensor event */
/* Reading data clears the interrupts. */
sensors_event_t event;
accel.getEvent(&event);
delay(10);
while (g_tap_count) {
Serial.println("Single tap detected!");
/* Clear the interrupt as a side-effect of reading the interrupt source register.. */
accel.checkInterrupts();
/* Decrement the local interrupt counter. */
g_tap_count--;
}
}