ebeowulf17
- Joined Aug 12, 2014
- 3,307
Unless I'm misreading this, when you capture data from the encoder value and save it as w1 or w2, you're starting with the raw encoder value, then limiting its max value with a modulo function, then multiplying it by 10:used this
This would just reset all to zero.C:switch (select) { case0: noInterrupts(); Counter1 = w1Value/10; interrupts(); break; case1: noInterrupts(); Counter1 = w2Value/10; interrupts(); break;
But why divide them 10 ? We are forcing it take the value of each variable, right ?
w1Value =readEncoder() %20*10;
w2Value =readEncoder() %50*10;
So, if your Counter1 encoder variable was 3, what you would store as w1Value is 30. Therefore, if you're later copying w1Value back into Counter1, you need to divide by 10 to end up with 3 again. If you don't divide by 10, and you copy 30 back in as the Counter1 value, then the next attempt to calculate w1Value from Counter1 will be 30 modulo 20 times 10. That works out to 100, which will bear no obvious relationship to the 30 you're expecting to see.
What makes you think everything will reset to zero? Did you run it and get zero every time, or is that just what you'd expect from the code? If it's actually giving you zero each time with my recommended code, then I've obviously gotten myself turned around somewhere!