I understand that it might be annoying that I am posting on a different thread for the same project, but it is a different code and approach, so I thought it was appropriate to do this.
I have literally been at this thing for 12 hours (I woke up this morning and tried to make progress, but nothing ). It’s been stupid array errors back and back again, which prompted me to take a different approach.
Basically, I want to have a potentiometer on an Arduino to send it’s values to Processing and from there I will draw a line that moves/draws as my control stick does. This is supposed to be a simpler version of what I really want to do, which is to makes lines with a gyroscope/accelerometer as the values change per axis.
But, I am going nuts making no progress! It is utterly frustrating to the point that reading documentation just throws me off and I feel so dumb because none of what I read sticks through.
It’d be great if someone could kind of walk me through this as I complete this project, and I understand I can’t just have someone do my project for me, but I just need a big help here.
Here’s the code:
import processing.serial.*;
Serial port;
int index = 0;
int[] input_arr = new int[2];
int px, py; //previous x, y
int x, y; //current x, y
void setup() {
background(0);
size(500, 500);
port = new Serial(this, "COM12", 115200);
stroke(255);
}
void draw() {
}
void serialEvent() {
while (port.available() > 0) {
//two values are being sent, x and y
//here i assume that after the newline it will go and read the
//next set of values
//how the values are coming in through serial:
// xvalue \t y value
// newline
// ...
String raw = port.readStringUntil('\n');
input_arr[index] = int(raw);
index++;
if (index > 1) {
x = px + input_arr[0];
y = py + input_arr[1];
// flushes buffer/serial, whatever it's called
port.clear();
//following line is for debug; however this doesnt print
//anything and that's my issue
println(input_arr[0]);
px = x;
py = y;
}
}
//im certain this should be in draw() but idk at this point tbh.
line(x, y, px, py);
}
At this point I am really really desperate for an answer, solution. Also, at least the code compiles, unlike what it’s been doing for the past 2 days – giving PointerException errors
Please help .-. Reading documentation is giving me nothing and even if I tried nothing is being processed. Stressed af because of this stupid project. I’ll give myself a break in hopes of a miracle (no, not really, I need to think straight to work well).
Thanks