Help: Arduino Joystick Shield tracing a wave

The goal of my project is using a ardunino leonardo with a joystick shield to trace a wave in processing. The joystick is able to control the y axis of the cursor (red dot) and the x is static. A generated wave moves across the screen and you trace it with the joystick moving up and down. Currently we cannot get an accurate way of tracing the curve of the wave with the joystick. What would be a better way to control the joystick to more accurately trace the curvature of the wave? Here is our current code to control the joystick:

import processing.serial.*;

Serial myPort;
float velocityX = 0;
float velocityY = 0;
float cursorX, cursorY;  // Current position of the cursor
String portName;
void updateCursor() {
    cursorX += velocityX;
    cursorY += velocityY;

    // Constrain the cursor to the window boundaries
    cursorX = constrain(cursorX, 0, width);
    cursorY = constrain(cursorY, 0, height);
}

void updateJoystickValues(float xVal, float yVal) {
   
    
    velocityY = yVal - 500;
    velocityY = map(velocityY, -500, 500, -1, 1) * 5;


    println("Velocity X: " + velocityX + ", Velocity Y: " + velocityY); // Debugging output
}
void serialEvent(Serial port) {
    String input = port.readStringUntil('\n');
    if (input != null) {
        input = input.trim();
        String[] strVals = splitTokens(input, ",");
        if (strVals.length >= 2) { // Assuming at least X and Y values are sent
            float xVal = float(strVals[0]);
            float yVal = float(strVals[1]);
            updateJoystickValues(xVal, yVal);
        }
    }
}

Hi @jzeng21, You are welcome, sorry no-one has replied. I’m not clear what you are trying to do, maybe others are as well and that’s why there are no replies.

The green trace on the image, that’s drawn by the computer? The white trace is the joystick trying to follow it? Someone is holding the joystick trying to follow the green line? Your posted code is not the whole thing? Just the part that does … ? You say “code to control the joystick” - but the hand controls the joystick? “code to read the joystick”?

Richard.

1 Like