Having eForth running on my Arduino testing some I/O is what i wanted. Some years ago i posted
https://blog.jeronimus.net/2017/04/funduino-joystick-shield.html about an Arduino joystick / button shield. This cheap shield has a joystick some input buttons. (And some connection headers that i did not use in this test.)
There are several versions of this board. I have the Funduino V1.A The shield gives your Arduino the functionality found on the old Nintendo controllers. It can be used for gaming or control hardware.
2 analog inputs and some digital Ideal to start with this this shield with eForth to do some simple input tests.
The table below lists how the buttons are connected to the Arduino
Arduino | Button | Port&bit |
D2 | Key A UP | PD2 |
D3 | Key B RIGHT | PD3 |
D4 | Key C DOWN | PD4 |
D5 | Key D LEFT | PD5
|
D6 | Key E Start | PD6 |
D7 | Key F Select | PD7 |
D8 | Analog button | PB0 |
A0 | AnalogX Joystick | Analog0 |
A1 | AnalogY Joystick | Analog1 |
The ForthArduino1.pdf contains several I/O examples that can be tested with this board:
For the analog ports
Paragraph 3.5 mentions
If you connect an external analog signal source to the A0 pin, then type the following commands to read its analog value:
27 C! \ setup A0 as input pin, which is on PC-0 port
1 28 C! \ turn on pull-up resister on A0 pin
40 7C C! \ setup reference and multiplexer inputs
C3 7A C! \ start conversion
78 ? \ display results 78 = low byte 79 = high byte
Example for digital port ( D8 PB0 button of joystick)
0 24 C! \ make all Port B pins input
1 25 C! \ turn on pull-up resistor for Line 8
23 C@ . \ read PINB port and show its contents
23 C@ . \ repeat with switch on and off
What i especially like about eForth on Arduino is that using the terminal you can interactively test and play with the system to adapt it to your own needs.
The AVR Family databook / ATmega328P datasheet contains detailed info about all the internal registers. Also the multiple functions of the pins are explained.
Also you can find the info that i summarized in the table below.
| Data register
| Data direction register | Input pin address
|
PortB | PORTB | DDRB | PINB |
D8 Analog button | 0x05 (0x25) | | 0x03 (0x23) |
PortC | PORTC | | PINC |
| 0x08 (0x28) | 0x07 (0x27) | 0x06 (0x26) |
PortD | PORTD | | PIND |
D2-D7 Key A - F | 0x0B (0x2B) | 0x0A (0x2A) | 0x09 (0x29) |
|
|
|
|
For reading the other buttons you need to read portD.
0 2B C! \ make all Port B pins inputFF 2A C! \ turn on pull-up resistor for all lines
29 C@ . \ read PINB port and show its contents
Some simple words to play with the shield and print the output :
: .PD 0 2A C! FF 2B C! 29 C@ . ; / Print PortD ( Buttons)
: .PB 0 24 C! 1 25 C! 23 C@ . ; / Print PortB ( Joystick button)
: .PX 27 C! 1 28 C! 40 7C C! C3 7A C! 78 ? ; / Print Joystick X value
: .PY 27 C! 1 28 C! 41 7C C! C3 7A C! 78 ? ; / Print Joystick Y value
: .JOY .PB .PX .PY ; / Print all Joystick values
I used short cryptic words like .PB. More descriptive would be .PrintPortB, For playing with the system it would require more typing more characters.
In Forth it is also possible to change the output to binary to easy see the bits corresponding to each button:
2 BASE ! / Set Binary output
HEX / Set Hexadecimal output
Hope this Arduino Forth ( eForth) introductory blog article can be help others starting to use Forth on Arduino.