ResponsiveAnalogRead.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ResponsiveAnalogRead.h
  3. * Arduino library for eliminating noise in analogRead inputs without decreasing responsiveness
  4. *
  5. * Copyright (c) 2016 Damien Clarke
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #ifndef RESPONSIVE_ANALOG_READ_H
  26. #define RESPONSIVE_ANALOG_READ_H
  27. #include <Arduino.h>
  28. class ResponsiveAnalogRead
  29. {
  30. public:
  31. // pin - the pin to read
  32. // sleepEnable - enabling sleep will cause values to take less time to stop changing and potentially stop changing more abruptly,
  33. // where as disabling sleep will cause values to ease into their correct position smoothly
  34. // snapMultiplier - a value from 0 to 1 that controls the amount of easing
  35. // increase this to lessen the amount of easing (such as 0.1) and make the responsive values more responsive
  36. // but doing so may cause more noise to seep through if sleep is not enabled
  37. ResponsiveAnalogRead(){}; //default constructor must be followed by call to begin function
  38. ResponsiveAnalogRead(int pin, bool sleepEnable, float snapMultiplier = 0.01){
  39. begin(pin, sleepEnable, snapMultiplier);
  40. };
  41. void begin(int pin, bool sleepEnable, float snapMultiplier = 0.01); // use with default constructor to initialize
  42. inline int getValue() { return responsiveValue; } // get the responsive value from last update
  43. inline int getRawValue() { return rawValue; } // get the raw analogRead() value from last update
  44. inline bool hasChanged() { return responsiveValueHasChanged; } // returns true if the responsive value has changed during the last update
  45. inline bool isSleeping() { return sleeping; } // returns true if the algorithm is currently in sleeping mode
  46. void update(); // updates the value by performing an analogRead() and calculating a responsive value based off it
  47. void update(int rawValueRead); // updates the value accepting a value and calculating a responsive value based off it
  48. void setSnapMultiplier(float newMultiplier);
  49. inline void enableSleep() { sleepEnable = true; }
  50. inline void disableSleep() { sleepEnable = false; }
  51. inline void enableEdgeSnap() { edgeSnapEnable = true; }
  52. // edge snap ensures that values at the edges of the spectrum (0 and 1023) can be easily reached when sleep is enabled
  53. inline void disableEdgeSnap() { edgeSnapEnable = false; }
  54. inline void setActivityThreshold(float newThreshold) { activityThreshold = newThreshold; }
  55. // the amount of movement that must take place to register as activity and start moving the output value. Defaults to 4.0
  56. inline void setAnalogResolution(int resolution) { analogResolution = resolution; }
  57. // if your ADC is something other than 10bit (1024), set that here
  58. private:
  59. int pin;
  60. int analogResolution = 1024;
  61. float snapMultiplier;
  62. bool sleepEnable;
  63. float activityThreshold = 4.0;
  64. bool edgeSnapEnable = true;
  65. float smoothValue;
  66. unsigned long lastActivityMS;
  67. float errorEMA = 0.0;
  68. bool sleeping = false;
  69. int rawValue;
  70. int responsiveValue;
  71. int prevResponsiveValue;
  72. bool responsiveValueHasChanged;
  73. int getResponsiveValue(int newValue);
  74. float snapCurve(float x);
  75. };
  76. #endif