-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpio_jsn_sr04t.h
32 lines (24 loc) · 1.13 KB
/
gpio_jsn_sr04t.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef gpio_jsn_sr04t_h
#define gpio_jsn_sr04t_h
#include <NewPing.h>
#include "NmeaXDR.h"
#define NEWPING_TRIGGER_PIN G5 // Pin tied to trigger pin on the ultrasonic sensor.
#define NEWPING_ECHO_PIN G6 // Pin tied to echo pin on the ultrasonic sensor.
#define NEWPING_MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing gpio_jsn_sr04t_sonar(NEWPING_TRIGGER_PIN, NEWPING_ECHO_PIN, NEWPING_MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void gpio_jsn_sr04t_report() {
float temp = 28.0; // Temperature in Celsius (this value would probably come from a temperature sensor).
float factor = sqrt(1 + temp / 273.15) / 60.368; // Speed of sound calculation based on temperature.
float distance_cm = (float)gpio_jsn_sr04t_sonar.ping_median(5) * factor;
if (fabs(distance_cm) > 0.001) {
gen_nmea0183_xdr("$BBXDR,D,%.2f,M,Range_JSN_S04T", distance_cm / 100.0);
}
}
void gpio_jsn_sr04t_try_init() {
pinMode(NEWPING_TRIGGER_PIN, OUTPUT);
pinMode(NEWPING_ECHO_PIN, INPUT);
app.onRepeat(1000, []() {
gpio_jsn_sr04t_report();
});
}
#endif