Serial receiving wrong data
in
Core Library Questions
•
1 year ago
Im trying to send with Arduino the code below integers between 0-300 and creates a line graph in processing. It works fine when I use the serial terminal and I see 0-300, but when it is being received in processing it only sees up to 255 and then starts over 0-44. and then restarts.
//Arduino code
int a=0;
void setup() {
Serial.begin(9600);
}
void loop() {
toSerial();
}
void toSerial() {
Serial.write(a);
if (a>300){
a=0;
}
a++;
}
//processing code
import processing.serial.*;
Serial myPort;
int value;
int[] values = new int[400];
void setup(){
size(400,400);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
for(int i = 0; i<values.length;i++){
values[i]=0;
}
}
void draw(){
getData();
background(255);
stroke(0);
beginShape();
for(int i = 0; i<values.length;i++){
//println(values[i]);
vertex(i,values[i]);
}
endShape();
}
void getData(){
if ( myPort.available() > 0) {
value = myPort.read();
for(int i = 1; i<values.length;i++){
values[i-1] = values[i];
}
values[values.length-1]=value;
}
}
Thanks for the help Ken
//Arduino code
int a=0;
void setup() {
Serial.begin(9600);
}
void loop() {
toSerial();
}
void toSerial() {
Serial.write(a);
if (a>300){
a=0;
}
a++;
}
//processing code
import processing.serial.*;
Serial myPort;
int value;
int[] values = new int[400];
void setup(){
size(400,400);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
for(int i = 0; i<values.length;i++){
values[i]=0;
}
}
void draw(){
getData();
background(255);
stroke(0);
beginShape();
for(int i = 0; i<values.length;i++){
//println(values[i]);
vertex(i,values[i]);
}
endShape();
}
void getData(){
if ( myPort.available() > 0) {
value = myPort.read();
for(int i = 1; i<values.length;i++){
values[i-1] = values[i];
}
values[values.length-1]=value;
}
}
Thanks for the help Ken
1