Please help me, friends. I have finished making a steps counter. My problem now is that my timer starts with the accelerometer, but it doesn't stop when the accelerometer stops.(Pedometer is an application where the user can count his\her steps. With this information, the user can count his total calories consumed, and the total elapsed time.) The problem is that my timer is not working properly. Please help me out.
here is code
if ( fabsf(acceleration.y) > progressAsInt ) {
steps++;
timer1 = [NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:@selector(timepassed) userInfo:nil repeats:YES];
NSString *stepcount = [NSString stringWithFormat:@"%d", steps];
[stepstaken setText:stepcount];
//it's for the MPH speed calculation formula changes can be happen
/*formula mph= miles/hour
0.5miles
10minutes = 10/60 hours = 0.1667
mph=0.5/0.1667*/
mspeed = (TotalDistance *3600)/60;
//mspeed =(steps*steplength)/63360*60;
NSString *speed = [NSString stringWithFormat:@"%2.2f",mspeed];
[mSpeed setText:speed];
//this for calculation of miles
TotalDistance = (steps *steplength)/1500;
NSString *distance =[NSString stringWithF开发者_C百科ormat:@"%5.3f",TotalDistance];
[miles setText:distance];
if (steps%20==0)
{
//And this for calorie calculation
cal++;
NSString *cal1 = [NSString stringWithFormat:@"%d",cal];
[calorie setText:cal1];
}else{
[timer1 invalidate];
}
}
I fixed the indentation of your code to match its structure. It should be pretty obvious to you now that the else
is in the wrong place if you are trying to invalidate the timer when the acceleration is below a threshold.
精彩评论