%%%%%%%%%%%%%%%%%%%%%
%  ESE 111 - Final Project  %
%  Step Counter code  %
%%%%%%%%%%%%%%%%%%%%%

a.pinMode(15,'input');
a.pinMode(16,'input');
a.pinMode(17,'input');
a.pinMode(18,'output');
a.pinMode(19,'output');
a.digitalWrite(18,0);
a.digitalWrite(19,1);
xval = zeros();
yval = zeros();
zval = zeros();
pause(.001);

% This section computes values of xbar, ybar, and zbar (calibration)

for i = 1:50
xval(i) = a.analogRead(3);
yval(i) = a.analogRead(2);
zval(i) = a.analogRead(1);

pause(.001)

end

[x1,x2] = size(xval);
[y1 y2] = size(yval);
[z1 z2] = size(zval);

xbar = sum(xval) / x2;
ybar = sum(yval) / y2;
zbar = sum(zval) / z2;


%% Re-initialize xval, yval, and zval

xval = zeros();
yval = zeros();
zval = zeros();
totvect = zeros();
totave = zeros();

xval(1) = 0;
yval(1) = 0;
zval(1) = 0;

% This section computes the magnitude of the acceleration vector, and then plots it 

for i = 2:300
xval(i) = a.analogRead(3);
yval(i) = a.analogRead(2);
zval(i) = a.analogRead(1);

totvect(i) = sqrt((xval(i)-xbar).^2 + (yval(i) - ybar).^2 + (zval(i) - zbar).^2);
totave(i) = (totvect(i) + totvect(i-1)) ./ 2 ;

subplot(2,1,1)
plot(totvect, 'b');
title('acceleration');
subplot(2,1,2)
plot(totave)
title('acceleration average')
pause(.001)

end