#include "Wire.h"
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <esp_wifi.h>
#include <esp_system.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//LIS3DH
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
// Adjust this number for the sensitivity of the 'click' force
// this strongly depend on the range! for 16G, try 5-10
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
#define CLICKTHRESHHOLD 40
byte interruptPin=3;
byte interruptPin1=4;
byte interruptPin2=5;
volatile bool lis3dh = false;
int i=0;
#define I2C_SDA 6
#define I2C_SCL 7
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("");
Serial.println("Start");
Serial.println("CPU Frequenz: " + getCpuFrequencyMhz());
//delay(100);
//print_GPIO_wake_up();
Wire.begin(I2C_SDA, I2C_SCL);
pinMode(interruptPin,INPUT);
pinMode(interruptPin1,INPUT);
pinMode(interruptPin2,INPUT);
startLIS3DH();
sensor_interrupts();
//LEDblink(150,3);
// put your setup code here, to run once:
}
void startLIS3DH()
{
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldn't find LIS3DH");
}
Serial.println("LIS3DH found!");
Serial.println(lis.getDeviceID());
lis3dh = true;
lis.setDataRate(LIS3DH_DATARATE_50_HZ);
lis.setRange(LIS3DH_RANGE_8_G); // 2, 4, 8 or 16 G!
//lis.writeRegister8(LIS3DH_CLICKCFG, 0x15);
//lis.writeRegister8(LIS3DH_CLICKTHS, 10);
lis.setClick(1,10,10,20,250);
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
Serial.print("Klickwerte: "); Serial.println(lis.getClick());
// 0 = turn off click detection & interrupt
// 1 = single click only interrupt output
// 2 = double cick only interrupt output, detect single click
// Adjust threshhold, higher numbers are less sensitive
//lis.setClick(1, CLICKTHRESHHOLD);
delay(100);
}
void lis3dhread()
{
lis.read(); // get X Y and Z data at once
// Then print out the raw data
Serial.print("X: "); Serial.print(lis.x);
Serial.print(" \tY: "); Serial.print(lis.y);
Serial.print(" \tZ: "); Serial.print(lis.z);
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lis.getEvent(&event);
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
Serial.print(" \tY: "); Serial.print(event.acceleration.y);
Serial.print(" \tZ: "); Serial.print(event.acceleration.z);
Serial.println(" m/s^2 ");
}
void IRAM_ATTR lisInterruptHandler() {
// Diese Funktion wird aufgerufen, wenn ein Interrupt-Signal vom LIS3DH kommt.
Serial.println("LIS3DTH Interrupt signal received");
LEDblink(100,10);
Serial.println(lis.readAndClearInterrupt());
// Lies alle Register um das Interrupt Signal zurückzusetzen,
// sonst wird es kontinuierlich feuern.
lis.read();
}
void sensor_interrupts()
{
if(lis3dh==true)
{
Serial.println("Interrupts für LIS3DH werden gesetzt");
attachInterrupt(digitalPinToInterrupt(interruptPin), lisInterruptHandler, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptPin2), lisInterruptHandler, CHANGE);
//esp_sleep_enable_ext0_wakeup(gpio_num_t(interruptPin), 1);
}
//attachInterrupt(digitalPinToInterrupt(interruptPin1), lisInterruptHandler, RISING);
}
void i2c_scan()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
}
void loop()
{
}