Asynchronous Serial Communication

 

Serial Communication Agreements

Three things to check for it to happen

  1. The rate at which data is sent and read (9600, etc)
  2. The voltage levels representing a 1 or a 0 bit
  3. The meaning of those voltage levels; 1 = hight & 0 = low or the other way around?
LAB1: Serial Terminal Programs (Link)

*Only one monitor at a time

Reading Adafruit LIS3DH Triple-Axis Accelerometer Breakout

Serial monitor

Cool term

Terminal

  • Get a list of my serial ports by typing
    ls -1 /dev/cu.*
  • See what the Arduino is sending out, type
    cat /dev/cu.myPortName
    *make sure to have space after “cat”
Adafruit LIS3DH
  • Three-axis sensing
  • Both I2C (2 possible addresses) and SPI interface options
  • Tap, Double-tap, orientation & freefall detection
  • Adafruit_LIS3DH.h library required, downloaded directly from Arduino manage library
  • LIS3DSH.h <-- this library doesn't work. (1, 2)
lAB2: Serial Input to P5.js

Steps in order for p5.js and Arduino’s incoming sensing values to communicate

1
At P5.js, add

p5.serialport.js.

2
At P5.js/ index.html, add

3
At P5.js/ sketch.js, add
scripts enabling the communication

p5.js link

Reading serial data as a binary  VS string
Serial.write();

Serial.write();

can directly send byte number to p5.js, however, the number number is limited to 255
(conversion of the value ex.map() required)

inData = Number(serial.read());

Reads the byte as a number, and stores it in the global variable inData


Sensor value: undefined
happened while doing the Serial.write with the value going over 255

Serial.println();

Serial.println();

With Serial.write(); value abled to send is limited to 0-255, which is less sensitive than reading values btwn 0-1023. However, the values will be printed in ASCII-encoded numeric string.

var inString = serial.readLine();
inData = Number(inString));

Create a local variable to get the string, then check to see if the string’s length is greater than zero. If it is, then put it into inData so that the other functions in the sketch can use the new data

Compare

ASCII table