Thanks Lexyth it works just fine! But the arduino output doesn’t show up in processing and everything seems fine. So I don’t know where to problem is. De arduino code works fine and the processing also, just the data transfer from arduino to processing doesn’t work. If someone could help me figure this out then I’m done!
Arduino code:
void setup()
{
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
int x = random(0, 1000);
int y = random(0, 1000);
int MyArray [2] = {x,y};
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.println();//('\n');
delay(3000);
}
Processing code:
import processing.serial.*;
Serial myPort; // maakt een object van seriele poort
String val; //ontvangt de data van de seriele poort
float x=0, y=0;
float space = 25; //grootte hokjes
void setup ()
{
size (1050, 1050); // maak het canvas
x = map(x,0,1000,25,1025); //and
y = map(y,0,1000,25,1025);
background(255);
strokeWeight(0);
String portName = Serial.list() [0]; // de 0 moet worden veranderd in de poort
myPort= new Serial(this, portName, 9600);
}
void draw ()
{
background(255);
fill(0);
textSize(10);
for (float xx=25; xx<=1025-space; xx+=space) { // x-as
for (float yy=25; yy<=1025-space; yy+=space) { // y-as
line(xx, yy, xx, yy+space);
line(xx, yy, xx+space, yy);
if (xx == 25) text((int)(yy-space), xx-space, yy);
else if (yy==25) text((int)(xx-space), xx, yy);
}
line(25, 1025, 1025, 1025);
line(1025, 25, 1025, 1025);
}
if (myPort.available() > 0) //als data beschikbaar is
{
val = myPort.readStringUntil('\n'); // lees het uit en sla het op in val
if (val != null) // Als er waardes worden uitgelezen gaat de hier in
{
float [] list = float( split (val, ",")); // data verkregen van arduino wordt bij de komma gesplitsd.
println(val); // print het uit op het console
x = list[0]; //part1 wordt de de eerst afkoppeling opgeslagen
y = list[1]; // part2 wordt de tweede afkoppeling opgeslagen
}
}
strokeWeight(14);
point (x, y); // de variabelen uit part1 en part2 worden gebruikt voor de coördinaten
strokeWeight(0);
}```