Lecture27_Serial_Comm_III

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

Connect your potentiometer and your Arduino as you did in the last lecture.

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

Plot a sinewave using the 'animatedline()' function. Read the comments to understand the code.

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

1. 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.

2. 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.

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

4. 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 figure 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 points takes 8/9600 seconds to be sampled.
** voltages: y*(5/1024)

Hint: Do not click the example before you try it yourself.

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

Submit an electronic version of your report to the email. Again, have figures, captions, title, explanations, and your code as the appendix. Due Wednesday.