¿ªÔÆÌåÓý

Re: W8TEE vft/tft questions


Jack Purdum
 

It appears that your encoder sends out 4 pulses on each detent. I'm not where I can test the code, but try something like this:

void SetEncoderDirection() {
??? int count = 0;

? ? encoderDirection = 0;
????
????newPosition = rotary.read();
??? if (newPosition == 0)
??????? return;

?? while (count < 4) {??????? // Ignore 3 readings

? ????newPosition = rotary.read();
?? ?? if (newPosition != oldPosition) {
? ? ? ?? if (newPosition > oldPosition)
? ? ?????? encoderDirection = 1;
? ?????? else
? ? ????? encoderDirection = -1;
? ????? oldPosition = newPosition;
???? }
??? count++;
}

The idea is to throw away all but the last direction read. Like I said, you'll need to play with it. Also, it will be easier to read your debug code if you don't put things on a new line. Use something like:

#ifdef DEBUG
?if (newPosition != oldPosition) {
? ? ?Serial.print("In SetEncoderDirection old = " );
???? Serial.print(oldPosition);????????????????????????????????????????// No newline here
? ?? Serial.print("?? new = " );
???? Serial.println(newPosition);
?}??????????????????????????????????????????????????????? // Move this one from here....
#endif

If DEBUG is #defined at the top of the file, the debug statements are compiled into the sketch. Comment the #define DEBUG line, and they are no long in the executable, but still can be turned on again if need be.

Jack, W8TEE

On Wednesday, May 23, 2018, 6:13:07 PM EDT, tom.mccobb@... <tom.mccobb@...> wrote:


Hi Jack, thanks for answering.

I have been watching the debug code you mentioned, and the output has changed since things went south.

I added some Serial.print statements of my own.? After watching the debug output for a while, I wanted to know what was going on in SetEncoderDirection:

void SetEncoderDirection() {
? encoderDirection = 0;
? newPosition = rotary.read();
?if (newPosition != oldPosition) {
? ? ?Serial.print("Old: " );
? Serial.println(oldPosition);
? ?Serial.print("New: " );
? Serial.println(newPosition);
?}??????????????????????????????????????????????????????? // Move this one from here....
? if (newPosition != oldPosition) {
? ? if (newPosition > oldPosition)
? ? ? encoderDirection = 1;
? ? else
? ? ? encoderDirection = -1;
? ? oldPosition = newPosition;
? }
}

...and also here, at the top of your debug block:

#ifdef DEBUG?
?Serial.print("Direction: " );
? Serial.println(encoderDirection);? ? ? ? ?
Serial.print("Examine Fast Tune: Start = ");? ? ? ? // May be useful for setting Fast Tune rate
Serial.print(fasttuneStart);
Serial.print("? ?end = ");
Serial.print(fasttuneEnd);
Serial.print("? diff = ");
Serial.println(fasttuneEnd - fasttuneStart);
#endif?

With this in place, I turned the encoder one detent stop, and you see that SetEncoderDirection was called three times.? I kept doing this, slowly, and you can see from the serial monitor output below that even though I am turning the encoder int he same direction, the encoderDirection is variously 1 and -1.? Every 3rd turn the frequency is just not updated at all, but with the vacillating encoderDirection, the freq. goes up 100Hz and down 100Hz.? I thought maybe a bad solder connection was causing a kind of "echo" in the circuit, so I re-flowed the solder to pins 18, 19 and 20.? I have tried a couple of encoders with the debounce caps installed (but who knows, maybe I have a drawer full of bad ones).? I was reading your followup article on encoders and when I got to the end where you suggest always to look for the last thing you think might be wrong, I even changed the connection wires.? Last thing? that I can think of is to swap out the Arduino mini, which I will get to in a day or so (ugh, all those header pins to solder).? If you can suggest anything else, let me know.? Serial output follows:

First turn here:
Old: -1
New: 0
Old: 0
New: -1
Old: -1
New: 0
Direction: 1
Examine Fast Tune: Start = 3324? ?end = 11398? diff = 8074
Old: 0
New: -1
Old: -1
New: 0
Old: 0
New: -1
Direction: -1
Examine Fast Tune: Start = 11539? ?end = 31281? diff = 19742
Old: -1
New: 0
Old: 0
New: -1
Old: -1
New: 0
Direction: 1
Examine Fast Tune: Start = 31422? ?end = 40482? diff = 9060
Old: 0
New: -1
Old: -1
New: 0
Old: 0
New: -1
Direction: -1
Examine Fast Tune: Start = 40623? ?end = 48869? diff = 8246
Old: -1
New: 0

Join [email protected] to automatically receive all group messages.