// Bechele2 Joystick Code V2.1 Sept 2021 // // Arduino code for the joystick used for programming servo movements in Bechele2 animatronic software: // http://bechele.de/pages/english/72-0.html // // Based on original code written by Rolf Jethon: // http://bechele.de/pages/english/77-0.html // // For more info on building this joystck: // https://zappedmyself.com/animatronics/bechele2-info/ // // This code sends the Joystick X & Y values and the button status to the Raspberry Pi // Each time the number 4 (ASCII value 52) is received from Raspberry Pi the data is sent // // Can run on any Arduino variant (I used a NANO clone) // - Cleaned up comments and naming // - Added code internal pullups to simplify wiring // - Changed the joystick averaging code to get better range of ADC values //***PIN ASSIGNMENTS***// #define Y_PIN A0 // Analog input pin that the X axis pot is connected to #define X_PIN A1 // Analog input pin that the Y axis pot is connected to #define BUTTON1_PIN 2 // Digital input pin the START button is connected to #define BUTTON2_PIN 3 // Digital input pin the STOP button is connected to //***CUSTOMIZE VALUES***// #define BAUD_RATE 19200 // Baud rate for serial port #define ALPHA_VALUE 0.9 // Averaging factor (0.1 - 1.0) higher value = faster averaging //***VARIABLE DECLARATION***// int xAxisValue = 496; // set X value to middle of possible range int yAxisValue = 496; // set Y value to middle of possible range int xAxisMax = 1022; int yAxisMax = 1022; int xVal; int yVal; int inByte = 0; // incoming serial byte float alphaFactor = ALPHA_VALUE; // set to defined value //***MICROCONTROLLER CONFIGURATION***// void setup() { pinMode(BUTTON1_PIN, INPUT_PULLUP); pinMode(BUTTON2_PIN, INPUT_PULLUP); Serial.begin(BAUD_RATE); // Setup serial port speed } //***START OF MAIN LOOP***// void loop() { xAxisValue = alphaFactor * analogRead(X_PIN) + (1 - alphaFactor) * xAxisValue; // Get Xaxis value and average it delay(3); yAxisValue = alphaFactor * analogRead(Y_PIN) + (1 - alphaFactor) * yAxisValue; // Get Yaxis value and average it int button1State = digitalRead(BUTTON1_PIN); // Get Button 1 state int button2State = digitalRead(BUTTON2_PIN); // Get Button 2 state xVal = xAxisMax - xAxisValue; yVal = yAxisMax - yAxisValue; // Test /* Serial.print(xVal); Serial.print(" "); Serial.print(yVal); Serial.print(" "); Serial.print(button1State); Serial.print(" "); Serial.println(button2State); */ // Test 2 (graphique) Serial.print(xVal); Serial.print(","); Serial.print(yVal); Serial.print(","); Serial.print(button1State * 1000); Serial.print(","); Serial.println(button2State * 1000); delay(50); /* if (Serial.available() > 0) { // Check to see if serial data request has been received inByte = Serial.read(); // Store serial data if (inByte == 52) { // Send data values if ASCII '4' is received Serial.print(xAxisValue); Serial.print(" "); Serial.print(yAxisValue); Serial.print(" "); Serial.print(button1State); Serial.print(" "); Serial.println(button2State); } inByte = 0; // Clear inByte value for next command }*/ }