AlanChatham / UnoJoy

UnoJoy! allows you to easily turn an Arduino Uno (or Mega or Leonardo) into a PS3-compatible USB game controller
GNU General Public License v3.0
482 stars 130 forks source link

Accelerometer Joystick #22

Open mer34 opened 6 years ago

mer34 commented 6 years ago

Hi,i want to use accelerometer as a joystick.So, i created 2 program.One for the unojoy program and the other for the accelerometer conversion.I created 2 program because of that one arduino is powerless to manage the unojoy programm and the accelerometer conversion. But when i switch to joystick the accelerometer is not recognizes.

Arduino program ``

include "UnoJoy.h"

include

SoftwareSerial mySerial(13,12); // RX, TX

void setup(){ setupPins(); setupUnoJoy();

mySerial.begin(9600); //Serial.begin(9600); }

void loop(){ // Always be getting fresh data dataForController_t controllerData = getControllerData(); setControllerData(controllerData); }

void setupPins(void){ // Set all the digital pins as inputs // with the pull-up enabled, except for the // two serial line pins int var ; for (int i = 2; i <= 12; i++){ pinMode(i, INPUT); digitalWrite(i, HIGH); } pinMode(A4, INPUT); digitalWrite(A4, HIGH); pinMode(A5, INPUT); digitalWrite(A5, HIGH); }

dataForController_t getControllerData(void){

int anx; int anxj; int any; int nombreEnCours = 0; int continu=0;

// Set up a place for our controller data // Use the getBlankDataForController() function, since // just declaring a fresh dataForController_t tends // to get you one filled with junk from other, random // values that were in those memory locations before dataForController_t controllerData = getBlankDataForController(); // Since our buttons are all held high and // pulled low when pressed, we use the "!" // operator to invert the readings from the pins controllerData.triangleOn = !digitalRead(2); controllerData.circleOn = !digitalRead(3); controllerData.squareOn = !digitalRead(4); controllerData.crossOn = !digitalRead(5); controllerData.dpadUpOn = !digitalRead(6); controllerData.dpadDownOn = !digitalRead(7); controllerData.dpadLeftOn = !digitalRead(8); controllerData.dpadRightOn = !digitalRead(9); controllerData.l1On = !digitalRead(10); controllerData.r1On = !digitalRead(11); controllerData.selectOn = !digitalRead(12); controllerData.startOn = !digitalRead(A4); // controllerData.homeOn = !digitalRead(A5);

// Set the analog sticks // Since analogRead(pin) returns a 10 bit value, // we need to perform a bit shift operation to // lose the 2 least significant bits and get an // 8 bit number that we can use
anxj=analogRead(A0); controllerData.leftStickX = anxj >> 2; controllerData.leftStickY = analogRead(A1) >> 2;

continu=0; //Serial.println(); while (mySerial.available()&& continu==0) {

                 char c = mySerial.read();
     //Serial.print(c); 
    // Si c'est un chiffre, on l'ajoute au nombre
    if ( ( c >= '0' ) && ( c <= '9' ) ) {
        nombreEnCours = ( ( nombreEnCours * 10 ) + ( c - '0' ) );
    }

    // Si c'est une lettre majuscule, alors on fait une action
    if ( ( c >= 'A' ) && ( c <= 'Z' ) ) {

        // Pour un 'A', on affiche un texte
        if ( c == 'X' ) {
            anx=nombreEnCours;
            //Serial.println(anx); 

            //Serial.println(controllerData.rightStickX);
        }

        if ( c == 'Y' ) {
            any=nombreEnCours;
            //Serial.println(any);
            //Serial.println();
            continu=1;
        }

        // Puis on revient à 0
        nombreEnCours = 0;
            // int var = mySerial.read();

              }
              }

  controllerData.rightStickX = (anx) >> 2; 
  controllerData.rightStickY = (any) >> 2; 

   //Serial.println(anx);
   //Serial.println(controllerData.rightStickX); 

              //Serial.println (controllerData.rightStickX);
              //Serial.println(anx);                  
              //Serial.println(controllerData.leftStickX);
              //Serial.println();

/ controllerData.rightStickX = analogRead(A2) >> 2; controllerData.rightStickY = analogRead(A3) >> 2; /

// And return the data! return controllerData; } " If anyone could help