? multiple calls to same pgm help

howdy, folks, happy december 15 (payday for me!) my question refers to multiple calls to the same program which reads from different analog input pins. essentially i'm using this to skinny my code down in an attempt to be less messy.

here is the code for the function that is called from my main loop whenever i need an analog read: it reads the pin, checks it against a threshold of 2, and if it passes, it divides it by 2 and outputs the value.

int knobFunction(int pin, int divisor)
{
  static int prevRead = 0;   
  int output = analogRead(pin)/divisor;         
  if (abs(output - prevRead) >= THRESHOLD)
    {prevRead = output;}
  else
    {output = prevRead;}
  return output/2;

rotary pots are hooked up to each analog input. what i'm worried about is this: if i'm moving more than 1 knob at the same time, my static ints are getting wiped out for the other knobs, correct? when i compile this and try it out, i THINK everything looks ok - except my values are flickering somewhat which makes me think that my threshold code is being hijacked by other analog ins.

if that is the case, would the alternative be to create different static ints for each value read from the pin? if so, does this threaten the neatness/efficiency i was looking for before? perhaps i should use arrays whose index number depends on the pin being read?

any thoughts on the matter would be appreciated. thank you for your time and consideration. nym

Make the knobs objects. The following outline is one possible implementation.

Code typed in off the top of my head, thus un-compiled and untested.

enum
{
      pinANALOG1    = ?         // assign analog pin number here!
    , pinANALOG2    = ?         // assign analog pin number here!
};

struct knob_t
{
    const uint8_t   _pin;
    
    int     _prevRead
    
    knob_t(byte pin) : _pin(pin), _prevRead(0)
    {
        // NOTE: can't remember - may not be required for analog input
        pinMode(_pin, INPUT);
    }

    int process(int divisor)
    {
        int     t = analogRead(_pin) / divisor;
        if ( abs(t - prevRead) >= THRESHOLD)
        {
            prevRead = t;
        }
        else
        {
            t = prevRead;
        }

        return (t / 2);
    }
};

knob_t      knob1(pinANALOG1);
knob_t      knob2(pinANALOG2);

void loop()
{
    ... some code ...

    int value_knob1 = knob1.process(divisor);
    int value_knob2 = knob2.process(divisor);

    ... some code ...
}

void setup()
{}

i’m afraid CS 110 didn’t cover objects… :frowning: that looks a little out of my league for now, unless you can point me to a good tutorial.

One of many on-line C+ tutorials http://www.cplusplus.com/doc/tutorial/

Keep in mind that the Arduino IDE supports development using the C++ language but does not provide any of the C++ Standard Library.

Post your complete code and we’ll work it out together.

perhaps i should use arrays whose index number depends on the pin being read?

That would certainly be my recommendation.

lloyddean - thanks, i have used that page before i started taking a class in C++, after i'd written some pathetic code for arduino (before i even knew that arduino language is essentially C++...!! man, that was ugly, every variable was a global variable... yeesh)

also, thanks for your generous offer to walk me through it. however, i did this:

int knobFunction(const int pin, int divisor)
{
  static int prevRead[6] = {0, 0, 0, 0, 0, 0};   
  int output = analogRead(pin)/divisor;         
  if (abs(output - prevRead[pin]) >= THRESHOLD)
    {prevRead[pin] = output;}
  else
    {output = prevRead[pin];}
  return output/2;
}

and it worked perfectly on the first try. objects are just around the corner for me, and when i tackle it, i know where to go.

i never cease to be impressed with the knowledge and generosity of people on these forums.

nym,

Congratulations I'm glad you found something that worked for you.

Sorry but I chose to answer with a solution that I thought was the most elegant instead of inquiring as to your current knowledge level and going from there; bad habit on my part.

When, and if, you're ever ready to continue learning C++ feel free to leave me a message here and I'll help however I am able.

Good luck with classes ;)

lloyd: i strive to eventually achieve elegance, but in the meantime my programs will be at best a pretty little pink piglet wearing lipstick. thanks for the pearls, though. my next class in the series deals more with objects and classes. my frustration with the classes is that only about half (eh, maybe 2/3rds) really relate directly to what i'm doing with arduino (interesting and atypical midi controllers for live music performance) but it's still worthwhile.