NES controller w/ Accelerometer...

Hello all, I'm building an NES controller with a build in accelerometer. I based most of my source code and practicaly everyone else off:
3xw.ladyada.net/make/usbgamepad/index.html
(can't post link but I believe spoofing to show credit is acceptable)
So a big thanks for that guide!

I'm using teensy 2.0 and arduino 1.8 and the patch listed on the guide above.

My code will be posted below, but I'm running into 2 issues, which are probably easy to fix but I'm stumped!
First, with the code below everything works when I press the buttons, however, when I hold the start and select I want only the fcnmouse to loop, I don't want any keyboard inputs. Currently the mouse will only move when I hold both down (woooot!) but it also outputs keyboard letters (baaaad!).

The second issue, I've commented the code out, but the last part of the fcnmouse I want to use pB and pA to be left and right click...but for some reason it won't compile with mouse.click(1,0,0) I'm I useing the mouse.click wrong? I tried to find help but I can't seem to find it.

Also, any suggestions for making the mouse more fluent or anything else (Lady's guide says there's an easier way....but if there's something more effecient I'm all for it!)

Code:

const int pAnXin = 3;
const int pAnYin = 2;
const int pAnZin = 1;
const int pAnDumb = 0;

#define KEYREPEAT 100  // milliseconds

#define KEYDELAY 200 // delay from first to second character

const int pUp = 0;
const int pRight = 1;
const int pDown = 2;
const int pLeft = 3;

const int pB = 4;
const int pA = 5;

const int pSelect = 6;
const int pStart = 7;

const int pinLED = 11;


//Variables for the states of the NES buttons

byte buttons[] = {pUp, pRight, pDown, pLeft, pB, pA, pSelect, pStart}; 

byte keys[] = {KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H};

 

#define NUMBUTTONS sizeof(buttons)

 

//How far the accelerometer is tilted before the controller starts moving the mouse:
const int cintMovementThreshold = 18;

 

//The average zero acceleration values read from the accelerometer for each axis:
const int cintZeroXValue = 328;
const int cintZeroYValue = 328;
const int cintZeroZValue = 328;


//The maximum (positive) acceleration values read from the accelerometer for each axis:
const int cintMaxXValue = 396;
const int cintMaxYValue = 396;
const int cintMaxZValue = 396;

 
//The minimum (negative) acceleration values read from the accelerometer for each axis:
const int cintMinXValue = 256;
const int cintMinYValue = 256;
const int cintMinZValue = 256;

//The sign of the mouse movement relative to the acceleration:
const int cintXSign = -1;
const int cintYSign = -1;
const int cintZSign = 1;

//const float cfloatMovementMultiplier = 1;
//The maximum speed in each axis (x and y) that the cursor should move: 

const int cintMaxMouseMovement = 15;

//This reduces the 'twitchiness' of the cursor by calling a delay function at the end of the main loop.
const int cintMouseDelay = 6;


 

void setup() {

  //Analog Reference, use default
  analogReference( DEFAULT );

  //Setup the pin modes.
  pinMode( pinLED, OUTPUT );

  //Enable pullup resitor on the pins.
  for (byte i=0; i< NUMBUTTONS; i++) {

  pinMode(buttons[i], INPUT_PULLUP);

  }

}


 

void loop() {

  //Determine if chip will be used as a mouse or controller
  if (digitalRead(buttons[pStart]) == 0 && digitalRead(buttons[pSelect]) ==0) {
 
  //Process the accelerometer to make the cursor move.
  fcnMouse();

  }
  
  else {

  //Progess the NES controller buttons to send keystrokes.
  fcnController();

  }  

  //Delay to avoid 'twitchiness' and bouncing inputs due to too fast of sampling.
  delay(cintMouseDelay);

}

 

//Function to process the acclerometer as a mouse and send clicks
void fcnMouse()

{

  //Initialize values for the mouse cursor movement.
  int intMouseXMovement = 0;
  int intMouseYMovement = 0;

  //Read the dummy analog channel this must be done first because the X analog channel was first and was unstable, it dropped or pegged periodically regardless of pin or source.
  analogRead( pAnDumb );

  //Read accelerometer readings  
  int intAnalogXReading = analogRead(pAnXin);
  int intAnalogYReading = analogRead(pAnYin);
  int intAnalogZReading = analogRead(pAnZin);

  //Calculate mouse movement if the analog X reading is ouside of the zero threshold...
  if( cintMovementThreshold < abs( intAnalogXReading - cintZeroXValue ) ) {

    //...calculate X mouse movement based on how far the X acceleration is from its zero value.
    intMouseXMovement = cintXSign * ( ( ( (float)( 2 * cintMaxMouseMovement ) / ( cintMaxXValue - cintMinXValue ) ) * ( intAnalogXReading - cintMinXValue ) ) - cintMaxMouseMovement );

  }

  else {

    //Within the zero threshold, the cursor does not move in the X.
    intMouseXMovement = 0;

  }

 

  //If the analog Y reading is outside of the zero threshold... 
  if( cintMovementThreshold < abs( intAnalogYReading - cintZeroYValue ) )  {

    //...calculate Y mouse movement based on how far the Y acceleration is from its zero value.
    intMouseYMovement = cintYSign * ( ( ( (float)( 2 * cintMaxMouseMovement ) / ( cintMaxYValue - cintMinYValue ) ) * ( intAnalogYReading - cintMinYValue ) ) - cintMaxMouseMovement );
    //it could use some improvement, like making it trigonometric.

  }

  else {

    //Within the zero threshold, the cursor does not move in the Y.

    intMouseYMovement = 0;

  }


  Mouse.move(intMouseXMovement, intMouseYMovement);

 

//  //Left and right click for buttons
//
//  if (digitalRead(buttons[pB])==0) {
//
//  Mouse.click(1,0,0);
//
//  }
//
//  else if (digitalRead(buttons[pA])==0) {
//
//  Mouse.click(0,0,1);
//
//  }
//
//  else {Mouse.click(0,0,0);
//
//  }

 

}

 

//Function to process the buttons from the NES controller
void fcnController() {

  static long currentkey = 0;
  byte nothingpressed = 1;

  // run through all the buttons
  for (byte i = 0; i < NUMBUTTONS; i++) {
    
    // are any of them pressed?
    if (! digitalRead(buttons[i])) {

      nothingpressed = 0; // at least one button is pressed!
   

      // if its a new button, release the old one, and press the new one
      if (currentkey != keys[i]) {
        Keyboard.set_key1(0);
        Keyboard.send_now();
        Keyboard.set_key1(keys[i]);
        currentkey = keys[i];
        Keyboard.send_now();
        delay(KEYDELAY);
      } else {

        // the same button is pressed, so repeat!
        Keyboard.set_key1(keys[i]);
        Keyboard.send_now();
        delay(KEYREPEAT);

      }

    }

  }

  

  if (nothingpressed) {

    // release all keys
    Keyboard.set_key1(0);
    Keyboard.send_now();

  }

}