User:MatteoP mp6/sandbox

This is my code and we will do a walk through like the one in my Major Project submission but more detailed for anyone interested.

I will use (//) To talk everything else is MATLAB CODE

//This is the simple function we learned in class to connect the Arduino to the grove-board %Connect to Arduino a=arduino('/dev/cu.usbserial-0001','uno');

//A function I found @Saeed to connect the MP-2950 accelerometer I had to purchase independently to due the one on the grove-board "not working". %Connect to Accelerometer imu = mpu9250(a);

//I created an array to store my values, so I can later index through my array and takes averages of y and z % Create array (x,y,z) y=[0,0,0]; z=[0,0,0];

//I use i to index through real time values and I need to initialize is here %Initialize parameters i = 1;

//Main while loop that encompasses my function while (true) //Set a variable (aR) as the output from the accelerometer, this allows me to track its value at store it as "aR" %Take acceleration from MPU-9250 aR= readAcceleration(imu); //Set the first column of aR (y-axis, can be proven in video), and second column (z-axis) to y and z respectively, but also making it different than //before, i changes through every iteration and the first y value is different than the second, which is different than the third, this is all stored in  //the array i set previously %Initialize ith index of each axis array y(i) = aR(1); z(i) = aR(2); //Make sure i changes after every loop %Set paramaters i = i+1; //Main loop, in infinite loop, when i==4 or when the array has recorded 3 values and stores them in the array, the program is allowed to move on. %Iterate though 3 values to get accurate measurement if (i == 4) //record value of button, this is to test to see if I should stop (return) my program completely. stop = readDigitalPin(a,'D6'); %Button stops sound if pressed if (stop == 1) %Stop program return end

//Take an average of the y and z values from the array, use the mean method %Take average and set them in each array avg_y = (y(1) + y(2) + y(3))/3; avg_z = (z(1) + z(2) + z(3))/3; //set i=1 again so when we start the iteration again, the array can be full of new values i = 1;

//check if the average of the z and y values are greater than my threshold values, in this case more than 9.15 or not in (-2 9.15 || ~(avg_y < 2 && avg_y > -2)))) %initialize parameter %start time //Start a timer to record time start_time=tic;

//Set a time for the timer to end, in this case, 5 seconds end_time = 5; %Timer

//Receptive loop that repeats until the timer is greater than the end_time, which was 5 second, meaning the program will wait 5 seconds before //continuing while (toc(start_time)<end_time) end

//Take an average of the values again, this way the values will be different than they were before the timer, by doing this it gives the //program to re-test the values to see if they are in-between the desired threshold. for c=1:3 %Take acceleration from MPU-9250 aR= readAcceleration(imu); %Initialize ith index of each axis array y(c) = aR(1); z(c) = aR(2); end avg_y = (y(1) + y(2) + y(3))/3; avg_z = (z(1) + z(2) + z(3))/3;

//Initialize value that will show up later on           fix=0;

//This part of the program tests my threshold again, AFTER THE TIMER, meaning that if the z and y values deviated out of the threshold 5 //seconds passed and we if it is still outside of the threshold, the individual is most likely slouching or leaning. while (fix == 0) if ((avg_z < 9.15 && (avg_y < 2 && avg_y > -2))) //parameter to leave loop, only if function is in-between the threshold value. fix = 1; //leave function, continue rest of program break; else

// Make a sound using the method we learned form class %Make a sound writePWMDutyCycle(a,'D5',0.25); pause(0.2); writeDigitalPin(a,'D5',0); pause(0.5); writePWMDutyCycle(a,'D5',0.25); pause(0.2); writeDigitalPin(a,'D5',0); pause(0.5); writePWMDutyCycle(a,'D5',0.25); pause(0.2); writeDigitalPin(a,'D5',0); pause(0.5); //Take average again, to see if the average value of the z or y axis is returned back the desired position c=1; for c=1:3 %Take acceleration from MPU-9250 aR= readAcceleration(imu); %Initilize ith index of each axis array y(c) = aR(1); z(c) = aR(2); end avg_y = (y(1) + y(2) + y(3))/3; avg_z = (z(1) + z(2) + z(3))/3; end end end end end

//Hope you enjoyed this walkthrough!!!