ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SHARPGP2Y.cpp
Go to the documentation of this file.
1#include "SHARPGP2Y.h"
2
3SHARPGP2Y::SHARPGP2Y(uint8_t pin) : pin(pin) {
4 pinMode(pin, INPUT);
5}
6
7float SHARPGP2Y::ReadingToDistance(uint8_t reading) {
8 float value = float(reading);
9 float value2 = value * value;
10
11 return 2583.711122992086f
12 - 20.19789785547f * value + 0.071746539329f * value2
13 - 0.000115854182f * value2 * value + 0.000000068590f * value2 * value2;
14}
15
17 return distance;
18}
19
21 noInterrupts();
22 float read = analogRead(pin);
23 interrupts();
24 distance = rAF.Filter(ReadingToDistance(static_cast<uint8_t>(read)));
25
26 return distance;
27}
A class for interfacing with the SHARP GP2Y distance sensor.
float Filter(float value)
Filters the input value using the running average.
float distance
Current distance measurement.
Definition SHARPGP2Y.h:27
RunningAverageFilter< 25 > rAF
Running average filter for smoothing sensor readings.
Definition SHARPGP2Y.h:26
float Update()
Updates the distance measurement by reading the sensor.
Definition SHARPGP2Y.cpp:20
uint8_t pin
Analog pin connected to the sensor.
Definition SHARPGP2Y.h:28
float GetDistance()
Retrieves the current distance measurement.
Definition SHARPGP2Y.cpp:16
float ReadingToDistance(uint8_t reading)
Converts a raw sensor reading to a distance value.
Definition SHARPGP2Y.cpp:7
SHARPGP2Y(uint8_t pin)
Constructs a SHARPGP2Y instance with the specified pin.
Definition SHARPGP2Y.cpp:3