Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The sensor outputs provided by the library are the raw 16-bit values
- obtained by concatenating the 8-bit high and low accelerometer and
- magnetometer data registers. They can be converted to units of g and
- gauss using the conversion factors specified in the datasheet for your
- particular device and full scale setting (gain).
- Eyample: An LSM303D gives a magnetometer y axis reading of 1982 with
- its default full scale setting of +/- 4 gauss. The M_GN specification
- in the LSM303D datasheet (page 10) states a conversion factor of 0.160
- mgauss/LSB (least significant bit) at this FS setting, so the raw
- reading of -1982 corresponds to 1982 * 0.160 = 317.1 mgauss =
- 0.3171 gauss.
- In the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data
- registers actually contain a left-aligned 12-bit number, so the lowest
- 4 bits are always 0, and the values should be shifted right by 4 bits
- (divided by 16) to be consistent with the conversion factors specified
- in the datasheets.
- Example: An LSM303DLH gives an accelerometer Z axis reading of -16144
- with its default full scale setting of +/- 2 g. Dropping the lowest 4
- bits gives a 12-bit raw value of -1009. The LA_So specification in the
- LSM303DLH datasheet (page 11) states a conversion factor of 1 mg/digit
- at this FS setting, so the value of -1009 corresponds to -1009 * 1 =
- 1009 mg = 1.009 g.
- */
- #include <Wire.h>
- #include <LSM303.h>
- LSM303 compass;
- char report[80];
- int start = 1,initial = 100,xMax , yMax, zMax , xMin, yMin , zMin;
- int xArr [100],yArr[100],zArr[100];
- int xArrSm [34],yArrSm[34],zArrSm[34];
- int movingNum =0;
- int average =0;
- int SumArr[4];
- int steps=0 , counter =1 , current , prev,endIndex=0 , last , lastLast, stepDist = 40, distance =0,calories = 0, velocity,weight = 62;
- void setup()
- {
- Serial.begin(9600);
- Wire.begin();
- compass.init();
- compass.enableDefault();
- }
- void loop()
- {
- for (int i = 1 ; i<initial && start;i++){
- compass.read();
- snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d",
- compass.a.x, compass.a.y, compass.a.z,
- compass.m.x, compass.m.y, compass.m.z);
- Serial.println(report);
- xArr[i] = compass.a.x;
- yArr[i] = compass.a.y;
- zArr[i] = compass.a.z;
- //smooth data
- if (i %3 == 0 ){
- if(i/3 == 1 ){
- Serial.print("just for test about 10sec get ready start moving!!");
- xArrSm[i/3] = (xArr[i] + xArr[i-1] + xArr[i-2])/3;
- yArrSm[i/3] = (yArr[i] + yArr[i-1] + yArr[i-2])/3;
- zArrSm[i/3] = (zArr[i] + zArr[i-1] + zArr[i-2])/3;
- xMax = xArrSm[i/3], yMax= yArrSm[i/3], zMax =zArrSm[i/3], xArrSm[i/3], yMin=yArrSm[i/3] , zMin=zArrSm[i/3] ;
- }else{
- xArrSm[i/3] = (xArr[i] + xArr[i-1] + xArr[i-2])/3;
- yArrSm[i/3] = (yArr[i] + yArr[i-1] + yArr[i-2])/3;
- zArrSm[i/3] = (zArr[i] + zArr[i-1] + zArr[i-2])/3;
- if (xArrSm[i/3] > xMax){
- xMax = xArrSm[i/3] ;
- }else if(xArrSm[i/3] < xMin){
- xMin = xArrSm[i/3];
- }
- if (yArrSm[i/3] > yMax){
- yMax = yArrSm[i/3];
- }else if(yArrSm[i/3] < yMin){
- yMin =yArrSm[i/3];
- }
- if (zArrSm[i/3] > zMax){
- zMax = zArrSm[i/3];
- }else if(zArrSm[i/3] < zMin){
- zMin = zArrSm[i/3] ;
- }
- }
- }
- endIndex = i;
- delay(100);
- }
- //calculate average
- if (start){
- int xDiff = xMax-xMin , yDiff = yMax-yMin , zDiff = zMax-zMin;
- int xSum = xMax+xMin , ySum = yMax+yMin , zSum = zMax+zMin;
- SumArr[1] = xSum; SumArr[2] = ySum; SumArr[3] = zSum;
- int maxNum = xDiff;
- movingNum = 1 ;
- if (yDiff > maxNum){
- maxNum = yDiff;
- movingNum = 2;
- }
- if (zDiff > maxNum){
- maxNum = zDiff;
- movingNum = 3 ;
- }
- average = SumArr[movingNum]/2;
- Serial.println("we will start right now!!");
- }
- start = 0 ;
- compass.read();
- snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d",
- compass.a.x, compass.a.y, compass.a.z,
- compass.m.x, compass.m.y, compass.m.z);
- Serial.println(report);
- if(movingNum == 1){
- if(counter % 3 == 0){
- if ( counter == 3){
- current = last + lastLast + compass.a.x;
- prev = current;
- } else{
- prev = current;
- current = last + lastLast + compass.a.x;
- }
- if(prev>current && prev >average && current < average){
- steps++;
- distance = stepDist * steps;
- Serial.print("steps updated:");
- Serial.println(steps);
- Serial.print("distance travelled updated:");
- Serial.println(distance);
- Serial.print("calories:");
- calories = velocity * weight/400;
- Serial.println(calories);
- }
- }else if(counter % 3 == 1){
- last = compass.a.x;
- }else if(counter % 3 == 2){
- lastLast = compass.a.x;
- }
- counter++;
- }else if (movingNum == 2){
- if(counter % 3 == 0){
- if ( counter == 3){
- current = last + lastLast + compass.a.y;
- prev = current;
- } else{
- prev = current;
- current = last + lastLast + compass.a.y;
- }
- if(prev>current && prev >average && current < average){
- steps++;
- distance = stepDist * steps;
- Serial.print("steps updated:");
- Serial.println(steps);
- Serial.print("distance travelled updated:");
- Serial.println(distance);
- Serial.print("calories:");
- calories = velocity * weight/400;
- Serial.println(calories);
- }
- }else if(counter % 3 == 1){
- last = compass.a.y;
- }else if(counter % 3 == 2){
- lastLast = compass.a.y;
- }
- counter++;
- }else if (movingNum == 3){
- if(counter % 3 == 0){
- if ( counter == 3){
- current = last + lastLast + compass.a.z;
- prev = current;
- } else{
- prev = current;
- current = last + lastLast + compass.a.z;
- }
- if(prev>current && prev >average && current < average){
- steps++;
- distance = stepDist * steps;
- Serial.print("steps updated:");
- Serial.println(steps);
- Serial.print("distance travelled updated:");
- Serial.println(distance);
- Serial.print("calories:");
- calories = velocity * weight/400;
- Serial.println(calories);
- }
- }else if(counter % 3 == 1){
- last = compass.a.z;
- }else if(counter % 3 == 2){
- lastLast = compass.a.z;
- }
- counter++;
- }
- //Serial.print("x:");
- //Serial.println(xx);
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement