Creating a controller. Total noob. Needing of a spoonfeed.

Hey all.

I’m completely new to the world of microcontrollers. I have a couple Arduino Uno boards and I finally got my Esplora in the mail. Only problem is. I have no clue how to remap the controller inputs.

I’m a low level game designer and I love all of the potential for interactivity I can squeeze out of this controllers features.

What I’m trying to do, is remap the joystick. I want to make the controls “W,A,S,D” for forward backward and side to side movement of the stick and “L Shift” on click of the joystick.

I would like the accelerometer to imitate mouse movement (basically like having a second joystick, so the real joystick controls movement within a game and the accelerometer controls “Looking around”)

For the 4 push buttons on the pad I would like one to be “Left mouse button”, one to be “M”, one to be “E” and the last to be “Space”. I can remap which input goes to which button if I can see the code…

When the mic picks up sound I want it to input “F”

So, if I can get my Esplora to work in these ways then I can review the code and get it to do whatever I like in the future. But I need help to get it to this point. If someone could knock the code out for me that would be fantastic. But I would benefit from someone explaining what I should do for me or a tutorial would be great also. I looked at the Example sketches and… That hasn’t helped much. I hope you guys can!

I’m actually doing something similar to this. I had to adapt some of my code, but here is everything but the mouse movement because that is quite complicated.

I’m pretty sure this code will work:

#include <Esplora.h>

void setup()
{
  Serial.begin(9600);
  Mouse.begin();
  Keyboard.begin();
} 

void loop()
{
  int right = Esplora.readButton(SWITCH_RIGHT);
  int left = Esplora.readButton(SWITCH_LEFT);
  int down = Esplora.readButton(SWITCH_DOWN);
  int up = Esplora.readButton(SWITCH_UP);
  int mic = Esplora.readMicrophone();
  int lshift = Esplora.readJoystickButton();
  int leftright = Esplora.readJoystickX();
  int updown = Esplora.readJoystickY();
  
  //these are the values for the accelerometer. 
  int xAxis = Esplora.readAccelerometer(X_AXIS);    // read the X axis
  int yAxis = Esplora.readAccelerometer(Y_AXIS);    // read the Y axis
  int zAxis = Esplora.readAccelerometer(Z_AXIS);    // read the Z axis
   
  //buttons
    if(up == LOW)
  {
    Keyboard.press('m');
    Keyboard.release('m');
  }

    if(down == LOW)
  {
    Keyboard.press('e');
    Keyboard.release('e');
  }
   
    if(left == LOW)
  {
    Keyboard.press(' '); 
    Keyboard.release(' ');
  }
   
    if(right == LOW)
  {
     Mouse.click(MOUSE_RIGHT);  //this will press and release once. You will need to recode to be able to hold it down. 
  }

  //mic
  if (mic > 350)    //edit 350 for the amplitude of the sound. (higher number means it will be triggered by a louder sound)
  {
    Keyboard.press('f');  
    Keyboard.release('f');
  }

  //joystick
  if (lshift == LOW)
  {
    Keyboard.press(KEY_LEFT_SHIFT);
    
    // this will simply press and release. if you wish to have it be held down, add a delimiter here.
    
    Keyboard.release(KEY_LEFT_SHIFT); 
  }
  
  if (updown > 450)
  {
    Keyboard.press('w');
    Keyboard.release('w');
  }

  if (updown < -450)
  {
    Keyboard.press('s');
    Keyboard.release('s');
  }

  if (leftright < -450)
  { 
    Keyboard.press('a');
    Keyboard.release('a');
  }

  if (leftright > 450)
  { 
    Keyboard.press('d');
    Keyboard.release('d');
  }


  delay(1); //amount of time delay per loop, measured in milliseconds.
}

A problem you will run into is how long you want to have something pressed down. This code loops over and over every millisecond and presses and releases a button when it is down. For example, if the left shift key is used as a crouch, even if the user holds the joystick down, the shift will be pressed and released every time, and you will end up with a high-speed teabag. To fix this, add a delimiter to indicate when to release the key, such as:

  //Pause on/off

  int pause = Esplora.readButton(SWITCH_RIGHT);

  if(pause == LOW)
  {
    Keyboard.press(KEY_ESC);     //will be held until the button is released.
  }
  if(pause == HIGH)
  {
    Keyboard.release(KEY_ESC);
  }

I am kind of scared of the mouse control thing, but the variables for the accelerometer are included in the code. I am a bit of a noob too, but if you have questions I can try and answer them.

This might help with mouse movement: http://forum.arduino.cc/index.php?topic=139102.0