Fab'd up a bracket and mounted the US sensor on the shaper.....
That helped a lot to settle down repeatability.
40 and -57 FPM doesn't give an average of 54 FPM though
The code boiled down to this....
--------------------------------------------------------------------
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
// Include the library:
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define MAX_DISTANCE 60
NewPing sonar(pingPin, echoPin, MAX_DISTANCE);
// Create variables
float c_cm = 0.0;
static float p_cm = 0.0;
float d_cm = 0.0;
unsigned long c_ms = 0;
static unsigned long p_ms = 0;
float d_ms = 0.0;
float d_sec = 0.0;
float cps = 0.0;
float fpm = 0.0;
static float maxfwdfpm = 0.0;
static float maxrevfpm = 0.0;
float dispfpm = 0.0;
float c_dur = 0.0;
void setup() {
Serial.begin(9600); // Starting Serial Terminal
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("FWD FPM ");
lcd.setCursor(0,1);
lcd.print("REV FPM ");
lcd.setCursor(0,1);
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
c_dur = sonar.ping_median(5);
c_cm = microsecondsToCentimeters(c_dur);
if (c_cm > 0)
{
if (abs(c_cm - p_cm) <= 0.3) p_cm = c_cm;
d_cm = c_cm - p_cm;
c_ms = millis();
d_ms = c_ms - p_ms;
d_sec = d_ms / 1000.0;
cps = d_cm / d_sec;
fpm = (cps / 30.48) * 60.0;
bool fwdfpm = false;
bool revfpm = false;
bool zfpm = false;
if (c_cm > p_cm)
{
fwdfpm = true;
if (fpm > maxfwdfpm) maxfwdfpm = fpm;
dispfpm = maxfwdfpm / 2.0;
}
if (c_cm < p_cm)
{
revfpm = true;
if (fpm < maxrevfpm) maxrevfpm = fpm;
dispfpm = maxrevfpm / 2.0;
}
if (c_cm == p_cm) zfpm = true;
if (fwdfpm)
{
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(round(dispfpm));
}
if (revfpm)
{
lcd.setCursor(8,1);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print(round(dispfpm));
}
if (zfpm)
{
// maxfwdfpm = 0.0;
// maxrevfpm = 0.0;
// dispfpm = 0.0;
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print("0.00");
lcd.setCursor(8,1);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print("0.00");
}
}
else
{
// maxfwdfpm = 0.0;
// maxrevfpm = 0.0;
// dispfpm = 0.0;
lcd.setCursor(8,0);
lcd.print(" ERROR ");
lcd.setCursor(8,1);
lcd.print(" ERROR ");
}
p_cm = c_cm;
p_ms = c_ms;
} //loop
float microsecondsToCentimeters(long microseconds) {
return ((microseconds / 29.0) / 2.0);
}
--------------------------------------------------------------------
It will track increases in FPM but not decreases AND somehow every once in a while it will jump into the 0 FPM code with the shaper running steady state. Can't for the life of me figure how to code around that.....