Showing posts with label JoystickShield. Show all posts
Showing posts with label JoystickShield. Show all posts

Thursday, November 19, 2020

FORTH on Arduino -3- (Funduino Joystick game shield)

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 

ArduinoButtonPort&bit
D2Key A UPPD2
D3Key B RIGHTPD3
D4Key C DOWNPD4
D5Key D LEFTPD5
D6Key E StartPD6
D7Key F SelectPD7
D8Analog buttonPB0
A0AnalogX JoystickAnalog0
A1AnalogY JoystickAnalog1


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

PortBPORTBDDRBPINB
D8
Analog button
0x05 (0x25)
0x04 (0x24)
0x03 (0x23)
PortCPORTC
DDRC
PINC

0x08 (0x28)0x07 (0x27)0x06 (0x26)
PortDPORTD
DDRD
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 input
FF 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.

Saturday, April 8, 2017

Funduino Joystick shield

Funduino Joystick shield

From Aliexpress i got a Arduino Gamepads JoyStick KeypadShield PS2  (I recently found it at another seller for 2,33 Euro ) 


Using Google i found out there are some different versions.
My shield  (V1.A ) uses the following connections

D2 Key A UP
D3 Key B RIGHT
D4 Key C DOWN
D5 Key D LEFT

D6 Key E Start
D7 Key F Select

D8 Analog button
A0 AnalogX Joystick
A1 AnalogY Joystick

There are some headers to get to the other Arduino pins.

Some headers are pre arranged for special interfaces (On my board I2C, Bluetooth, Nokia5110 display, nRF24L01)
A slide switch to select between 3V3 and 5V mode.
A red power led

TestScript

/*
JoystickArduinoBasicExample

A basic sketch to demonstrate reading values from the joystick shield

How to use:
* Connect joystick shield to your Arduino
* Upload this sketch to your Arduino
* Open the Arduino IDE Serial Monitor (set to 9600 baud)
* Waggle joystick, push buttons

Requires:
* Arduino
* Joystick Shield

April 2017 by JJ adapted version of a version
written for SparkFun Arduino Inventor's Kit CIRC-JOY
Based on original example by Ryan Owens
*/

// Store the Arduino pin associated with each input

const byte PIN_BUTTON_UP = 2;
const byte PIN_BUTTON_RIGHT = 3;
const byte PIN_BUTTON_DOWN = 4;
const byte PIN_BUTTON_LEFT = 5;

const byte PIN_BUTTON_START = 6;
const byte PIN_BUTTON_SELECT = 7;

const byte PIN_BUTTON_JOY = 8; // Select button is triggered when joystick is pressed
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;

void setup() {
Serial.begin(9600);
// Specify each pin connected to a pushbutton as an input.
// Also enable the Arduino's internal "pull-up" resistors
// for each pushbutton we want to read--this means the shield
// doesn't need to have resistors on it.
// Note that when a pull-up resistor is used on a pin the
// meaning of the values read are reversed compared to their
// usual meanings:

// * HIGH = the button is not pressed
// * LOW = the button is pressed

pinMode(PIN_BUTTON_RIGHT, INPUT);
digitalWrite(PIN_BUTTON_RIGHT, HIGH);

pinMode(PIN_BUTTON_LEFT, INPUT);
digitalWrite(PIN_BUTTON_LEFT, HIGH);

pinMode(PIN_BUTTON_UP, INPUT);
digitalWrite(PIN_BUTTON_UP, HIGH);

pinMode(PIN_BUTTON_DOWN, INPUT);
digitalWrite(PIN_BUTTON_DOWN, HIGH);

pinMode(PIN_BUTTON_START, INPUT);
digitalWrite(PIN_BUTTON_START, HIGH);

pinMode(PIN_BUTTON_SELECT, INPUT);
digitalWrite(PIN_BUTTON_SELECT, HIGH);

pinMode(PIN_BUTTON_JOY, INPUT);
digitalWrite(PIN_BUTTON_JOY, HIGH);
}

void loop() {
// Print the current values of the inputs (joystick and
// buttons) to the console.
Serial.print("l:");
Serial.print(digitalRead(PIN_BUTTON_LEFT));
Serial.print(" ");
Serial.print("r:");
Serial.print(digitalRead(PIN_BUTTON_RIGHT));
Serial.print(" ");

Serial.print("u:");
Serial.print(digitalRead(PIN_BUTTON_UP));
Serial.print(" ");

Serial.print("d:");
Serial.print(digitalRead(PIN_BUTTON_DOWN));
Serial.print(" / ");

Serial.print("g:");
Serial.print(digitalRead(PIN_BUTTON_START));
Serial.print(" ");

Serial.print("s:");
Serial.print(digitalRead(PIN_BUTTON_SELECT));
Serial.print(" / ");

Serial.print("j:");
Serial.print(digitalRead(PIN_BUTTON_JOY));
Serial.print(" ");

Serial.print("x:");
Serial.print(analogRead(PIN_ANALOG_X));
Serial.print(" ");
Serial.print("y:");
Serial.print(analogRead(PIN_ANALOG_Y));
Serial.print(" ");

Serial.println();
}

Links