Intro to Asynchronous Seriel Communication

Data Format

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

Analog reading in different format

Flow Control

Use Serial.available() to call and receive data.

Serial Input to P5.js

I can't run p5 when Arduino connected only by p5.serialcontrol, but it works when adding port select menu to the sketch.

Screen Shot 2021-10-19 at 3.39.58 PM.png

Screen Shot 2021-10-19 at 3.37.38 PM.png

serial.read()

Draw a graph with the sensor values

Serial Communication1.mp4

Reading Serial Data as a String

Screen Shot 2021-10-19 at 3.56.48 PM.png

Screen Shot 2021-10-19 at 3.58.41 PM.png

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);
}