简体   繁体   中英

Arduino - What is [./n.]?

I was making a dimmer in arduino using a potentiometer and I found a website that told me to convert the Analog values (0-1023) to Voltage (0-255). The line that converted simply divided 255 by 1023 and multiplied it with the potentiometer reading. The line for this was ledWrite = (255./1023.) * potRead; . I tried to write is as ledWrite = (255/1023) * potRead; , with the periods, but the code wouldn't run.

What's ./n. ?

The assignment

ledWrite = (255./1023.) * potRead;

is equivalent to

ledWrite = (255.0 / 1023.0) * potRead;

That is, the trailing zero can be omitted .

On the Arduino Uno , the constants are interpreted as doubles and the result of the division is a double value.

At the present time , note that a double and a float have the same precision ( 4 bytes ) on most Arduino boards, with the exception of the Arduino Due .


In the following line

ledWrite = (255/1023) * potRead;

the two numeric literals are interpreted as integers and the division operation is the one among integers , which in this case always returns 0 since 255 is smaller than |1023| .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM