lround()
[Math]
long lround(double x);
Description
Returns the value of x rounded to the nearest integer value, but rounds halfway cases away from zero.
Parameters
x
: The value to calculate the rounded value.
Returns
The value of x rounded to the nearest integer value as a long integer.
Example Code
The code reads the value of a potentiometer connected to pin
IN
and place the rounded value of x on pin
OUT
.
void setup() {
(0, "IN"); // set the label of pin 0 to "IN"
pinLabel(1, "OUT"); // set the label of pin 1 to "OUT"
pinLabel(1, OUTPUT); // set pin 1 as output
pinMode(2, "VCC"); // set the label of pin 2 to "VCC"
pinLabel(2); // set pin 2 as power pin
powerPin(3, "GND"); // set the label of pin 3 to "GND"
pinLabel(3); // set pin 3 as ground pin
groundPin}
void loop() {
int val = analogRead(0); // read the value of the potentiometer
double x = map(val, 0, 1023, -5, 5); // map the value
long y = lround(x); // calculate the rounded value of x
int pwm = (int) map(y, -5, 5, 0, 1023); // map the value
(1, pwm); // write the value of pwm to pin 1
analogWrite}
Notes and Warnings
- If
x
is not a finite number or an overflow occurs, this function returns theLONG_MIN
value (0x80000000
).