2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/AnalogPin/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-10-17 13:20:48 -04:00
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/AnalogPin/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AnalogPin/actions/workflows/arduino-lint.yml)
|
|
|
|
[![JSON check](https://github.com/RobTillaart/AnalogPin/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AnalogPin/actions/workflows/jsoncheck.yml)
|
2023-10-17 09:04:21 -04:00
|
|
|
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/AnalogPin.svg)](https://github.com/RobTillaart/AnalogPin/issues)
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AnalogPin/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AnalogPin.svg?maxAge=3600)](https://github.com/RobTillaart/AnalogPin/releases)
|
2023-10-17 09:04:21 -04:00
|
|
|
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/AnalogPin.svg)](https://registry.platformio.org/libraries/robtillaart/AnalogPin)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
# AnalogPin
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
Arduino library to add functionality on top of analogRead().
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
## Description
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
AnalogPin is an Arduino class that adds noise filtering and smoothing to analogRead().
|
2021-10-17 13:20:48 -04:00
|
|
|
Furthermore it can speed up the analogRead() function by tuning the pre-scaler.
|
2020-11-27 05:10:47 -05:00
|
|
|
This latter is AVR only.
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
## Interface
|
|
|
|
|
2023-10-17 09:04:21 -04:00
|
|
|
```cpp
|
|
|
|
#include "AnalogPin.h"
|
|
|
|
```
|
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
- **AnalogPin(uint8_t pin)** constructor with analogue pin as parameter.
|
|
|
|
- **void setPrescaler(uint8_t prescale = 7)** AVR only pre-scaler.
|
|
|
|
- **uint8_t getPrescaler()** return pre-scaler set.
|
|
|
|
- **void setNoiseThreshold(uint8_t noise = 0)** set noise level that should be ignored. Typical 0..2.
|
|
|
|
- **uint8_t getNoiseThreshold()** return set value.
|
|
|
|
- **void setSmoothWeight(uint8_t alpha = 0)** alpha = 0..31, parameter for low pass filter.
|
|
|
|
- **uint8_t getSmoothWeight(void)** returns set alpha.
|
|
|
|
- **int read(bool twice = false)** read function, optional read twice to stabilize.
|
|
|
|
- **int readSmoothed()** read version that uses low pass filter.
|
|
|
|
- **int readPrevious()** returns previous read value.
|
|
|
|
- **int readLast()** returns last read value without reading a new one.
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
## Operation
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
**get/setPrescaler(uint8_t prescale)** can be used to speed up analogRead().
|
2021-10-17 13:20:48 -04:00
|
|
|
The effect is that both the accuracy and precision are affected.
|
2020-11-27 05:10:47 -05:00
|
|
|
You should verify if this is acceptable for your project.
|
|
|
|
***Works only for AVR based boards***
|
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
**get/setNoiseThreshold(uint8_t noise)** is used to set the noise threshold to be used by the **read()** function.
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
**read(bool twice)** implements an **analogRead()** that suppresses small noise fluctuations.
|
2021-10-17 13:20:48 -04:00
|
|
|
The parameter twice is used to force analogRead() to be executed twice to reduce noise from the multiplexing.
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
Example: if the previous read has the value 300 and you
|
|
|
|
want to interpret all subsequent readings between 290
|
|
|
|
and 310 as 300 (the same) your code should look like:
|
|
|
|
```
|
|
|
|
AP.setNoiseThreshold(10);
|
|
|
|
AP.read();
|
|
|
|
```
|
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
**get/setSmoothWeight(uint8_t alpha)** is used to set the weight factor for the **readSmoothed()** function.
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
The weight of the previous read is **alpha/32**.
|
|
|
|
A higher alpha will dampen the signal more, a lower alpha
|
|
|
|
will follow the actual signal better.
|
|
|
|
This can be used to suppress noise too.
|
|
|
|
|
|
|
|
```
|
|
|
|
AP.setSmoothWeight(4); // weight = 4/32 = 1/8 = 12.5%
|
|
|
|
AP.readSmoothed();
|
|
|
|
```
|
|
|
|
|
|
|
|
**readSmoothed()** implements an analogRead with a running average build in.
|
|
|
|
|
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
## Future
|
|
|
|
|
2023-10-17 09:04:21 -04:00
|
|
|
#### Must
|
|
|
|
|
2021-12-12 09:57:24 -05:00
|
|
|
- update documentation
|
|
|
|
- advantage of certain functions, when to use
|
2023-10-17 09:04:21 -04:00
|
|
|
|
|
|
|
#### Should
|
|
|
|
|
2021-10-17 13:20:48 -04:00
|
|
|
- more examples
|
2023-10-17 09:04:21 -04:00
|
|
|
|
|
|
|
#### Could
|
|
|
|
|
2022-10-28 15:33:10 -04:00
|
|
|
- move code to .cpp
|
2021-10-17 13:20:48 -04:00
|
|
|
|
2023-10-17 09:04:21 -04:00
|
|
|
#### Wont
|
|
|
|
|
|
|
|
- **volts()** + get/setFactor(float f)
|
|
|
|
- any other unit would make equal sense?
|
|
|
|
|
|
|
|
|
|
|
|
## Support
|
|
|
|
|
|
|
|
If you appreciate my libraries, you can support the development and maintenance.
|
|
|
|
Improve the quality of the libraries by providing issues and Pull Requests, or
|
|
|
|
donate through PayPal or GitHub sponsors.
|
|
|
|
|
|
|
|
Thank you,
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
|