|
|
@@ -0,0 +1,133 @@
|
|
|
+#include <LiquidCrystal_I2C.h>
|
|
|
+
|
|
|
+/*
|
|
|
+ * Arduino UNO:
|
|
|
+ *
|
|
|
+ * GND -> GND
|
|
|
+ * 5V -> VCC
|
|
|
+ * A4 -> SDA
|
|
|
+ * A5 -> SDL
|
|
|
+ *
|
|
|
+ */
|
|
|
+LiquidCrystal_I2C lcd(0x27, 16, 2);
|
|
|
+
|
|
|
+double ema = 0;
|
|
|
+
|
|
|
+/*
|
|
|
+ * Timing vars (milliseconds)
|
|
|
+ */
|
|
|
+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;
|
|
|
+
|
|
|
+const char * sensorName = "SR04";
|
|
|
+const int dp = pseudoRandomNumberGenerator ? 0 : 2;
|
|
|
+
|
|
|
+// HC-SR04 Pins
|
|
|
+const int trigPin = 3;
|
|
|
+const int echoPin = 2;
|
|
|
+
|
|
|
+// Function to read distance in cm
|
|
|
+float readDistance() {
|
|
|
+ // Enforce minimum 60ms delay between readings to prevent ghost echoes
|
|
|
+ if (millis() - lastRead < 60) {
|
|
|
+ return -1.0; // Return invalid flag if called too soon
|
|
|
+ }
|
|
|
+
|
|
|
+ // Clear trigPin
|
|
|
+ digitalWrite(trigPin, LOW);
|
|
|
+ delayMicroseconds(2);
|
|
|
+
|
|
|
+ // Send 10us pulse
|
|
|
+ digitalWrite(trigPin, HIGH);
|
|
|
+ delayMicroseconds(10);
|
|
|
+ digitalWrite(trigPin, LOW);
|
|
|
+
|
|
|
+ // Update last read time immediately after triggering
|
|
|
+ lastRead = millis();
|
|
|
+
|
|
|
+ // Read echo pulse duration (timeout 30000us = ~5 meters)
|
|
|
+ long duration = pulseIn(echoPin, HIGH, 30000);
|
|
|
+
|
|
|
+ // Handle timeout (no echo received)
|
|
|
+ if (duration == 0) {
|
|
|
+ return -1.0; // Return invalid flag
|
|
|
+ }
|
|
|
+
|
|
|
+ // Calculate distance: (duration * speed of sound) / 2
|
|
|
+ float distance = (duration * 0.0343) / 2.0;
|
|
|
+ return distance;
|
|
|
+}
|
|
|
+
|
|
|
+int myRead(){
|
|
|
+ return readDistance();
|
|
|
+}
|
|
|
+
|
|
|
+int read(){
|
|
|
+ if (pseudoRandomNumberGenerator)
|
|
|
+ {
|
|
|
+ return random(0, 1024);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return myRead();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void display(){
|
|
|
+ lcd.print("EMA: ");
|
|
|
+ lcd.print(ema, dp);
|
|
|
+ lcd.print(" ");
|
|
|
+}
|
|
|
+
|
|
|
+void setup() {
|
|
|
+ lcd.init();
|
|
|
+ lcd.begin(16, 2);
|
|
|
+ lcd.backlight();
|
|
|
+ lcd.clear();
|
|
|
+
|
|
|
+ // Initialize HC-SR04 pins
|
|
|
+ pinMode(trigPin, OUTPUT);
|
|
|
+ pinMode(echoPin, INPUT);
|
|
|
+
|
|
|
+ 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("...");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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();
|
|
|
+ }
|
|
|
+}
|