In this project, I use analog joystick as a game control device for Atari’s Lunar Lander game on a web browser. The controller supports the following functions:

1) A switch changes two modes that the joy stick is either a mouse when outside of the game, or the controller during the game. 2) Before game starts, hit the switch button to active joy stick to move the mouse and click. 3) In the game, joy stick operates to place key board input to control the space space.

The three technique tasks are 1) using button to indicate change of state to activate different modes; 2) analog read from joystick x-axis and y-axis to drive mouse, and joystick click as left click of the mouse; 3) analog read from joystick x-axis and y-axis to press keyboard(‘A’,’D’,’W’,’S’) and click to replace space bar. (see code below)

To have keyboard function, I used Arduino MKR1000 instead of Arduino Uno. MKR 1000 also have WIFI functionality so it is possible to create a wireless controller eventually. This microprocessor operates on 3.3v, which is different from Uno (5v)IMG_4637.JPG

 

This was my first time dealing with a different Arduino and joystick, and there are several things I learned from this project:

  1. We need to really understand ‘control’ when developing a controller. The original keyboard input are letters, but they do different things. You might want to start questioning: What does it actually do belong press one key? What kind of control?  Is this control a switch, or maneuvering? Is this control more as a digital input or analog input? During the developing phase I was solely focused on replacing ‘press key’ action without thinking carefully about these questions.
  2. Try to avoid modes. If there are modes, certain indication (LED lights for instance) will be necessary to help the users to understand which mode they are in.
  3. Have better housing.

img_4639

 

#include <Keyboard.h>

int xPin = A0;
int yPin = A1;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

int previousState = HIGH;
int count = 0;

#define potPin 3

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);

//activate pull-up resistor on the push-button pin
pinMode(buttonPin, INPUT_PULLUP);
pinMode(potPin, INPUT_PULLUP);
}

void loop() {
int button = digitalRead(potPin);
if (button != previousState) {
delay(10);
if (button == LOW) {
Serial.print(“Mouse On “);
}
}
previousState = button;
xPosition = analogRead(xPin);
delay(1);
yPosition = analogRead(yPin);
delay(1);
buttonState = digitalRead(buttonPin);

//Serial.print(“X: “);
//Serial.print(xPosition);
//Serial.print(” | Y: “);
//Serial.print(yPosition);
//Serial.print(” | Button: “);
//Serial.println(buttonState);

if (buttonState == 0){
Keyboard.press((char) 32); //push button to hit spacebar;
delay(1);
Keyboard.release((char) 32);
}

if (xPosition == 0){
Keyboard.press(‘a’); //push button to hit spacebar;
delay(1);}

if (xPosition > 0 ){
Keyboard.release(‘a’);
}
if (xPosition > 1000){
Keyboard.press(‘d’); //push button to hit spacebar;
delay(1);
}
if (xPosition > 500 && xPosition < 1000){
Keyboard.release(‘d’);
}

if (yPosition == 0){
Keyboard.press(‘s’); //push button to hit spacebar;
delay(1);
}
if (yPosition > 0){
Keyboard.release(‘s’);
}

if (yPosition > 1000){
Keyboard.press(‘w’); //push button to hit spacebar;
delay(1);
}
if (yPosition < 1000){
Keyboard.release(‘w’);
}
delay(20); // add some delay between reads
}

 

Advertisements