Interface and Application Programming

Week 16

Introduction

In interface and application programming week, the time-of-flight sensor value in millimeter distance will be sent as an output from our ATMEGA328P based micro-controller board. Through serial communication the data was received to the MATLAB IDE, and the distance was displayed on the graphical user interface.

MATLAB - Code

Based on previous documented Arduino code for time-of-flight input sensor, and the addition was to use the command 'serial.write' to write the PWM value to the serial port. The code is as follows:

    byte PWM_PIN = 6;

    int pwm_value;

    void setup() {
      pinMode(PWM_PIN, INPUT);
      Serial.begin(9600);
    }

    void loop() {
      pwm_value = pulseIn(PWM_PIN, HIGH);
      pwm_value = (pwm_value)/10;
      Serial.write(pwm_value);
      Serial.println(pwm_value);
    }
     
        

MATLAB - Commands

To establish a serial communication between MATLAB and the board the following commands were used:

        x = serial('COM3','BaudRate',9600)
            fopen(x);       
        

This is a very basic code consisting of two lines of code, the first is to create a serial port object and the second is to connect the created serial interface object with the ATMEGA328 board. This code confirms that the serial communication is working correctly. From there, further experimentations were made using various commands to verify receiving and sending data in various formats.

I had an issue were the serial port was still being used and MATLAB referred to using the following command "instrfind".


The following code was used to solve this problem:

        if ~isempty(instrfind)
        fclose(instrfind);
        delete(instrfind);
        end       
        

Now to verify the serial communication between MATLAB and the board, writing 'x', which is the name of the serial object into the command window results in the following:


The below screen capture confirms the serial communication with open status, it also confirms the bytes available to be received, which cap at 512 bytes. Using commands like 'fread(x)', 'fgetl(x)', etc will allow receiving and reading the bytes.


MATLAB - Code

A while loop has been added in the below code which includes a 'fgetl(x)' command which reads one line of ASCII text from serial port and discards the terminator, unlike other commands which read the whole string into a MATLAB table which hits cap at 512 byte that must be cleared every time, which is not suitable for our use case. The line of code which includes 'str2double' filters out any non numerical ASCII value. The value is then graphed as a GUI using a bar and draw now function. Although 'Bar(a)' is not suited for real time data, a trick of adding two columns of 2100 allows it to seem so. Otherwise the graph would be updated in an incorrect way making it seem that 200, 500, or 700 is the maximum distance read.

Compiling the previous commands, the following code was formed:

%MATLAB Code for Serial Communication between Arduino and MATLAB
close all
clear
    if ~isempty(instrfind)
     fclose(instrfind);
      delete(instrfind);
end

x = serial('COM3','BaudRate',9600)
fopen(x);

go = true;
while go
    distanceValue = fgetl(x);
    out=str2double(regexp(distanceValue,'[\d.]+','match'))
    if out > 2100;
        out = 2100;
    end
    a = [2200 out 2200];
    bar(a);
    grid;
    drawnow;
end       
        



Assignment Files:

All codes shown above can be copied to clipboard and pasted into its respective IDE.

Files has been obtained from week 11 and week 12.