A way to get degrees to display to the half degree

Please forgive me if I'm posting this in the wrong forum-category.
But below is a little snippet of code I plan on using to display degrees - with resolution to the half-degree.
See attachment

A very interesting question, yes you can but will it be accurate. A schematic, not a fritzy thing, would be helpful, at least that way we can get an idea of what sensor you are using and is it capable of at least 1/2 degree resolution.

If the floating point number x is always positive, this will display x to the nearest 0.5 unit value:

Serial.println( (float) ((int)(2.0*x+0.5))/2.0, 1);
 // (or)
Serial.println( (round(x*2.0))/2.0, 1);

jremington:
If the floating point number x is always positive, this will display x to the nearest 0.5 unit value:

Serial.println( (float) ((int)(2.0*x+0.5))/2.0, 1);

// (or)
Serial.println( (round(x*2.0))/2.0, 1);

That is cool!
I haven't tried these in the Arduino interface - but in Excel I get:
X    |  RESULT

1    | 1
1.25  | 1.3
1.5  | 1.5
1.75  | 1.8
2    | 2








| 1 | 1 |
| - | - |
| 1.25 | 1.3 |
| 1.5 | 1.5 |
| 1.75 | 1.8 |
| 2 | 2 |

The Arduino is programmed in C/C++.

void setup() {
Serial.begin(9600);
while(!Serial);
for(float x=0.0; x<2.8; x+=0.2) {
  Serial.print(x,2);
  Serial.print(" -> ");
  Serial.println( (round(x*2.0))/2.0, 1);
  } 
}

void loop() {}

Output

0.00 -> 0.0
0.20 -> 0.0
0.40 -> 0.5
0.60 -> 0.5
0.80 -> 1.0
1.00 -> 1.0
1.20 -> 1.0
1.40 -> 1.5
1.60 -> 1.5
1.80 -> 2.0
2.00 -> 2.0
2.20 -> 2.0
2.40 -> 2.5
2.60 -> 2.5

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.