mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.5 AnalogPin
This commit is contained in:
parent
695c60b6d6
commit
5625713596
@ -2,6 +2,10 @@ compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
- leonardo
|
||||
- due
|
||||
- zero
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
@ -4,10 +4,14 @@ name: Arduino CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
arduino_ci:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: Arduino-CI/action@master
|
||||
# Arduino-CI/action@v0.1.1
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
@ -1,41 +1,57 @@
|
||||
//
|
||||
// FILE: AnalogPin.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.4
|
||||
// VERSION: 0.2.5
|
||||
// DATE: 2013-09-09
|
||||
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2013-09-09 initial version
|
||||
// 0.1.01 - 2013-11-09 added some comments
|
||||
// 0.1.02 - 2014-10-05 changed signatures datatypes
|
||||
// 0.1.03 - 2014-12-07 some refactor
|
||||
// 0.1.04 - 2015-03-06 refactor smaller footprint
|
||||
// 0.2.00 - 2015-05-14 added prescale support
|
||||
// 0.2.01 - 2015-12-19 breaking interface; big refactor;
|
||||
// 0.2.2 2020-03-25 refactor AVR specific code; bugfix
|
||||
// 0.2.3 2020-05-27 update library.json
|
||||
// 0.2.4 2020-12-10 add Arduino-ci
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 2013-09-09 initial version
|
||||
// 0.1.01 2013-11-09 added some comments
|
||||
// 0.1.02 2014-10-05 changed signatures data types
|
||||
// 0.1.03 2014-12-07 some refactor
|
||||
// 0.1.04 2015-03-06 refactor smaller footprint
|
||||
// 0.2.00 2015-05-14 added pre-scale support
|
||||
// 0.2.01 2015-12-19 breaking interface; big refactor;
|
||||
// 0.2.2 2020-03-25 refactor AVR specific code; bugfix
|
||||
// 0.2.3 2020-05-27 update library.json
|
||||
// 0.2.4 2020-12-10 add Arduino-ci
|
||||
// 0.2.5 2021-10-17 update Arduino-CI
|
||||
|
||||
|
||||
#include "AnalogPin.h"
|
||||
|
||||
|
||||
AnalogPin::AnalogPin(const uint8_t pin)
|
||||
{
|
||||
_pin = pin;
|
||||
_prescale = 7;
|
||||
_alpha = 0;
|
||||
_noise = 0;
|
||||
rawRead();
|
||||
_rawRead();
|
||||
_prevValue = _value;
|
||||
}
|
||||
|
||||
|
||||
void AnalogPin::setPrescaler(const uint8_t prescale)
|
||||
{
|
||||
_prescale = prescale;
|
||||
if (_prescale < 2) _prescale = 2;
|
||||
else if (_prescale > 7) _prescale = 7;
|
||||
};
|
||||
|
||||
|
||||
void AnalogPin::setSmoothWeight(const uint8_t alpha)
|
||||
{
|
||||
_alpha = alpha;
|
||||
if (_alpha > 31) _alpha = 31;
|
||||
};
|
||||
|
||||
|
||||
int AnalogPin::read(const bool twice)
|
||||
{
|
||||
if (twice) rawRead();
|
||||
rawRead();
|
||||
if (twice) _rawRead();
|
||||
_rawRead();
|
||||
if ( (_noise == 0) || (((_value - _prevValue) & 0x7FFF) > _noise) )
|
||||
{
|
||||
_prevValue = _value;
|
||||
@ -43,9 +59,10 @@ int AnalogPin::read(const bool twice)
|
||||
return _prevValue;
|
||||
}
|
||||
|
||||
|
||||
int AnalogPin::readSmoothed()
|
||||
{
|
||||
rawRead();
|
||||
_rawRead();
|
||||
if (_alpha > 0)
|
||||
{
|
||||
_value = _value + (_alpha * (_prevValue - _value)) / 32;
|
||||
@ -54,7 +71,8 @@ int AnalogPin::readSmoothed()
|
||||
return _value;
|
||||
}
|
||||
|
||||
void AnalogPin::rawRead()
|
||||
|
||||
void AnalogPin::_rawRead()
|
||||
{
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
// remember old register value
|
||||
|
@ -2,15 +2,17 @@
|
||||
//
|
||||
// FILE: AnalogPin.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.4
|
||||
// VERSION: 0.2.5
|
||||
// DATE: 2013-09-09
|
||||
// PURPOSE: wrapper for analogRead with smoothing and noise filtering
|
||||
// URL: https://github.com/RobTillaart/AnalogPin
|
||||
//
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define ANALOGPIN_LIB_VERSION "0.2.4"
|
||||
#define ANALOGPIN_LIB_VERSION (F("0.2.5"))
|
||||
|
||||
|
||||
class AnalogPin
|
||||
{
|
||||
@ -23,7 +25,7 @@ public:
|
||||
// 2 => 14 uSec 5 => 38 uSec
|
||||
// 3 => 18 uSec 6 => 63 uSec
|
||||
// 4 => 24 uSec 7 => 120 uSec (default/normal)
|
||||
void setPrescaler(const uint8_t prescale = 7) { _prescale = constrain(prescale, 2, 7); };
|
||||
void setPrescaler(const uint8_t prescale = 7);
|
||||
inline uint8_t getPrescaler(void) const { return _prescale; };
|
||||
|
||||
// noise 0..255; in practice only small values are used (0..10).
|
||||
@ -31,7 +33,7 @@ public:
|
||||
inline uint8_t getNoiseThreshold(void) const { return _noise; };
|
||||
|
||||
// alpha 0..31;
|
||||
void setSmoothWeight(const uint8_t alpha = 0) { _alpha = min(alpha, 31); };
|
||||
void setSmoothWeight(const uint8_t alpha = 0);
|
||||
inline uint8_t getSmoothWeight(void) const { return _alpha; };
|
||||
|
||||
// set twice to true to do analogRead twice to reduce noise too
|
||||
@ -42,9 +44,9 @@ public:
|
||||
inline int readPrevious(void) const { return _prevValue; }
|
||||
inline int readLast(void) const { return _value; }
|
||||
|
||||
private:
|
||||
|
||||
void rawRead();
|
||||
private:
|
||||
void _rawRead();
|
||||
|
||||
uint8_t _pin;
|
||||
uint8_t _alpha;
|
||||
|
@ -1,8 +1,11 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/AnalogPin/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![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)
|
||||
[![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)
|
||||
|
||||
|
||||
# AnalogPin
|
||||
|
||||
Arduino library to add functionality on top of analogRead()
|
||||
@ -12,27 +15,37 @@ Arduino library to add functionality on top of analogRead()
|
||||
|
||||
AnalogPin is an Arduino class that adds noise filtering and smoothing
|
||||
to analogRead().
|
||||
Furthermore it can speed up the analogRead() function by tuning the prescaler.
|
||||
Furthermore it can speed up the analogRead() function by tuning the pre-scaler.
|
||||
This latter is AVR only.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
|
||||
- **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.
|
||||
|
||||
|
||||
## Operation
|
||||
|
||||
**readLast()** returns the last read value without reading a new one.
|
||||
|
||||
**get/setPrescaler(prescale)** can be used to speed up analogRead().
|
||||
|
||||
The effect is that both the accuracy and precission are affected.
|
||||
**get/setPrescaler(prescale)** can be used to speed up analogRead().
|
||||
The effect is that both the accuracy and precision are affected.
|
||||
You should verify if this is acceptable for your project.
|
||||
***Works only for AVR based boards***
|
||||
|
||||
**get/setNoiseThreshold(noise)** is used to set the noise threshold to be used by the **read()** function.
|
||||
|
||||
**get/setNoiseThreshold(noise)** is used to set the noise threshold to be used by
|
||||
the **read()** function.
|
||||
|
||||
**read(twice)** implements an **analogRead()** that supresses small noise fluctuations.
|
||||
The parameter twice is used to force analogRead() to be executed twice to reduce noise
|
||||
from the multiplexing.
|
||||
**read(twice)** implements an **analogRead()** that suppresses small noise fluctuations.
|
||||
The parameter twice is used to force analogRead() to be executed twice to reduce noise from the multiplexing.
|
||||
|
||||
Example: if the previous read has the value 300 and you
|
||||
want to interpret all subsequent readings between 290
|
||||
@ -56,8 +69,9 @@ This can be used to suppress noise too.
|
||||
|
||||
**readSmoothed()** implements an analogRead with a running average build in.
|
||||
|
||||
Two functions that expose information that might sometimes be useful.
|
||||
**readPrevious()** returns the previous value read.
|
||||
|
||||
**readLast()** returns the last value read.
|
||||
## Future
|
||||
|
||||
- more examples
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/AnalogPin"
|
||||
},
|
||||
"version": "0.2.4",
|
||||
"version": "0.2.5",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=AnalogPin
|
||||
version=0.2.4
|
||||
version=0.2.5
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino Library for AnalogPin
|
||||
|
Loading…
Reference in New Issue
Block a user