Vibration window measurement
This commit is contained in:
parent
5bf3870869
commit
ee7df4f6e3
@ -3,8 +3,8 @@
|
|||||||
// Feature Configuration - Comment out to disable
|
// Feature Configuration - Comment out to disable
|
||||||
#define ENABLE_SENSORS
|
#define ENABLE_SENSORS
|
||||||
#define ENABLE_DISPLAY
|
#define ENABLE_DISPLAY
|
||||||
#define ENABLE_WIFI
|
//#define ENABLE_WIFI
|
||||||
#define ENABLE_NOTIFICATIONS
|
//#define ENABLE_NOTIFICATIONS
|
||||||
|
|
||||||
#ifdef ENABLE_WIFI
|
#ifdef ENABLE_WIFI
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
@ -47,9 +47,11 @@ BH1750 lightMeter;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Thresholds
|
// Thresholds
|
||||||
const float LIGHT_ACTIVATION_THRESHOLD = 200.0;
|
const float LIGHT_ACTIVATION_THRESHOLD = 100.0;
|
||||||
const float LIGHT_DEACTIVATION_THRESHOLD = 10.0;
|
const float LIGHT_DEACTIVATION_THRESHOLD = 20.0;
|
||||||
const unsigned long VIBRATION_STATE_CHANGE_INTERVAL = 5000; // 5 seconds
|
const unsigned long VIBRATION_STATE_CHANGE_INTERVAL = 5000; // 5 seconds
|
||||||
|
const unsigned long VIBRATION_ACTIVE_THRESHOLD = 10000;
|
||||||
|
const int VIBRATION_WINDOW_SIZE = 4; // X * 5s seconds window
|
||||||
|
|
||||||
// Device state
|
// Device state
|
||||||
bool isVibrationActive = false;
|
bool isVibrationActive = false;
|
||||||
@ -57,18 +59,17 @@ bool isLightActive = false;
|
|||||||
bool isDeviceActive = false;
|
bool isDeviceActive = false;
|
||||||
|
|
||||||
// Time tracking for sensor states
|
// Time tracking for sensor states
|
||||||
unsigned long vibrationLowStartTime = 0;
|
unsigned long lastVibrationCheckTime = 0;
|
||||||
unsigned long vibrationHighStartTime = 0;
|
|
||||||
unsigned long lightHighStartTime = 0;
|
unsigned long lightHighStartTime = 0;
|
||||||
unsigned long lightLowStartTime = 0;
|
unsigned long lightLowStartTime = 0;
|
||||||
|
|
||||||
// OLED display data
|
// OLED display data
|
||||||
#ifdef ENABLE_DISPLAY
|
#ifdef ENABLE_DISPLAY
|
||||||
String displayData[4];
|
String displayData[5];
|
||||||
int currentDisplayLine = 0;
|
int currentDisplayLine = 0;
|
||||||
unsigned long lastDisplayScrollTime = 0;
|
unsigned long lastDisplayScrollTime = 0;
|
||||||
const unsigned long DISPLAY_SCROLL_INTERVAL = 2000; // 1000 = 1 second
|
const unsigned long DISPLAY_SCROLL_INTERVAL = 2000; // 1000 = 1 second
|
||||||
const int displayTextLines = 5;
|
const int displayTextLines = 4;
|
||||||
int displayDataLines = displayTextLines;
|
int displayDataLines = displayTextLines;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -77,6 +78,30 @@ String queuedNotification = "";
|
|||||||
String queuedTitle = "";
|
String queuedTitle = "";
|
||||||
String queuedPriority = "";
|
String queuedPriority = "";
|
||||||
|
|
||||||
|
#ifdef ENABLE_SENSORS
|
||||||
|
unsigned long vibrationHistory[VIBRATION_WINDOW_SIZE];
|
||||||
|
int vibrationHistoryIdx = 0;
|
||||||
|
unsigned long vibrationWindowTotal = 0;
|
||||||
|
unsigned long vibrationTotalLow = 0;
|
||||||
|
volatile unsigned long lastChangeTime = 0;
|
||||||
|
volatile unsigned long lowTimeAccumulator = 0;
|
||||||
|
volatile unsigned long highTimeAccumulator = 0;
|
||||||
|
|
||||||
|
void IRAM_ATTR onChange() {
|
||||||
|
unsigned long now = micros();
|
||||||
|
unsigned long duration = now - lastChangeTime;
|
||||||
|
lastChangeTime = now;
|
||||||
|
|
||||||
|
if (digitalRead(VIBRATION_PIN) == HIGH) {
|
||||||
|
// Changed to HIGH, so previous state was LOW
|
||||||
|
lowTimeAccumulator += duration;
|
||||||
|
} else {
|
||||||
|
// Changed to LOW, so previous state was HIGH
|
||||||
|
highTimeAccumulator += duration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("Setup started.");
|
Serial.println("Setup started.");
|
||||||
@ -88,6 +113,11 @@ void setup() {
|
|||||||
#ifdef ENABLE_SENSORS
|
#ifdef ENABLE_SENSORS
|
||||||
// Initialize vibration sensor pin
|
// Initialize vibration sensor pin
|
||||||
pinMode(VIBRATION_PIN, INPUT);
|
pinMode(VIBRATION_PIN, INPUT);
|
||||||
|
lastChangeTime = micros();
|
||||||
|
attachInterrupt(digitalPinToInterrupt(VIBRATION_PIN), onChange, CHANGE);
|
||||||
|
|
||||||
|
// Initialize history buffer
|
||||||
|
for(int i=0; i<VIBRATION_WINDOW_SIZE; i++) vibrationHistory[i] = 0;
|
||||||
|
|
||||||
// Initialize light sensor
|
// Initialize light sensor
|
||||||
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||||
@ -159,35 +189,44 @@ void loop() {
|
|||||||
|
|
||||||
// Sensor readings
|
// Sensor readings
|
||||||
#ifdef ENABLE_SENSORS
|
#ifdef ENABLE_SENSORS
|
||||||
bool currentVibrationState = (digitalRead(VIBRATION_PIN) == LOW);
|
|
||||||
float currentLightLevel = lightMeter.readLightLevel();
|
float currentLightLevel = lightMeter.readLightLevel();
|
||||||
#else
|
#else
|
||||||
bool currentVibrationState = false;
|
|
||||||
float currentLightLevel = 0.0;
|
float currentLightLevel = 0.0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Vibration state change logic
|
// Vibration state change logic
|
||||||
if (currentVibrationState && !isVibrationActive) { // Vibration is active (LOW)
|
#ifdef ENABLE_SENSORS
|
||||||
if (vibrationLowStartTime == 0) {
|
if (currentTime - lastVibrationCheckTime >= VIBRATION_STATE_CHANGE_INTERVAL) {
|
||||||
vibrationLowStartTime = currentTime;
|
lastVibrationCheckTime = currentTime;
|
||||||
|
|
||||||
|
noInterrupts();
|
||||||
|
unsigned long now = micros();
|
||||||
|
unsigned long duration = now - lastChangeTime;
|
||||||
|
lastChangeTime = now;
|
||||||
|
|
||||||
|
if (digitalRead(VIBRATION_PIN) == LOW) {
|
||||||
|
lowTimeAccumulator += duration;
|
||||||
|
} else {
|
||||||
|
highTimeAccumulator += duration;
|
||||||
}
|
}
|
||||||
if (currentTime - vibrationLowStartTime >= VIBRATION_STATE_CHANGE_INTERVAL) {
|
|
||||||
|
vibrationTotalLow = lowTimeAccumulator;
|
||||||
|
lowTimeAccumulator = 0;
|
||||||
|
highTimeAccumulator = 0;
|
||||||
|
interrupts();
|
||||||
|
|
||||||
|
vibrationWindowTotal -= vibrationHistory[vibrationHistoryIdx];
|
||||||
|
vibrationHistory[vibrationHistoryIdx] = vibrationTotalLow;
|
||||||
|
vibrationWindowTotal += vibrationTotalLow;
|
||||||
|
vibrationHistoryIdx = (vibrationHistoryIdx + 1) % VIBRATION_WINDOW_SIZE;
|
||||||
|
|
||||||
|
if (vibrationWindowTotal > VIBRATION_ACTIVE_THRESHOLD) {
|
||||||
isVibrationActive = true;
|
isVibrationActive = true;
|
||||||
}
|
} else {
|
||||||
vibrationHighStartTime = 0; // Reset high timer
|
|
||||||
} else if (!currentVibrationState && isVibrationActive) { // Vibration is inactive (HIGH)
|
|
||||||
if (vibrationHighStartTime == 0) {
|
|
||||||
vibrationHighStartTime = currentTime;
|
|
||||||
}
|
|
||||||
if (currentTime - vibrationHighStartTime >= VIBRATION_STATE_CHANGE_INTERVAL) {
|
|
||||||
isVibrationActive = false;
|
isVibrationActive = false;
|
||||||
}
|
}
|
||||||
vibrationLowStartTime = 0; // Reset low timer
|
|
||||||
} else if (currentVibrationState) {
|
|
||||||
vibrationHighStartTime = 0;
|
|
||||||
} else {
|
|
||||||
vibrationLowStartTime = 0;
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Light state change logic
|
// Light state change logic
|
||||||
if (currentLightLevel >= LIGHT_ACTIVATION_THRESHOLD && !isLightActive) {
|
if (currentLightLevel >= LIGHT_ACTIVATION_THRESHOLD && !isLightActive) {
|
||||||
@ -244,10 +283,10 @@ void loop() {
|
|||||||
|
|
||||||
#ifdef ENABLE_DISPLAY
|
#ifdef ENABLE_DISPLAY
|
||||||
// Update display data
|
// Update display data
|
||||||
displayData[0] = "Vibration: " + String(isVibrationActive ? "ON" : "OFF");
|
displayData[0] = "Vibra: " + String(vibrationWindowTotal);
|
||||||
displayData[1] = "Light: " + String(currentLightLevel) + " lx";
|
displayData[1] = " : " + String(isVibrationActive ? "ON" : "OFF");
|
||||||
displayData[2] = "Light Active: " + String(isLightActive ? "ON" : "OFF");
|
displayData[2] = "Light: " + String(currentLightLevel) + " lx";
|
||||||
displayData[3] = "Device: " + String(isDeviceActive ? "ACTIVE" : "INACTIVE");
|
displayData[3] = " : " + String(isLightActive ? "ON" : "OFF");
|
||||||
displayDataLines = 4;
|
displayDataLines = 4;
|
||||||
|
|
||||||
// Update display
|
// Update display
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user