Bizarre Problem with Math and Heart Rate in ConnectIQ SDK

Hi,

The portion of code that I've posted is supposed to come up with an average heart rate variable. The problem I'm having is that every time that the average gets within about 10 bpm to the current HR (only from the low end. It will decrease to a lower average just fine, but it won't increase), it is no longer affected by the current heart rate and stays where it is. The math formula I wrote in here works fine, I tested it by hand with no problem. I was hoping someone could point something I perhaps missed as someone newer to the Garmin environment? Maybe there's a problem with rounding that I'm unaware of?

Thanks!

All variables are set by default to 0, and this is located in a method the runs each second.

myHR = Info.getActivityInfo().currentHeartRate;
    	mySaturation = Info.getActivityInfo().currentOxygenSaturation;
    	
    	
    	if(myHR != null) {
    		secondDivisor += 1;
    		if(myAverageHR == 0) {
    			myAverageHR = myHR;
    			Sys.println(secondDivisor);
    			Sys.println(myHR);
    			Sys.println(myAverageHR);
    			Sys.println("---------------------");
    		}
    		else {
    		myAverageHR = ((myAverageHR * secondDivisor) + myHR)/(secondDivisor + 1);
    		Sys.println(secondDivisor);
    		Sys.println(myHR);
    		Sys.println(myAverageHR);
    		Sys.println("---------------------");
    		}

  • Try

    myAverageHR = ((myAverageHR * secondDivisor) + myHR).toFloat()/(secondDivisor + 1);

    I haven't tested this, but it seems that all variables are int's and therefore the result is cutted to an int.

  •  is on the right track. You need the result to be a floating point so that it isn't truncated to the next lowest whole number. The part that he not handling is that the result is now a float and you are probably not going to want to display it as such. You will probably want to round to the nearest whole number value as opposed to always rounding down. This will do exactly that.

    var displayValue = Math.round(myAverageHR); // round to nearest whole number