Thanks for the replies.
I think you are a bit confused with your code
void readPotentiometers(int A)
A = analogRead(potA);
This receives an integer (from whatever called the function) and saves it into a variable called A.
The next line promptly replaces A with something obtained from analogRead().
That means that whatever information was passed to the program is lost.
You should save analogRead() to another variable (perhaps potPosition ?)
Then you can create an IF statement that does X or Y depending on the value of A.
Ha. You’re right. I tried to throw together an example of what I was try to accomplish before I headed out this morning. I failed, as this part makes no sense. Rather than create a new variable inside the function, I was looking to insert a previously created variable at this location in the function. I’m thinking this isn’t something that’s possible with this approach though.
a static local variable is remembered over function calls
static int prevValue = -1;
int value = analogRead(potA);
value = constrain(value, 40, 980);
if ((prevValue + 20) < value || (prevValue - 20) > value)
prevValue = value ;
value = map(value , 40, 980, 0, 255);
Serial << "New value: " << value << endl;
I didn’t know about “static local variable”. That will be very useful.
There are a few ways to get the same effect.
One way is to combine all the state data that your function needs into a struct that you pass in by reference/pointer. This was the function can access whatever parameters you want specific to each input. This seems the best way to me, and is a good step on the path towards a clean object oriented design. (The next step would probably be to convert the struct into a class, and convert the function into a method of that class.)
Another way is to pass all the values the function wants, with pass-by-reference semantics used to use some of them as in+out parameters where necessary.
I hope that someday I understand everything in your post =) It seems like great advice.
Sorry everyone for the hastily and poorly demonstrated question. Using static local variables as suggested above, I think I’ve found a way to accomplish what I was trying to do.
Thanks for the help.