|
|
@@ -1,5 +1,8 @@
|
|
|
+#include "src/sensor_ema/sensor_ema.h"
|
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
|
|
+const char * sensorName = "SR04";
|
|
|
+const int dp = 2;
|
|
|
/*
|
|
|
* Arduino UNO:
|
|
|
*
|
|
|
@@ -11,28 +14,26 @@
|
|
|
*/
|
|
|
LiquidCrystal_I2C lcd(0x27, 16, 2);
|
|
|
|
|
|
-double ema = 0;
|
|
|
|
|
|
-/*
|
|
|
- * Timing vars (milliseconds)
|
|
|
- */
|
|
|
+// HC-SR04 Pins
|
|
|
+const int trigPin = 3;
|
|
|
+const int echoPin = 2;
|
|
|
unsigned long lastRead = 0;
|
|
|
-unsigned long lastWrite = 0;
|
|
|
-const unsigned long interval = 2000;
|
|
|
-
|
|
|
-// If no sensor, can use a PRNG
|
|
|
-bool pseudoRandomNumberGenerator = true;
|
|
|
-const int seed = 0;
|
|
|
|
|
|
-// EMA vars
|
|
|
-double alpha = 0.1;
|
|
|
+void my_init() {
|
|
|
+ lcd.init();
|
|
|
+ //lcd.begin(16, 2);
|
|
|
+ lcd.backlight();
|
|
|
+ lcd.clear();
|
|
|
|
|
|
-const char * sensorName = "SR04";
|
|
|
-const int dp = pseudoRandomNumberGenerator ? 0 : 2;
|
|
|
+ // Initialize HC-SR04 pins
|
|
|
+ pinMode(trigPin, OUTPUT);
|
|
|
+ pinMode(echoPin, INPUT);
|
|
|
|
|
|
-// HC-SR04 Pins
|
|
|
-const int trigPin = 3;
|
|
|
-const int echoPin = 2;
|
|
|
+ lcd.print("Sampling ");
|
|
|
+ lcd.print(sensorName);
|
|
|
+ lcd.print("...");
|
|
|
+}
|
|
|
|
|
|
// Function to read distance in cm
|
|
|
float readDistance() {
|
|
|
@@ -66,68 +67,27 @@ float readDistance() {
|
|
|
return distance;
|
|
|
}
|
|
|
|
|
|
-int myRead(){
|
|
|
- return readDistance();
|
|
|
-}
|
|
|
-
|
|
|
-int read(){
|
|
|
- if (pseudoRandomNumberGenerator)
|
|
|
- {
|
|
|
- return random(0, 1024);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return myRead();
|
|
|
- }
|
|
|
+void my_write(float val) {
|
|
|
+ lcd.setCursor(0, 1);
|
|
|
+ lcd.print("EMA: ");
|
|
|
+ lcd.print(val, dp);
|
|
|
+ lcd.print(" ");
|
|
|
}
|
|
|
|
|
|
-void display(){
|
|
|
- lcd.print("EMA: ");
|
|
|
- lcd.print(ema, dp);
|
|
|
- lcd.print(" ");
|
|
|
-}
|
|
|
+// --- Global Context ---
|
|
|
+SensorEMA_t sensorCtx;
|
|
|
|
|
|
void setup() {
|
|
|
- lcd.init();
|
|
|
- lcd.begin(16, 2);
|
|
|
- lcd.backlight();
|
|
|
- lcd.clear();
|
|
|
-
|
|
|
- // Initialize HC-SR04 pins
|
|
|
- pinMode(trigPin, OUTPUT);
|
|
|
- pinMode(echoPin, INPUT);
|
|
|
+ // Assign function pointers
|
|
|
+ sensorCtx.fn_init = my_init;
|
|
|
+ sensorCtx.fn_read = readDistance;
|
|
|
+ sensorCtx.fn_write = my_write;
|
|
|
|
|
|
- if (pseudoRandomNumberGenerator)
|
|
|
- {
|
|
|
- randomSeed(seed);
|
|
|
- }
|
|
|
- ema = read();
|
|
|
-
|
|
|
- lcd.setCursor(0, 0);
|
|
|
- if (pseudoRandomNumberGenerator)
|
|
|
- {
|
|
|
- lcd.print("Sampling PRNG...");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- lcd.print("Sampling ");
|
|
|
- lcd.print(sensorName);
|
|
|
- lcd.print("...");
|
|
|
- }
|
|
|
+ // Initialize Library (Alpha=0.1, Interval=2000ms)
|
|
|
+ SensorEMA_setup(&sensorCtx, 0.1, 2000);
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
- long curMillis = millis();
|
|
|
- int raw = read();
|
|
|
-
|
|
|
- if (raw >= 0){
|
|
|
- ema = (raw * alpha) + (ema * (1.0 - alpha));
|
|
|
- }
|
|
|
-
|
|
|
- if (curMillis - lastWrite >= interval)
|
|
|
- {
|
|
|
- lastWrite = curMillis;
|
|
|
- lcd.setCursor(0, 1);
|
|
|
- display();
|
|
|
- }
|
|
|
+ // Call library loop as fast as possible
|
|
|
+ SensorEMA_loop(&sensorCtx, millis());
|
|
|
}
|