-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAK1904_Accelerate_LIS3DH.ino
79 lines (69 loc) · 1.64 KB
/
RAK1904_Accelerate_LIS3DH.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
@file RAK1904_Accelerate_LIS3DH.ino
@author rakwireless.com
@brief Setup and read values from a lis3dh sensor
@version 0.1
@date 2020-12-28
@copyright Copyright (c) 2020
**/
#include <Arduino.h>
#include "SparkFunLIS3DH.h" //http://librarymanager/All#SparkFun-LIS3DH
#include <Wire.h>
#define CONVERT_G_TO_MS2 9.80665f
#define FREQUENCY_HZ 50
#define INTERVAL_MS (1000 / (FREQUENCY_HZ + 1))
LIS3DH SensorTwo(I2C_MODE, 0x18);
void lis3dh_get()
{
// read the sensor value
float x, y, z;
x = SensorTwo.readFloatAccelX() * CONVERT_G_TO_MS2;
y = SensorTwo.readFloatAccelY() * CONVERT_G_TO_MS2;
z = SensorTwo.readFloatAccelZ() * CONVERT_G_TO_MS2;
Serial.print(x);
Serial.print(',');
Serial.print(y);
Serial.print(',');
Serial.println(z);
}
void setup()
{
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
if (SensorTwo.begin() != 0)
{
Serial.println("Problem starting the sensor at 0x18.");
}
else
{
Serial.println("Sensor at 0x18 started.");
// Set low power mode
uint8_t data_to_write = 0;
SensorTwo.readRegister(&data_to_write, LIS3DH_CTRL_REG1);
data_to_write |= 0x08;
SensorTwo.writeRegister(LIS3DH_CTRL_REG1, data_to_write);
delay(100);
data_to_write = 0;
SensorTwo.readRegister(&data_to_write, 0x1E);
data_to_write |= 0x90;
SensorTwo.writeRegister(0x1E, data_to_write);
delay(100);
}
Serial.println("enter !");
}
void loop()
{
lis3dh_get();
delay(INTERVAL_MS);
}