Go Down

Topic: Esplora Accelerometer instead of joystick (Read 68 times) previous topic - next topic

MakTibbles

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.

Quote
#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.
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy