Esplora Accelerometer instead of joystick

Hello Everyone,

I'm trying to make a game controller so instead of using the joystick you tilt the controller.
It's just a simple X-axis = left right set up but I'm still fairly new and it's giving me grief.

Currently I haven't got the Axis to work at all and the code is churning out 'W' constantly I have no idea why.

#include <Esplora.h>

void setup()
{
Serial.begin(9600);
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 leftright = Esplora.readAccelerometer(X_AXIS);
int updown = Esplora.readAccelerometer(Y_AXIS);

//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

Serial.print("x: "); // print the label for X
Serial.print(xAxis); // print the value for the X axis
Serial.print("\ty: "); // print a tab character, then the label for Y
Serial.print(yAxis); // print the value for the Y axis

delay(500);

//buttons
if(up == LOW)
{
Keyboard.press('X');
Keyboard.release('X');
}

if(down == LOW)
{
Keyboard.press('C');
Keyboard.release('C');
}

if(left == LOW)
{
Keyboard.press('V');
Keyboard.release('V');
}

if(right == LOW)
{
Keyboard.press('B');
Keyboard.release('B');
}

if (updown > -100)
{
Keyboard.press('KEY_UP_ARROW');
Keyboard.release('KEY_UP_ARROW');
}

if (updown < 100)
{
Keyboard.press('KEY_DOWN_ARROW');
Keyboard.release('KEY_DOWN_ARROW');
}

if (leftright < 100)
{
Keyboard.press('KEY_LFET_ARROW');
Keyboard.release('KEY_LEFT_ARROW');
}

if (leftright > -100)
{
Keyboard.press('KEY_RIGHT_ARROW');
Keyboard.release('KEY_RIGHT_ARROW');
}

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

GameController.ino (1.61 KB)

what if you change your leftright and updown conditions:
if (leftright > 100), if (updown > 100) [instead of if... < 100]
if (leftright < -100), if (updown > 100) [instead of if... > -100]

I guess you lost interest, found other things to do, or whatever.
I wrote the following sketch to confirm my postulate and to check out the SIP strip I/O.
0 is neutral; as the nose rises from neutral Y-axis outputs positive and as the nose dips Y-axis outputs negative.

It lights one of three external LEDs based on the pitch (Y-axis) of the Esplora --

#include <Esplora.h>
const byte pospitch = 1;
const byte negpitch = 0;
const byte neut = 8;
void setup()
{
  Serial.begin(9600);        // initialize serial communications with your computer
  
  pinMode (pospitch, OUTPUT);
  pinMode (negpitch, OUTPUT);
  pinMode (neut, OUTPUT);
  
}
void loop()
{
  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
  
  if (yAxis > 80)
  {
    digitalWrite (pospitch, HIGH);
    digitalWrite (negpitch, LOW);
    digitalWrite (neut, LOW);
  }
  if (yAxis < -20)
  {
    digitalWrite (pospitch, LOW);
    digitalWrite (negpitch, HIGH);
    digitalWrite (neut, LOW);
  }
  if ((yAxis < 80) && (yAxis > -20))
  {
    digitalWrite (pospitch, LOW);
    digitalWrite (negpitch, LOW);
    digitalWrite (neut, HIGH);
  }
  delay(100);              // wait half a second (500 milliseconds)
}

It seems that the output isn't symmetrical (degrees +/- : output +/-)