Intro to Asynchronous Serial Communications
To begin with, I used potentiometer to understand the difference between ASCII and Binary data. Video 1 shows that Serial.println() formats the value it prints as an ASCII-encoded decimal number, with a linefeed at a carriage return at the end. Video 2 shows the output from analogRead() in a single byte, and map the output to a range from 0-255.
Video 1
Video 2
Arduino can set the data in many formats (see Figure 1). I don't know why the first column shows differently from the website when it supposes to show the raw binary value.
Figure 1
I had trouble implementing the flow control (see Figure 2). The error is a function-definition is not allowed here before {. I realized that's just the data format, so I have to write a program that reads that format, which I will follow the two-way serial communication lab after i go through all week 6's lab.
Serial Input to P5.js
Goal: To generate an analog output value from a potentiometer, then send that value via asynchronous serial communication to P5.js. To use that value in P5.js to draw a graph.
To start off, I programed Arduino with potentiometer, and downloaded the p5.js serial port file from github, then uploaded it to p5js editor (see Figure 3 and 4). The port name I got from the console is ws: / / localhost 8081.
Figure 3
Figure 4
Then I followed the the codes from the tutorial. When I run the sketch, it didn't give me the sensor value (see Figure 5).
Figure 5
I went back and checked my code and serial monitor value, and this is what it showed (see Figure 6). I'm not sure why it doesn't show the mapped value. This is probably also the reason that I couldn't sketch the graph.
Figure 6
However, the control panel (see Figure 7) from the p5.serial control app is generating values from the potentiometer.... I'm confused.
Figure 7
I changed serial.write() to serial.print(). The mapped value is showing as I turning potentiometer! However, it still couldn't get the graph (see Figure 8), and the error says map() received an empty variable.
Figure 8
Serial Output from P5.js
Goal: Knows how to use P5 to control a microcontroller using asynchronous serial communication.
I set up my breadboard with a simple LED circuit, and successfully connected arduino and P5 sketches to serial control app. After I run the sketch, the initial incoming serial value is undefined, but when I drag the mouse up and down on the canvas, it didn't change the brightness of LED.
After I quit serial control panel and made sure everything is connected with serial.write(), I run the sketch again. It finally worked! The brightness change is pretty subtle though.
Comments