Overview
In this lesson, we will make a star wars game based on the Processing and play the game with a PS2 joystick and a button connected to the Arduino UNO.
Components
- 1 * Arduino UNO
- 1 * USB Cable
- 1 * Button
- 1 * PS2 Joystick
- 1 * Breadboard
- Several jumper wires
Principle
The experiment consists of two parts: first, acquire the data from Arduino; second, process the data.
Play the Star Wars Game:
① When you press the button, the fighter aircraft will fire bullets
② When you operating the PS2 Joystick, you can change the position of movement of the fighter aircraft.
Note:
1. In this experiment, my Arduino UNO board is connected to my computer port COM26. But it may differ in your case. So please adjust it according to your actual situation.
2. If the Processing does not run normally, you may need to install the related function libraries.
Procedures
Step 1: Build the circuit
Step 2: Program
_37_Button_PS2Joystick_Processing.ino
/*********************************************************** File name: 37_Button_PS2Joystick_Processing.ino Description: Arduino and processing interactive Button and Joystick control Star Wars game Website: www.adeept.com E-mail: support@adeept.com Author: Tom Date: 2015/05/02 ***********************************************************/ int xpotPin = 0; // Define UNO board A0 pin connect the Ioystick x int ypotPin = 1; // Define UNO board A1 pin connect the Ioystick y int btnPin = 9; // Define UNO board digital 9 pin connect the button int xval = 0; int yval = 0; int btnval= 0; void setup() { // Start the serial port, baud rate to 9600 Serial.begin(9600); pinMode(btnPin,INPUT_PULLUP);//Set btnPin inputand pull up } void loop() { // Read the Joystick x y and button information int xval = analogRead(xpotPin); int yval = analogRead(ypotPin); int btnval = digitalRead(btnPin); Serial.print(xval);//Dend xval value Serial.print(','); Serial.print(yval);//Dend yval value Serial.print(','); Serial.print(btnval);//Dend btnval value Serial.println(','); delay(100); }
Step 3: Compile the program and upload to Arduino UNO board
Step 4: Run the Processing software (Processing_PS2Joystick_Button.pde)