Advertisement
Guest User

Untitled

a guest
Nov 12th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SPARK 4.99 KB | None | 0 0
  1. const int counter_button = 7;
  2. const int ledpin = 12;
  3. const int zpin = A1;
  4. const int ypin = A2;
  5. const int xpin = A3;
  6. const int groundpin = A4;
  7. const int powerpin = A5;
  8. const int threshold = 75;         //change with measured threshold (depends on individual)
  9. const int samplingRate = 15;      //sampling frequency in Hertz
  10. const int blinkInterval = 250;    //the amount of time that the LED is on for
  11.  
  12. int oneStep = 0;  // number of steps with one foot
  13. int steps = 0;    // total number of steps
  14. unsigned long startTime = 0;  //keeps track of when the LED is first turned on
  15. unsigned long currentTime;    //updated to store the current time since the program started
  16. boolean onWayUp;              //condition to track the "up" part of the signal; ...
  17. boolean isOn = false;         //... changes when the samples pass the threshold
  18. boolean LEDon;                //condition to track the state of the LED
  19.  
  20. float zval;  // z-component of acceleration as given by the accelerometer
  21. float xval;  // x-component of acceleration as given by the accelerometer
  22. float yval;  // y-component of acceleration as given by the accelerometer
  23. float zbar;  // average z-acceleration when not in motion
  24. float xbar;  // average x-acceleration when not in motion
  25. float ybar;  // average y-acceleration when not in motion
  26.  
  27. float totMag;  // the magnitude of the acceleration vector
  28.  
  29. void setup(){
  30.   pinMode(counter_button, INPUT);  
  31.   pinMode(ledpin, OUTPUT);
  32.   pinMode(zpin, INPUT);
  33.   pinMode(ypin, INPUT);
  34.   pinMode(xpin, INPUT);
  35.   pinMode(groundpin, OUTPUT);
  36.   pinMode(powerpin, OUTPUT);
  37.   Serial.begin(9600);
  38.   digitalWrite(groundpin, LOW);
  39.   digitalWrite(powerpin, HIGH);
  40. }
  41.  
  42. void loop(){
  43.   /* if switch is turned on
  44.        calibrate position, print xbar, ybar, zbar
  45.      while switch is on
  46.        read accelerometer data, calculate magnitude
  47.        detect steps, print total number of steps  */
  48.   if (digitalRead(counter_button) == 1 && isOn == false){
  49.     isOn = true;                 //stores the state of the pedometer as on
  50.     oneStep = 0;                 //resets the step count everytime it is restarted
  51.     posCal();                    //calibrates the accelerometer
  52.   }
  53.   /* following code prints the total steps and distance traveled when the switch is
  54.      turned off, then changes the state of the pedometer to off */
  55.   if (digitalRead(counter_button) == 0 && isOn){
  56.     Serial.print("Total steps taken: ");
  57.     Serial.print(oneStep*2);
  58.     Serial.print(" || Approximate distance traveled: ");
  59.     Serial.print(oneStep*6);
  60.     Serial.println(" feet");
  61.     isOn = false;
  62.   }
  63.   if (isOn)
  64.     stepDetect();                /*measures steps taken; prints them; turns on LED and
  65.                                    records the time the LED was turned on in startTime*/
  66.   currentTime = millis();        //the total time the program has been running
  67.   if (LEDon == true && currentTime - startTime > blinkInterval){
  68.     digitalWrite(ledpin, LOW);   //turns off LED when it is on and the interval has passed
  69.     LEDon = false;               //LED condition updated
  70.   }
  71.   delay(1000/samplingRate);      //sampling delay is 1000/(sampling frequency)
  72. }
  73.  
  74. void posCal(){
  75.   /* Calibrates the starting orientation and sets xbar, ybar, and zbar to values
  76.   that represent the arduino on the body before moving */
  77.   digitalWrite(ledpin, HIGH);
  78.   float xtot = 0;
  79.   float ytot = 0;
  80.   float ztot = 0;
  81.   for (int i = 0; i < 100; i++){
  82.     xval = float(analogRead(xpin));
  83.     yval = float(analogRead(ypin));
  84.     zval = float(analogRead(zpin));
  85.     xtot = xtot + xval;
  86.     ytot = ytot + yval;
  87.     ztot = ztot + zval;
  88.     delay(1);
  89.   }
  90.   xbar = xtot/100;
  91.   ybar = ytot/100;
  92.   zbar = ytot/100;
  93.   Serial.print(xbar);
  94.   Serial.write('\t');
  95.   Serial.print(ybar);
  96.   Serial.write('\t');
  97.   Serial.print(zbar);
  98.   Serial.write('\n');
  99.   digitalWrite(ledpin, LOW);
  100. }
  101.  
  102. void stepDetect(){
  103.   /* This function uses the running totMag value to determine if there
  104.   has been a step, and if there has, the step counter is increased by 1
  105.   and an LED blink begins (records the time the LED was turned on in
  106.   order to blink the LED without changing the time between samples */
  107.   xval = float(analogRead(xpin));    
  108.   yval = float(analogRead(ypin));
  109.   zval = float(analogRead(zpin));
  110.   //xval, yval, and zval are used to measure totMag each sample
  111.   totMag = sqrt((xval-xbar)*(xval-xbar) + (yval-ybar)*(yval-ybar) + (zval-zbar)*(zval-zbar));
  112.   if (totMag > threshold && onWayUp == false){
  113.      onWayUp = true;    
  114.      oneStep++;    //increments the step counter each time the magnitude goes above the threshold
  115.      Serial.print("Step cycle count: ");
  116.      Serial.println(oneStep);          
  117.      digitalWrite(ledpin, HIGH);  //turns LED on
  118.      LEDon = true;                //boolean condition to keep track of LED's state
  119.      startTime = millis();        //records the time the LED was turned on
  120.   }
  121.   if (totMag < threshold && onWayUp == true)
  122.      onWayUp = false;  //notes that a single step is no longer being measured
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement