Physics 37100 Advanced Physics Laboratory I

Lab #4

(PART I:  Wait for it…)

 

1)          Create a sketch that waits for the letter ‘?’ over the serial port.  When it receives ‘?’ it will send back ‘K’ and turn on and LED.

a.      Set a poll time of 200 μs using delayMicroseconds() to check Serial.available().

b.      Use Serial.read() to examine each character that is sent.  If the character is not ‘?’ then continue to wait.

c.      Test the sketch using the serial monitor. 

2)          Convert the sketch into a function waitFor().

a.      waitFor should take 3 inputs:

                                                              i.      A character to wait for.

                                                            ii.      A poll time, which should default to 200 μs.

                                                          iii.      A time out period, which should default to 60e6 μs.

b.      waitFor should output true if it reads the character and false if the time out period expires.

c.      Rewrite and test the sketch from 1) using your new function waitFor(‘R’).

                                                              i.      Try waiting for other characters.

                                                            ii.      Test the defaults and overrides.

3)          Make a Matlab script that sends the character ‘?’ over the serial port.

a.      using code similar to this:

 

s=serial('COM8');  % Create serial object

fopen(s);          % Open serial port

pause(2);          % Wait for port to open

 

fprintf(s,’?’);    % Send character ‘?’

 

 

%% Always close -- use: fclose(instrfind);delete(instrfind) %  to close orphans

fclose(s);

delete(s);

clear s;

 

b.      Test to make sure the LED only lights when ‘?’ is sent.

4)          Add code to the matlab script to wait for the character ‘K’ to be sent over the using fgetl() (type “doc serial/fgetl” for info). 

5)          Using the code developed above we have a way to establish communications between matlab and the arduino.  It is like a short conversation: matlab asks ‘?’ == ‘Are you ready?’ and arduino responds ‘K’ == ‘Yes, I am OK.’.

a.      Save the sketch and matlab script to send with your report.