view Potentiometer.h @ 1:c74204ea535c

added isStopped
author ark
date 2017-01-25 23:01 +0100
parents 4c32c9d4b94f
children a5e06e87f081
line wrap: on
line source

#ifndef Potentiometer_h
#define Potentiometer_h

/**
    utility class for dejittering a potentiometer and/or mapping its value to
    ranges.

    (c) 2017 arkadiusz@wobblyhead.net
*/
class Potentiometer {
    public: 
        // dejittered pot value
        int value;

        // flag, if true the pot is not moving
        bool isStopped;
        
        /**
            create an uninitialised Potentiometer.
            setup() must be called after this to make it usable.
        */
        Potentiometer();

        /**
            setup pot instance.
            @param {byte} pin   input pin
        */
        void setup(byte pin);

        /**
            check pin and update values.
        */
        void update();

        /**
            check pin and update values.
            @param {unsigned long} ms   ignored
        */
        void update(unsigned long ms);

        /** jitter threshold. value changes smalled than the threshold will be
         * added to current average. value changes larger than the threshold
         * reset the average.
         */
        const unsigned long JITTER_THRESHOLD = 2;
        
        /** max samples for de-jittering.
         */
        const int MAX_SAMPLES = 30;

    private: 
        byte pin;
        float sum;
        float avg;
        int num;

};

#endif