/* getControls(long debounce) + Gets current status of controlls with a configurable debounce delay + Updates buttonStatus Array[X, Y, A, B, C, D] + X -> -1 Left & 1 Right + Y -> -1 Down & 1 Up + A,B,C,D -> 0 Not Pushed & 1 Pushed Y A -X JOY X B C -Y D saveControls - Saves last state of controls. Good for comparing current state to previous state + Updates lastButtons Array[X, Y, A, B, C, D] to buttonStatus clearControls - Clears last state of controls. Use when ending program that uses saveControls. + Updates lastButtons Array[X, Y, A, B, C, D] to -1 */ // Save Last Status of Controls void saveControls() { for (int i = 0; i < 6; i++) { lastButtons[i] = buttonStatus[i]; } } // Clear Last Status of Controls void clearControls() { for (int i = 0; i < 6; i++) { lastButtons[i] = -1; } } // Get Status of Controls void getControls(unsigned long debounce) { /* // Joystick X int xRead = analogRead(joystickX); if (xRead > 1000){buttonStatus[0] = -1;} else if (xRead < 100){buttonStatus[0] = 1;} else{buttonStatus[0] = 0;} */ // Joystick X int x = 0; int xRead = analogRead(joystickX); // Read X Joystick value if (xRead > 1000) { x = -1; } else if (xRead < 100) { x = 1; } else { x = 0; } if (x == 1 || x == -1) { // If Pressed if (millis() - buttonTimers[0] > debounce) { // If last time it was pressed is greater than debounce value buttonStatus[0] = x; // Update button register to 1 buttonTimers[0] = millis(); // Update button timer to current time } else { buttonStatus[0] = 0; // Otherwise set button to zero and don't update timer } } else { buttonStatus[0] = 0; } // Joystick Y int y = 0; int yRead = analogRead(joystickY); // Read Y Joystick value if (yRead > 1000) { y = -1; } else if (yRead < 100) { y = 1; } else { y = 0; } if (y == 1 || y == -1) { // If Pressed if (millis() - buttonTimers[1] > debounce) { // If last time it was pressed is greater than debounce value buttonStatus[1] = y; // Update button register to 1 buttonTimers[1] = millis(); // Update button timer to current time } else { buttonStatus[1] = 0; // Otherwise set button to zero and don't update timer } } else { buttonStatus[1] = 0; } // Button A int a = digitalRead(buttonPins[0]); // Read A button value if (a == 1) { // If Pressed if (millis() - buttonTimers[2] > debounce) { // If last time it was pressed is greater than debounce value buttonStatus[2] = 1; // Update button register to 1 buttonTimers[2] = millis(); // Update button timer to current time } else { buttonStatus[2] = 0; // Otherwise set button to zero and don't update timer } } else { buttonStatus[2] = 0; } // Button B int b = digitalRead(buttonPins[1]); // Read B button value if (b == 1) { if (millis() - buttonTimers[3] > debounce) { buttonStatus[3] = 1; buttonTimers[3] = millis(); } else { buttonStatus[3] = 0; } } else { buttonStatus[3] = 0; } // Button C int c = digitalRead(buttonPins[2]); // Read C button value if (c == 1) { if (millis() - buttonTimers[4] > debounce) { buttonStatus[4] = 1; buttonTimers[4] = millis(); } else { buttonStatus[4] = 0; } } else { buttonStatus[4] = 0; } // Button D int d = digitalRead(buttonPins[3]); // Read D button value if (d == 1) { if (millis() - buttonTimers[5] > debounce) { buttonStatus[5] = 1; buttonTimers[5] = millis(); } else { buttonStatus[5] = 0; } } else { buttonStatus[5] = 0; } }