Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FastIO FastPWM USBDevice
Diff: potSensor.h
- Revision:
- 47:df7a88cd249c
- Parent:
- 44:b5ac89b9cd5d
- Child:
- 48:058ace2aed1d
--- a/potSensor.h Mon Feb 15 20:30:32 2016 +0000
+++ b/potSensor.h Thu Feb 18 07:32:20 2016 +0000
@@ -25,7 +25,7 @@
{
}
- virtual bool highResScan(float &pos)
+ virtual bool read(uint16_t &pos)
{
// Take a few readings and use the average, to reduce the effect
// of analog voltage fluctuations. The voltage range on the ADC
@@ -38,20 +38,18 @@
// random noise in the readings.
//
// Readings through the standard AnalogIn class take about 30us
- // each, so 5 readings is about 150us. This is plenty fast enough
- // for even a low-res scan.
- pos = (pot.read() + pot.read() + pot.read() + pot.read() + pot.read())/5.0;
+ // each, so taking 5 readings takes about 150us. This is fast
+ // enough to resolve even the fastest plunger motiono with no
+ // aliasing.
+ pos = uint16_t((
+ uint32_t(pot.read_u16())
+ + uint32_t(pot.read_u16())
+ + uint32_t(pot.read_u16())
+ + uint32_t(pot.read_u16())
+ + uint32_t(pot.read_u16())
+ ) / 5U);
return true;
}
-
- virtual bool lowResScan(float &pos)
- {
- // Since we have only one analog input to sample, our read time is
- // very fast compared to the image sensor alternatives, so there's no
- // need to do anything different for a faster low-res scan. Simply
- // take a normal high-res reading.
- return highResScan(pos);
- }
private:
AnalogIn pot;