Serial.write()
sends out the binary value range from 0 to 1023, a 10-bit data which can't fit in a byte, so it needs to be divide by 4 in the code or use map()
function.
Serial.println()
can send out ASCII-encoded data, larger numbers and characters
Analog reading in different format
Use Serial.available()
to call and receive data.
I can't run p5 when Arduino connected only by p5.serialcontrol, but it works when adding port select menu to the sketch.
serial.read()
When use Serial.println()
, the data send from Arduino is ASCII-encoded, but P5.js reads every byte's value of the data, where the graph has a bunch of 13 and 10(the number of ASCII carriage return and newline). Use serial.readLine()
in P5 to read the incoming serial data as a string, and fix the gaps when data come in by checking if the string is a valid number.
var inString = serial.readLine();
if(inString.length >0) {
inData = Number(inString);
}