Skip to content

Instantly share code, notes, and snippets.

@xd2
Created June 7, 2019 22:30
Show Gist options
  • Save xd2/42224eadb99a683fcc1b18fa52eeb5af to your computer and use it in GitHub Desktop.
Save xd2/42224eadb99a683fcc1b18fa52eeb5af to your computer and use it in GitHub Desktop.
#include "UnoJoy.h"
int hZero = 0;
int vZero = 0;
void setup(){
setupPins();
setupUnoJoy();
setupZeros();
}
void setupZeros(){
hZero = analogRead(A0);
vZero = analogRead(A1);
}
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
for (int i = 2; i <= 12; i++){
pinMode(i, INPUT_PULLUP);
digitalWrite(i, HIGH);
}
pinMode(A4, INPUT);
digitalWrite(A4, HIGH);
pinMode(A5, INPUT);
digitalWrite(A5, HIGH);
}
dataForController_t getControllerData(void){
// 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(10);
controllerData.circleOn = !digitalRead(9);
controllerData.squareOn = !digitalRead(8);
controllerData.crossOn = !digitalRead(7);
//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
int x = square(analogRead(A0), hZero) ;
int y = square(analogRead(A1), vZero) ;
controllerData.leftStickX = x >> 2;
controllerData.leftStickY = y >> 2;
//controllerData.rightStickX = analogRead(A2) >> 2;
//controllerData.rightStickY = analogRead(A3) >> 2;
// And return the data!
return controllerData;
}
int square(int value, int zero){
if(value > zero * 1.1) {
return 1023;
} else if(value < zero * 0.8) {
return 0;
} else return 512;
}
@xd2
Copy link
Author

xd2 commented Aug 30, 2020

To debug gamepad pins values in Arduino mode :

int hZero = 0;
int vZero = 0;

void setup() {
  Serial.begin(115200);

  setupPins();
  setupZeros();
}

void setupZeros() {
  hZero = analogRead(A0);
  vZero = analogRead(A1);
}

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


void debug() {
  int ten = digitalRead(10);
  //Serial.print("d10:");
  //Serial.println(ten);

  Serial.print("A0");
  Serial.print(":");
  Serial.print(analogRead(A0));
  Serial.print(" ");

  Serial.print("A1");
  Serial.print(":");
  Serial.print(analogRead(A1));
  Serial.print(" ");


  for (int i = 7; i <= 10; i++) {
      Serial.print("D");
      Serial.print(i);
      Serial.print(":");
      Serial.print(digitalRead(i));
      Serial.print(" ");
  }
      Serial.println("");


}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment