Lecture31_Serial_Communication_I

In this experiment, we will detect a real-time serial data set and plot it in a professional figure.

Demonstrate your work during the class to get the credit. If you can't get it done, you still have a chance to work it in the next class or you can checkout a board to work on it afterwards.

Task 1: (25 points)

Plot a sinewave using the 'animatedline()' function. Read the comments to understand the code. (no Arduino board is needed for this)

(25 points)

Connect your potentiometer to your Arduino board.

The voltage is sampled by Port A0, so use the following code for your arduino to stream the data into your computer through the USB connection.

Double check the name of the port being used may be changed this time.

Plot a real-time serial data using the following script. Read the comments to understand the code.

MODIFY the code, use the while loop to collect 20k data points (the example I provided above can only collect 1 data since the data points are being overwritten in y).  Modify the script to store all the detected data in a data set of 'y'. Do not click the solution unless you can't get it work. (solution for 10 k data points, do not click me before you try it yourself!!!!!!!)

During the serial communication, turn the knob of the potentiometer to change the voltages. Make sure you can see the voltage changes in the monitor.

Task 3: (25 points)

After the serial plotting is done, in the command window, type:

'save y1.mat y' (this command means create a .mat file named 'y1.mat' to save the data in variable 'y' in there), you should see a y1.mat file being created in the working directory:
Kick out the first NaN data in your data files before you save them.

Save them:

Now, you have the data saved on your hard drive. You can copy the data file to your USB and load it in the future.

Repeat Task 1 to collect another session of 20k data points, and save it as y2.mat.

Task 4: (25 points)

Plot the data in a professional way. Use function load('y1.mat') and load('y2.mat') in your script to load these data. Every time you load each of these data set, the previous one will be overwritten. So assign the loaded 'y' to another variable before you load the other one. Plot the two functions in the same window, use two different colors for the two lines.

Convert the data numbers into correct time points and convert Y into real voltages but not digital numbers. Plot the X axis as the time, Y as the voltage, change the line width and font size to make the figure in a presentable quality.
** hint: 9600 bits/s, 8 bits per data points. You have 20 k data points, which means it takes about 20k*8/9600 seconds. So every data point takes 8/9600 seconds to be sampled.
** voltages: y*(5/1024)

example (I basically use the same data set but Times 2 of the y values for the second data set).