//Unused Constants Due to Major Fluctuations const int X_NEUTRAL = 500; //The value of the x axis when joystick is resting (changes frequently) const int Y_NEUTRAL = 506; //The value of the y axis when joystick is resting (changes frequently) const int FLUX_BUFFER = 8; //The amount of a buffer between the resting state and motion (to handle slight power fluctuations) //Input Pins byte buttonPin = 12; byte joystickX = A0; byte joystickY = A1; byte joystickSwitch = 2; //Stored Values byte buttonState; byte lastButtonState = LOW; int xPos = 0; int yPos = 0; void setup() { //Setup the input pins and Serial Communication Serial.begin(9600); pinMode(joystickX, INPUT); pinMode(joystickY, INPUT); pinMode(buttonPin, INPUT); } void loop() { //Tell Processing which direction to move the object printDirection(); //Tell Processing if the button was pressed printButton(); //Tell Processing the command has ended endCommand(); } void printDirection(){ //Read the x and y values from the joystick xPos = analogRead(joystickX); yPos = analogRead(joystickY); //---------Uncomment the lines below to get your x and y axis values printed to the Serial Monitor-------- //---------This is useful for determining what your joystick's resting x and y values are---------------- // Serial.print("X-Position: "); // Serial.print(xPos); // Serial.print("\n"); // Serial.print("Y-Position: "); // Serial.print(yPos); // Serial.print("\n"); //Print the direction that the joystick moved //Sensitivity decreased and previous use of resting and buffer constants commented out due to erratic x and y fluctuations //if(yPos < Y_NEUTRAL - FLUX_BUFFER){ if(yPos < 400){ Serial.print("UP"); Serial.print(","); } //else if(yPos > Y_NEUTRAL + FLUX_BUFFER){ else if(yPos > 600){ Serial.print("DOWN"); Serial.print(","); } else{ Serial.print("NEUTRAL"); Serial.print(","); } //if(xPos < X_NEUTRAL - FLUX_BUFFER){ if(xPos < 400){ Serial.print("LEFT"); Serial.print(","); } //else if(xPos > X_NEUTRAL + FLUX_BUFFER){ else if(xPos > 600){ Serial.print("RIGHT"); Serial.print(","); } else{ Serial.print("NEUTRAL"); Serial.print(","); } } void printButton(){ //Read the button state buttonState = digitalRead(buttonPin); //Prevent button from triggering multiple times per press and print when it is pressed if(buttonState == HIGH and lastButtonState == LOW){ Serial.print("BUTTON"); } else{ Serial.print("NEUTRAL"); Serial.print(","); } //Update the last state of the button lastButtonState = buttonState; } void endCommand(){ //End the commands with a newline character Serial.print("\n"); }