Devices & Components
Arduino® UNO R4 WiFi
MAX4466 microphone
Software & Tools
NanoEdge AI Studio
Arduino IDE
Project description
Code
Main
c
Final code
1/* If you want to use NEAI functions please, include NEAI library 2 in your Arduino libraries then, uncomment NEAI parts in the following code 3*/ 4 5/* Libraries part */ 6#include "Wire.h" 7#include <LIS2DW12Sensor.h> 8 9/* Macros definitions */ 10#define SERIAL_BAUD_RATE 115200 11 12/* Sensor data rates. 13 You can choose from the following values for both accel & gyro: 14 12.5f, 25.0f, 50.0f, 100.0f, 200.0f, 400.0f, 800.0f & 1600.0f. 15*/ 16#define SENSOR_DATA_RATE 1600.0f 17 18/* Sensor ranges. 19 You can choose from: 20 2, 4, 8 & 16. 21*/ 22#define SENSOR_RANGE 4 23 24 25#define SENSOR_SAMPLES 1024 26#define AXIS 3 27 28/* Sensor object declaration using I2C */ 29LIS2DW12Sensor Accelero(&Wire); 30 31/* Global variables definitions */ 32static uint8_t drdy = 0; 33static uint16_t neai_ptr = 0; 34static int32_t accelerometer[3]; 35static float neai_buffer[SENSOR_SAMPLES * AXIS] = {0.0}; 36 37 38/* Initialization function: In this function, 39 code runs only once at boot / reset. 40*/ 41void setup() { 42 /* Init serial at baud rate 115200 */ 43 Serial.begin(SERIAL_BAUD_RATE); 44 45 /* I2C workaround: Sometimes, on some boards, 46 I2C get stuck after software reboot, reset so, 47 to avoid this, we toggle I2C clock pin at boot. 48 */ 49 pinMode(SCL, OUTPUT); 50 for (uint8_t i = 0; i < 20; i++) { 51 digitalWrite(SCL, !digitalRead(SCL)); 52 delay(1); 53 } 54 delay(100); 55 56 Wire.begin(); 57 Accelero.begin(); 58 Accelero.Enable_X(); 59 Accelero.Set_X_ODR(SENSOR_DATA_RATE); 60 Accelero.Set_X_FS(SENSOR_RANGE); 61} 62 63/* Main function: Code run indefinitely */ 64void loop() { 65 /* Get data in the neai buffer */ 66 while (neai_ptr < SENSOR_SAMPLES) { 67 /* Check if new data if available */ 68 Accelero.ReadReg(LIS2DW12_STATUS, &drdy); 69 if (drdy & 0x01) { 70 /* If new data is available we read it ! */ 71 Accelero.Get_X_Axes(accelerometer); 72 /* Fill neai buffer with new accel data */ 73 neai_buffer[AXIS * neai_ptr] = (float) accelerometer[0]; 74 neai_buffer[(AXIS * neai_ptr) + 1] = (float) accelerometer[1]; 75 neai_buffer[(AXIS * neai_ptr) + 2] = (float) accelerometer[2]; 76 /* Increment neai pointer */ 77 neai_ptr++; 78 } 79 } 80 /* Reset pointer */ 81 neai_ptr = 0; 82 83 /* Print the whole buffer to the serial */ 84 for (uint16_t i = 0; i < AXIS * SENSOR_SAMPLES; i++) { 85 Serial.print((String)neai_buffer[i] + " "); 86 } 87 Serial.print("\n"); 88 // } 89 90 /* Clean neai buffer */ 91 memset(neai_buffer, 0.0, AXIS * SENSOR_SAMPLES * sizeof(float)); 92}
Downloadable files
Main
download code
arduino_cat_flap.ino
Comments
Only logged in users can leave comments