├── AnalogSmooth.cpp ├── AnalogSmooth.h ├── LICENSE.txt ├── README.md ├── examples └── analogSmooth │ └── analogSmooth.ino └── keywords.txt /AnalogSmooth.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AnalogSmooth.cpp - Library to smooth analog signal 3 | jitter by averaging concurrent readings. 4 | Created by Michael Thessel. 5 | */ 6 | #include "Arduino.h" 7 | #include "AnalogSmooth.h" 8 | 9 | /* 10 | Constructor 11 | */ 12 | AnalogSmooth::AnalogSmooth() 13 | { 14 | this->_init(10); 15 | } 16 | 17 | /* 18 | Constructor 19 | */ 20 | AnalogSmooth::AnalogSmooth(unsigned int windowSize) 21 | { 22 | this->_init(windowSize); 23 | } 24 | 25 | /* 26 | Initialzie the environment 27 | */ 28 | void AnalogSmooth::_init(unsigned int windowSize) 29 | { 30 | // Restrict the size of the history array 31 | // to >= 1 and < 100 items 32 | if (windowSize > 100) { windowSize = 100; } 33 | if (windowSize < 1) { windowSize = 1; } 34 | this->_windowSize = windowSize; 35 | 36 | this->_analogPointer = 0; 37 | this->_maxPointer = 0; 38 | } 39 | 40 | /* 41 | Perform smooting of analog input from given value 42 | */ 43 | float AnalogSmooth::smooth(float value) 44 | { 45 | // Return if we only keep track of 1 value 46 | if (this->_windowSize == 1) { 47 | return value; 48 | } 49 | 50 | // Save the value to the history array 51 | this->_analog[this->_analogPointer] = value; 52 | 53 | // Calculate the moving average 54 | float total = 0; 55 | for (int i = 0; i <= this->_maxPointer; i++) { 56 | total = total + this->_analog[i]; 57 | } 58 | float avg = total / (this->_maxPointer + 1); 59 | 60 | // Keep track of how many items we have in the array 61 | if (this->_maxPointer < this->_windowSize - 1) { 62 | this->_maxPointer++; 63 | } 64 | 65 | // Update the array pointer 66 | this->_analogPointer++; 67 | if (this->_analogPointer == this->_windowSize) { 68 | this->_analogPointer = 0; 69 | } 70 | 71 | // Retrun the average 72 | return avg; 73 | } 74 | 75 | /* 76 | Perform smooting of analog input from given pin 77 | */ 78 | float AnalogSmooth::analogReadSmooth(uint8_t pin) 79 | { 80 | // Read the pin 81 | float current = analogRead(pin); 82 | 83 | return this->smooth(current); 84 | } 85 | -------------------------------------------------------------------------------- /AnalogSmooth.h: -------------------------------------------------------------------------------- 1 | /* 2 | AnalogSmooth.h - Library to smooth analog signal 3 | jitter by averaging concurrent readings. 4 | Created by Michael Thessel. 5 | */ 6 | 7 | #ifndef AnalogSmooth_h 8 | #define AnalogSmooth_h 9 | 10 | #include "Arduino.h" 11 | 12 | class AnalogSmooth 13 | { 14 | public: 15 | AnalogSmooth(unsigned int windowSize); 16 | AnalogSmooth(); 17 | float analogReadSmooth(uint8_t pin); 18 | float smooth(float value); 19 | private: 20 | unsigned int _windowSize; 21 | uint8_t _pin; 22 | float _analog[100]; 23 | unsigned int _analogPointer; 24 | unsigned int _maxPointer; 25 | void _init(unsigned int windowSize); 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2018 Michael Thessel 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino Analog Smooth 2 | ===================== 3 | 4 | Arduino library to smoothen jittery analog signals 5 | 6 | For more information and a usage example visit my [blog](http://michaelthessel.com/analog-smoothing-library-for-arduino/). 7 | -------------------------------------------------------------------------------- /examples/analogSmooth/analogSmooth.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Smoothens signal jitter on analog inputs by averaging 3 | concurrent readings. The number of readings averaged can 4 | range from 1 (no smooting) to 100. 5 | 6 | The more readings you average the more consistent the signal 7 | becomes. At the same time it takes longer to detect actual 8 | changes in the signal. 9 | */ 10 | #include 11 | 12 | int analogPin = 1; 13 | 14 | // Defaults to window size 10 15 | AnalogSmooth as = AnalogSmooth(); 16 | 17 | // Window size can range from 1 - 100 18 | AnalogSmooth as100 = AnalogSmooth(100); 19 | 20 | void setup() { 21 | Serial.begin(9600); 22 | } 23 | 24 | void loop() { 25 | // Regular reading 26 | float analog = analogRead(analogPin); 27 | Serial.print("Non-Smooth: "); 28 | Serial.println(analog); 29 | 30 | // Smoothing with window size 10 31 | float analogSmooth = as.smooth(analog); 32 | Serial.print("Smooth (10): "); 33 | Serial.println(analogSmooth); 34 | 35 | // Smoothing with window size 100 36 | float analogSmooth100 = as100.analogReadSmooth(analogPin); 37 | Serial.print("Smooth (100): "); 38 | Serial.println(analogSmooth100); 39 | 40 | Serial.println(""); 41 | delay(1000); 42 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | AnalogSmooth KEYWORD1 2 | analogReadSmooth KEYWORD2 3 | --------------------------------------------------------------------------------