Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int counter_button = 7;
- const int ledpin = 12;
- const int zpin = A1;
- const int ypin = A2;
- const int xpin = A3;
- const int groundpin = A4;
- const int powerpin = A5;
- const int threshold = 75; //change with measured threshold (depends on individual)
- const int samplingRate = 15; //sampling frequency in Hertz
- const int blinkInterval = 250; //the amount of time that the LED is on for
- int oneStep = 0; // number of steps with one foot
- int steps = 0; // total number of steps
- unsigned long startTime = 0; //keeps track of when the LED is first turned on
- unsigned long currentTime; //updated to store the current time since the program started
- boolean onWayUp; //condition to track the "up" part of the signal; ...
- boolean isOn = false; //... changes when the samples pass the threshold
- boolean LEDon; //condition to track the state of the LED
- float zval; // z-component of acceleration as given by the accelerometer
- float xval; // x-component of acceleration as given by the accelerometer
- float yval; // y-component of acceleration as given by the accelerometer
- float zbar; // average z-acceleration when not in motion
- float xbar; // average x-acceleration when not in motion
- float ybar; // average y-acceleration when not in motion
- float totMag; // the magnitude of the acceleration vector
- void setup(){
- pinMode(counter_button, INPUT);
- pinMode(ledpin, OUTPUT);
- pinMode(zpin, INPUT);
- pinMode(ypin, INPUT);
- pinMode(xpin, INPUT);
- pinMode(groundpin, OUTPUT);
- pinMode(powerpin, OUTPUT);
- Serial.begin(9600);
- digitalWrite(groundpin, LOW);
- digitalWrite(powerpin, HIGH);
- }
- void loop(){
- /* if switch is turned on
- calibrate position, print xbar, ybar, zbar
- while switch is on
- read accelerometer data, calculate magnitude
- detect steps, print total number of steps */
- if (digitalRead(counter_button) == 1 && isOn == false){
- isOn = true; //stores the state of the pedometer as on
- oneStep = 0; //resets the step count everytime it is restarted
- posCal(); //calibrates the accelerometer
- }
- /* following code prints the total steps and distance traveled when the switch is
- turned off, then changes the state of the pedometer to off */
- if (digitalRead(counter_button) == 0 && isOn){
- Serial.print("Total steps taken: ");
- Serial.print(oneStep*2);
- Serial.print(" || Approximate distance traveled: ");
- Serial.print(oneStep*6);
- Serial.println(" feet");
- isOn = false;
- }
- if (isOn)
- stepDetect(); /*measures steps taken; prints them; turns on LED and
- records the time the LED was turned on in startTime*/
- currentTime = millis(); //the total time the program has been running
- if (LEDon == true && currentTime - startTime > blinkInterval){
- digitalWrite(ledpin, LOW); //turns off LED when it is on and the interval has passed
- LEDon = false; //LED condition updated
- }
- delay(1000/samplingRate); //sampling delay is 1000/(sampling frequency)
- }
- void posCal(){
- /* Calibrates the starting orientation and sets xbar, ybar, and zbar to values
- that represent the arduino on the body before moving */
- digitalWrite(ledpin, HIGH);
- float xtot = 0;
- float ytot = 0;
- float ztot = 0;
- for (int i = 0; i < 100; i++){
- xval = float(analogRead(xpin));
- yval = float(analogRead(ypin));
- zval = float(analogRead(zpin));
- xtot = xtot + xval;
- ytot = ytot + yval;
- ztot = ztot + zval;
- delay(1);
- }
- xbar = xtot/100;
- ybar = ytot/100;
- zbar = ytot/100;
- Serial.print(xbar);
- Serial.write('\t');
- Serial.print(ybar);
- Serial.write('\t');
- Serial.print(zbar);
- Serial.write('\n');
- digitalWrite(ledpin, LOW);
- }
- void stepDetect(){
- /* This function uses the running totMag value to determine if there
- has been a step, and if there has, the step counter is increased by 1
- and an LED blink begins (records the time the LED was turned on in
- order to blink the LED without changing the time between samples */
- xval = float(analogRead(xpin));
- yval = float(analogRead(ypin));
- zval = float(analogRead(zpin));
- //xval, yval, and zval are used to measure totMag each sample
- totMag = sqrt((xval-xbar)*(xval-xbar) + (yval-ybar)*(yval-ybar) + (zval-zbar)*(zval-zbar));
- if (totMag > threshold && onWayUp == false){
- onWayUp = true;
- oneStep++; //increments the step counter each time the magnitude goes above the threshold
- Serial.print("Step cycle count: ");
- Serial.println(oneStep);
- digitalWrite(ledpin, HIGH); //turns LED on
- LEDon = true; //boolean condition to keep track of LED's state
- startTime = millis(); //records the time the LED was turned on
- }
- if (totMag < threshold && onWayUp == true)
- onWayUp = false; //notes that a single step is no longer being measured
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement