I have this simple function that produces 2 float values sum and product from which i calculate another varaible NodePrio. I had the value of NodePrio coming wrong so i decided to put in some console debug messages to read these values as they were being modified in the loop Here is the code
void cycPrioCalc(){
float sum = 0.0, product = 1.0;
for (int i = 0; i< 4; i++){
if (loads[i].state == LOW){
if (loads[i].dynPrio > 1.0){
loads[i].dynPrio -= Pstep;
}
if (loads[i].dynPrio < 1.0){ // stop overshoot
loads[i].dynPrio = 1.0;
}
}
else if (loads[i].state == HIGH){
if (loads[i].dynPrio < loads[i].fixPrio){
loads[i].dynPrio += Pstep;
}
if (loads[i].dynPrio > loads[i].fixPrio){ // stop overshoot
loads[i].dynPrio = loads[i].fixPrio;
}
}
// CAlculate the priority of the node
sum += loads[i].dynPrio;
Serial.print(sum);
Serial.print("\n");
product *= loads[i].dynPrio;
Serial.print(product);
Serial.print("\n");
}
NodePrio = product / sum;
}
The values for loads[i].dynPrio goes 1.0, 2.0,3.0 and 4.0 for the 4 elements of the loop. so according to this logic at the end of the loop sum should be 1.0+2.0+3.0+4.0 = 10.0 and product should be 1.0 * 2.0 * 3.0 * 4.0 = 24.0
and NodePrio should be __ 24.0 / 10.0 = 2.40 __
strangely, what i receive in serial output are the last values that were computed,
** sum = 4.0** and ** product = 1.0 ** thus setting **NodePrio = 0.25 **
here is the serial output
1.00 -> Sum at i=0
1.00 -> Product at i=0
2.00 -> Sum at i=1
1.00 -> Product at i=0
3.00 -> Sum at i=2
1.00 -> Product at i=0
4.00 -> Sum at i=3
1.00 -> Product at i=0
0.25 -> NodePrio
First place to look was the braces, which look fine to me. All help is apprecieated.
loads[i].dynPrio
andloads[i].state
they are probably not what you expect. – Craig Dec 5 '14 at 22:03