%This work is licenced under the Creative Commons Attribution-Share%Alike 3.0 Unported License. %To view a copy of this licence, visit %http://creativecommons.org/licenses/by-sa/3.0/ %or send a letter to Creative Commons, 171 Second Street, Suite 300, %San Francisco, %California 94105, USA. %By HUSSAM EL_SHEIKH %20th JULY 2009 %www.husstechlabs.com %function aserial2 %function aserial2 to display graphically acceleration% %Create serial object called "s"; %Using COMM port 6; %Baud rate 19200; %Terminator is a Line feed followed by carriage reset; %Open the serial object ready for use. s = serial ('COM6'); s.BaudRate = 19200; s.Terminator = 'LF/CR'; fopen (s) %init X=Y=Z=0 X = 0; Y = 0; Z = 0; %define output variable out = [X Y Z]; %Draw a bar chart with three bars figure; %name the figure handle "ht" ht = bar (out); %Set the limits of the Y axis, i.e. just over +- 1g ylim([-1100 1100]) ylabel('GForce (mili-G)') title('Acceleration measurements from each axis 123 = XYZ') %number of times to loop duration = 100; for t = 1 : 1 : duration, %Input the data from the serial port into "data" variable data = fscanf (s); %The data in "data" is in characters, so convert to numeric values data2 = str2num(data); %Split into X,Y,Z X = data2 (1); Y = data2 (2); Z = data2 (3); %Procedures to format the data from the accelerometer into G-force if X > 127 X = 256 - X; X = X*18*-1; else X = X*18; end if Y > 127 Y = 256 - Y; Y = Y*18*-1; else Y = Y*18; end if Z > 127 Z = 256 - Z; Z = Z*18*-1; else Z = Z*18; end %populate the "out" variable with X,Y,Z values out = [X Y Z ]; %Update the YData of the Bar chart with the new contents of "out" set(ht,'YData',out) %refresh and draw the new chart drawnow end %Clean up and shut it down fclose (s) delete (s) clear s clear all