Guys,
Thanks for the answers so far, but I'm still not getting any result.
So here's the thing I want to do:
I want to calculate if an encoder has rotated and in which direction it rotated. When I know if the pot has rotated I calculate the real value of the pot.
The encoders are read via a shiftIn function. I end up with a byte. Now in total I will have 4 bytes as there are lot's of pot's involved. bit0&1, bit2&3, bit4&5 and bit6&7 represent an encoder.
So I want to create a function I can call for each byte and that results in an array, 4 long, contiaining the pot values.
For the direction detection I need the byte containing the encoder position and the previous byte containing the previous encoder position. I read the 2 bit's for old value and new value, compare them and decide of the encoder was turned CW or CCW. If CW I write a +1 value in the encoders array, els CCW is a -1. The real value of the pot is than calculated by taking the previous value of the pot and adding it with the encoders array. This I do for every set of bit's and thus the output of the function will be an array.
Next step, how I think, posibly completely wrong;
So I first read the rotary encoders giving me following bytes: switchVar1, switchVar2, switchVar3 and switchVar4
than I do
valuesPot1 = myPotCalcFunction(byte switchvar1, byte OLDswitchVar1, int OLDvaluesPot1);
valuesPot2 = myPotCalcFunction(byte switchvar2, byte OLDswitchVar2, int OLDvaluesPot2);
valuesPot3 = myPotCalcFunction(byte switchvar3, byte OLDswitchVar3, int OLDvaluesPot3);
valuesPot4 = myPotCalcFunction(byte switchvar4, byte OLDswitchVar4, int OLDvaluesPot4);
with valuesPot1 an array[4] and OLD valuesPot1 an array [4]
My function than looks like this:
int myPotCalcFunction (byte switchvar, byte OLD switchVar, intOLDvaluesPot)
{ for each couple of bit's bit0&1, bit2&3, bit4&5, bit6&7
calculate rotation, calculate potvalue;
return potvalue, oldencoders and old switchvar}
Now, I need to create the OLDswitchVar in this function as I only change the bit pairs when a movement is detected.
I hope this makes some sense. I'm really wondering if this is the way to go. Maybe there is a different approach for these kind of things?
Anyway;
Why are you returning them in the first place? They are global variables.
is this also true if you want to have the function run for 4 times with different variables?
I tried removing the returns, but no change.
Cheers,
ToAd