Jonathan Oxer
[Blog]
Blog > Water flow gauge and wierd bug when adding floats
>> Water flow gauge and wierd bug when adding floats
Sat, Jul 25th 11:27pm 2009: Practical Arduino
Original posted on Practical Arduino I've just finished off what was meant to be one of the simplest projects in the book, and from an electrical point of view it is: there are exactly 2 parts required other than an Arduino and basic stuff like wire / headers, and one of those parts is a resistor. The other part is the flow gauge itself. Simple, right?
Yeah, I thought so too. Problem is that once I got into writing up the project I discovered that I had to explain what an interrupt is and how it works. That naturally leads into an explanation of the "volatile" keyword, and by that point you're in a whole world of pain if this is really intended to be a "beginner" project. On the original table of contents it's listed as project #2, right near the start of the "easy" section.
I have a feeling it may need to be reclassified and pushed further down the list.
The other interesting thing that came out of it is I had a bug that if I defined two floats with non-zero values and summed them together the answer was 0. The code wasn't quite like this, but conceptually the code was doing:
float flowLitres = 1.3;
float totalLitres = 0.4;
totalLitres += flowLitres;
and getting a result of 0.
Stupefying.
I assumed I was just having a brain-fart moment and had done something noobish that I couldn't see because it was staring me in the face, so I punted the code to Hugh who saw the exact same symptoms and couldn't figure out why. He even poked around in the ASM to see what stupid tricks the compiler might be trying to pull and couldn't spot why it was happening. Reassuring in a way, because it means I wasn't just being dumb after all!
At that point pragmatism took over and I simply multiplied everything by 1000 and started working in Millilitres and using ints, but I hate not knowing what caused the problem in the first place.
Original posted on Practical Arduino I've just finished off what was meant to be one of the simplest projects in the book, and from an electrical point of view it is: there are exactly 2 parts required other than an Arduino and basic stuff like wire / headers, and one of those parts is a resistor. The other part is the flow gauge itself. Simple, right?
Yeah, I thought so too. Problem is that once I got into writing up the project I discovered that I had to explain what an interrupt is and how it works. That naturally leads into an explanation of the "volatile" keyword, and by that point you're in a whole world of pain if this is really intended to be a "beginner" project. On the original table of contents it's listed as project #2, right near the start of the "easy" section.
I have a feeling it may need to be reclassified and pushed further down the list.
The other interesting thing that came out of it is I had a bug that if I defined two floats with non-zero values and summed them together the answer was 0. The code wasn't quite like this, but conceptually the code was doing:
float flowLitres = 1.3;
float totalLitres = 0.4;
totalLitres += flowLitres;
and getting a result of 0.
Stupefying.
I assumed I was just having a brain-fart moment and had done something noobish that I couldn't see because it was staring me in the face, so I punted the code to Hugh who saw the exact same symptoms and couldn't figure out why. He even poked around in the ASM to see what stupid tricks the compiler might be trying to pull and couldn't spot why it was happening. Reassuring in a way, because it means I wasn't just being dumb after all!
At that point pragmatism took over and I simply multiplied everything by 1000 and started working in Millilitres and using ints, but I hate not knowing what caused the problem in the first place.[ Back to top ]
