// This version of MegaJoy sketch was created originally // by Alan Chatham and the UnoJoy team. // // WIP build being modified by Deusimar Rodrigues dos Santos Junior, // also known as Shadow Nightmares, in July 2020 // // Using parts of a code created by AMStudio/Armandoiglesias, // written in 2018 //-------------------------------------------------------------------- #include "MegaJoy.h" void setup(){ setupPins(); setupMegaJoy(); pinMode(A2, INPUT_PULLUP); // X axis pinMode(A3, INPUT_PULLUP); // Y axis pinMode(2, INPUT); digitalWrite(2, HIGH); } void loop(){ // Always be getting fresh data megaJoyControllerData_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 = 3; i <= 54; i++){ pinMode(i, INPUT); digitalWrite(i, HIGH); } } megaJoyControllerData_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 megaJoyControllerData_t controllerData = getBlankDataForMegaController(); // Since our buttons are all held high and // pulled low when pressed, we use the "!" // operator to invert the readings from the pins for (int i = 3; i < 54; i++){ controllerData.buttonArray[(i - 3) / 8] |= (!digitalRead(i)) << ((i - 3) % 8); } // 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 controllerData.analogAxisArray[0] = analogRead(A0); controllerData.analogAxisArray[1] = analogRead(A1); int x = analogRead(A2); // X axis int y = analogRead(A3); // Y axis if(x<350) // Shifter on the left? { if(y<350) digitalWrite(3, LOW); delay(15); // 1st gear if(y>650) digitalWrite(4, LOW); delay(15); // 2nd gear if((y>=350) && (y<=650)) {digitalWrite(3, HIGH); digitalWrite(4, HIGH);}; delay(15); } else if(x>650) // Shifter on the right? { if(y<350) digitalWrite(7, LOW); delay(15); // 5th gear if((y>650) && (digitalRead(2) == HIGH)) {digitalWrite(8, LOW); digitalWrite(9, HIGH);}; delay(15); // 6th gear if((y>650) && (digitalRead(2) == LOW)) {digitalWrite(9, LOW); digitalWrite(8, HIGH);}; delay(15); // Reverse if((y>=350) && (y<=650)) {digitalWrite(7, HIGH); digitalWrite(8, HIGH); digitalWrite(9, HIGH);}; delay(15); } else // Shifter is in the middle { if(y<350) digitalWrite(5, LOW); delay(15); // 3rd gear if(y>650) digitalWrite(6, LOW); delay(15); // 4th gear if((y>=350) && (y<=650)) {digitalWrite(5, HIGH); digitalWrite(6, HIGH);}; delay(15); } // And return the data! return controllerData; }