Of course, after an afternoon of experimenting with an Arduino and KY-040 Rotary Encoder Board, the question of how to use a Rotary Encoder attached to an ATARI joystick port became an intriguing ponderance. After all, the Atari can read a trackball and mouse containing two encoders. How hard can it be to read one?
For this first experiment KISS was an objective. The objective was to be able to read the input and determine if the knob was being turned in a CW direction or CCW direction. Also, the encoder has a switch that is activated when the knob is pushed in. A simple working BASIC program should yield enough information for development of more advanced methods of use.
First I'll explain getting an encoder hooded up to the joystick port. Then a short explanation of how the encoder works and how it will effect the joystick port reading. This should make the example program a little easier to understand.
I started with a bunch of KY-040 Rotary Encoder Boards for the Arduino. This board has the encoder and 3 pull down 10K resisters required by the Arduino. So my first encoder was de-soldered from the board and pins were added for insertion into the breadboard.
(I just received an order of cheep EC11 encoders. They are not as beefy. They will work and no de-soldering.)
The wiring is easy if you have an old joystick cable.
Pin1 - Clock
Pin2 - Data
Pin6 - Pushbutton
Pin8 - Ground for the three switches
Bread boards and hookup wires were used in this first attempt. Future needs will definitely dictate further hardware improvements.
Search the net for Rotary Encoder to gain a full explanation of how they work. Then there is this way of thinking about it…..
The encoder has 2 switches that are offset to achieve a slight difference in activation time. Before the knob is rotated both switches are off. Turn the knob one click and one switch turns on before the other. Turn the knob the other way and the other switch turns on before the one switch. Take a look at these two equations.
X = A - B - C
X = A - C - B
When the calculation is complete both equations are equal. BUT…After the first subtraction the values will be different, if B is not equal to C. If you can determine the value of X after the first subtraction you can state the direction of rotation.
A - the neutral position = 15
B - Clock Switch = 1
C - Data Switch = 2
The STICK(n) values of the logic table turn out to be:
Since only one switch can be first, the result of 14 or 13 will tell you which direction the knob was turned and the program can act accordingly.
There are timing problems if the program runs slowly. (Kind of like BASIC slow) Turn the knob fast enough and you run the risk of jumping from 15 to 12 before polling for the value again (I can see a DLI routine in my future).
As it happens the two rotary switches are tied to pins that control bits 1 and 2 of the Joystick port. 1 being the clock and 2 being data switch. Atari sets the joystick pins to HIGH for initial value of 15. Turning on a switch grounds the pin to LOW and the value of the bit is subtracted from 15. (It’s a reverse logic situation where switch ON = LOW.)
The push knob switch is wired into the trigger pin and operates just like the trigger button and has no effect on the STICK(0) values.
1 REM ROTARY ENCODER-PROOF OF CONCEPT
3 REM BY Kevin Packard 5/2021
5 REM EC11 encoder with switch, hooked
6 REM to Joystick Port1.
7 REM PIN 1 -> Clock
8 REM PIN 2 -> Data (DT)
9 REM PIN 8 -> Both ground pins
10 REM PIN 6-> Trigger
110 SOUND 0,0,0,0
120 IF STRIG(0)=0 THEN SOUND 0,60,10,15
140 IF S1=15 THEN GOTO 170
150 IF S1=14 AND LAST=15 THEN ? "CW"
160 IF S1=13 AND LAST=15 THEN ? "CCW"
180 GOTO 110
The program displays the direction of rotation and makes a sound when the push button is activated. There's more REM then program lines.
100 sets up neutral value of S1
110 turns off sound
120 checks trigger and makes sound if pushed
130 Gets the present value of STICK(0) - Works best if polled once/cycle.
140 S1 in neutral position?(no rotation) save some time goto line 170
150 Value change of S1 prints CW to screen
160 Value change of S1 prints CCW to screen
170 Changes last to S1 - Keep lines 150 and 160 from execution until LAST reset to 15
180 Go Back to 110 and loop until break key pressed.
Also note that there is no action taken if S1 and/or LAST = 12.
Does the world really need another Etch-A-Sketch program. I think not. How about a microKorg patch editor based on the MIDI control codes and operating in the Diamond GOS environment . Now that's a definite maybe.