You might have tried to do some arithmetic in a Codecraft and got the wrong results.
For example, perhaps you would like to convert a temperature sensor reading from Celsius to Fahrenheit.
°F = (°C × 9/5) + 32
Seems easy enough, but it might not be working in your Codecraft sketch.
The problem is about how Arduino (and the C language) does arithmetic. And a Codecraft bug. It's about data types.
Example:
You should reasonably expect that this operation results in the value 1.8.
But, it doesn't; it's 1.
Codecraft generates this Arduino/C code from this sketch: (note that "// text ..." is the comment from the sketch.)
float x;
void setup(){
Serial.begin(19200);
// 9 / 5
x = (9 / 5);
Serial.println(x);
// 9.0 / 5.0
x = (9 / 5);
Serial.println(x);
// 9.000001 / 5.000001
x = (9.000001 / 5.000001);
Serial.println(x);
}
void loop(){
}
And, the Arduino serial monitor displays: (press reset if you don't see the output)
1.00
1.00
1.80
Oops! Can you find bug?
Codecraft sees "9.0" as "9." And generates:
x = (9 / 5);
That's the wrong code, because "9" and "5" as treated as integers, not floating point.
So, as integers, 9 / 5 = 1. (not 1.8)
(Note that "x" is a float, so it prints as a floating point -- 1.00);
So, we need a workaround sketch to avoid running into this Codecraft bug.
One way is to simply use "1.8" instead of having the code do the division:
(You can download it: Temperature_C-to-F.cdc)