Serial Communication Lab

I have been so frustrated for so long about this issue that keeps resurfacing, especially in this lab. I first had this problem in the stupid pet trick, when I didn’t know anything about serial communication, I faked my way around it by altering the code, and now its back to haunt me. I think I’ve pretty much asked everybody I can think of, and everybody has different ideas about where this serial “noise” is coming from, all of which make subtle differences but don’t eliminate the problem.

Basically this issue is this: when I set up for serial communication, the serial monitor in the arduino reads everything perfectly as expected, with values, when it is decimal for example, ranging with the potentiometer from 0 to 255. When I read those values in Processing with println(Serial.list()), in the potentiometer’s zero position, there is a constant feed of 0,10,13,0,10,13,0,10,13..,etc. As I increase the voltage with the potentiometer, the zero value moves as would make sense, slowly up to 255 with the voltage, but the 10 and 13 continue to interrupt the read, so that in this lab, for example, there was a constant 10,13 read in the graph:

I think that there is some kind of data getting sent along with what I want to send, or some delay causing something to get stuck, who knows.

The only way I could make it stop was by initiating a variable for the last myPort reading, so that if the myPort reading was either 13 or 10 then the value printed was the last myPort reading. Luckily this seemed to work, but it produced a step like graph, because there are skipped readings as a result.

And this solution is totally unsatisfying because I can’t figure out what is going on!! Nobody seems to have this problem, so could it be my arduino? I even tried on another computer, different sensor, etc, and I had the same issue with slight variations.

Here is my code for the fake solution:

import processing.serial.*;
int inByte;
Serial myPort;
int graphXPos = 1;
void setup () {
size(600, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
background(65,143,211);
}
void draw(){}
void serialEvent (Serial myPort) {
int lastinByte=inByte;
inByte = myPort.read();
if(inByte==10){inByte=lastinByte;}
if(inByte==13){inByte=lastinByte;}
println(inByte);
stroke(255,173,64);
line(graphXPos, height, graphXPos, height – inByte);
if (graphXPos >= width) {
graphXPos = 0;
background(65,143,211);
}
else {
graphXPos++;
}
}

import processing.serial.*;int inByte;
Serial myPort;       int graphXPos = 1;   void setup () {  size(600, 300);          println(Serial.list());  myPort = new Serial(this, Serial.list()[0], 9600);  background(65,143,211);}

void draw(){}
void serialEvent (Serial myPort) {
int lastinByte=inByte;  inByte = myPort.read();
if(inByte==10){inByte=lastinByte;}   if(inByte==13){inByte=lastinByte;}   println(inByte);  stroke(255,173,64);  line(graphXPos, height, graphXPos, height – inByte);

if (graphXPos >= width) {    graphXPos = 0;    background(65,143,211);  }   else {    graphXPos++;  }}

Ok, I am so relieved to have found the solution in the next lab, but I am going to leave this here as a reminder of the torment and the happiness I just felt when I read in the next Lab that “Serial.println(analogValue, DEC) actually sent FOUR bytes! It sent a byte to represent the 3, a byte to represent the 2, a byte to tell the Monitor to move the cursor down a line(newline), and a byte to move the cursor all the way to the left (carriage return). The raw binary values of those four bytes are 51 (ASCII for “3”), 50 (ASCII for “2”), 10 (ASCII for “newline”), and 13 (ASCII for “carriage return”). Check the ASCII table and you’ll see for yourself.”

Of course! I had consistently changed the arduino code of the previous program to say println instead of print because I was checking the serial monitor first to see if the values were being generated correctly by the potentiometer, and it was easier to see them spaced by lines. Even though I did change it back to BYTE, I didn’t consider that as a result of the println function, the arduino would be sending the value reading, then 10 for “newline” and 13 for “carriage return”. How funny.