Simple arduino to processing serial not outputting anything

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 :disappointed:). 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

Hi eezJerome, I have This question About:

  • can you post the Arduino Code?
  • why you force input array data?
int[] input_arr = new int[2];

Sorry if I will don’t response quickly but I’m busy to interconnect one Robot and one Laser 3D with Arduino wireless…

Oh sure! Here:



void setup() {
  Serial.begin(115200);
}

void loop() {
  int x = analogRead(A1);
  int y = analogRead(A0);

  int x1 = map(x, 1023, 0, -10, 10);
  int y1 = map(y, 0, 1023, -10, 10);

  Serial.print(x1); 
  Serial.write('\t');
  Serial.print(y1); 
  Serial.println('\n');
}

so you should split your arduino line by the ‘\t’ to get a string array,
and convert that strings to int.


but the arduino code not makes sense,
you read 0 … 1023 and create int -10 … 10
that are 20 steps only // you damage your resolution.

why not make them float in arduino and send float?

or much better just send the 0… 1023
and do the rest in Processing!

also test that little example project Issue with using array after splitTokens() here

Thanks will do! I was actually reading the other one; i’ll report any progress/issues