Making a library to increase a function with analog value

Hi, I have this function which increases a variable when a analog value is given to it. Eg, low analog value would increase/decrease the value a little over time and a higher analogvalue would then increase the value a lot over time.

But here is the questions.
How should I go about expanding this library?

  1. Make it work with floats, longs doubles, what ever input type you want.
  2. Make it a pointer based and a return based. Eg, call the update function and it updates the pointer given to it.

like this

int valueToBeUpdated = 0;

void functionThatWouldUpdateValue();

I think you would have to pass the &memory location in the intilization part and & or pointer which the update function would get the analog value from.

Have a function that would mass update all of the objects.
I would like to have a function that I can pass function pointers to and that function would then call the update with a single line.

  1. Have some optional arguments like deadzone and max/min value. I know how to implement theese but not elegantly.
if(inputvalue < deadzone || deadzone < inputvalue){

and to return the max I could just use a ternary.
return (input < max) ? input : max;
But not sure how to have this with pointer. 
}

Thanks for the help!

Forgot to include the source files!

analogIncrease.cpp (554 Bytes)
analogIncrease.h (484 Bytes)

seems like you're talking about several different things

  • i think you're describing a class, not necessarily a library
  • i don't see the update() identified in the .h, wouldn't it be the function used to update the value and shouldn't it be public and shouldn't it return the updated value
  • don't see inputArray[] nor understand why you would need such
  • not sure what you mean by value type -- the value provided to the class or the value maintained and returned. (why not just do everything with doubles)
  • don't understand low, high and changeby. seems you would have a threshold above which you adjust the value by a large amount and below which you adjust by a small amount. are low/high the adjustment values or thresholds
  • don't understand the purpose of a deadzone. is the deadzone around the threshold
  • additional parameter (i.e. max, min can always be set in the constructor but need to be defined
  • are there just 5 parameters: min, deltaLow, thresh, deltaHigh, max?

in C++ Programming Style Cargill discusses poorly designed classes, showing examples, discussing their flaws and how to improve them

consider


// -----------------------------------------------------------------------------
class AnalogIncrease {
    double _min;
    double _delLo;
    double _thresh;
    double _delHi;
    double _max;
    double _val;

  public:
    AnalogIncrease (double min,
                    double delLo,
                    double thresh,
                    double delHi,
                    double max )
    {
        _min    = min,
        _delLo  = delLo,
        _thresh = thresh,
        _delHi  = delHi,
        _max    = max;
        _val    = 0;
    }

    double value (void)   { return _val; };

    double update (double val)  {
        if (_val < val)  {

            Serial.print   (_val);
            Serial.print   (" < ");
            Serial.print   (val);
            Serial.println ("  greater");
        }
        if ( _thresh < val)
            _val += _val < val ? _delHi : -_delHi;
        else
            _val += _val < val ? _delLo : -_delLo;
        return _val;
    };
};

AnalogIncrease ai (10, 1, 50, 5, 100);

// -----------------------------------------------------------------------------
void loop (){
    char s [40];
    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', s, sizeof(s));
        s [n] = 0;

        Serial.println (ai.update (atof(s)));
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.