if (soc > 90)
{
Wire.write(255);
break;
}
else if (soc < 90)
{
Wire.write(100); //
break;
}
else if (soc < 85)
{
Wire.write(65); //
break;
}
I added the brake function so that the following statement was not executed.
The compiler reports that the break statement is not within the loop. What could be causing this?
Suppose you want to sift stones for sizes between 10, 40, 50, 80 (and below and above).
You could make "if (stone > 40 && stone < 50)" for every condition. However, you can also make sifters on top of each other, letting the smaller ones fall through into the next sifter.
if (stone > 80) {
// stone is above 80
...
} else if ( stone > 50 ) {
// stone is between 50 and 80
...
} else if ( stone > 40 ) {
// stone is between 40 and 50
...
} else if ( stone > 10 ) {
// stone is between 10 and 40
...
} else {
// stone is below 10
...
}
It means that if a stone 100, only one 'if'-condition is used, and the rest is skipped. However, when a stone is 12, it has to pass 4 sifters (4 'if'-conditions). There is no other way. It is normal programming. The processor can do that very fast.
Why timing is extremely critical, perhaps the 'stone' value can be re-calculated for a 'case'-statement or with a table. However, running through a few 'if'-statements is also very fast.
First, break is only used in loop structures and switch/case. Second, I don't think the code is going to work the way you want it to. For example, suppose soc is 83. The first if condition is false, so it falls into the second test, which is true. Note that, even thought the third condition is also true, you will never reach it. You need to reorder your tests and leave out the break keyword. It isn't needed.
if (soc < 85) { // Less than 85
Wire.write(65);
} else if (soc => 85 && soc < 90) { // Between 85 and 90
Wire.write(100);
} else { // 90 and above
Wire.write(255);
}