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);
}
}
}