As the title says... Chat on...

User avatar
By dilettante
#75524 Integer firmware build. Nothing connected to A0 pin.

Custom terminal where Ctrl-Enter causes response text to display in hex until the next Enter key-press.

Code: Select all> uart.write(0, adc.read(0)) {Ctrl-Enter}
01 3E 20 {Enter} <-- Note: 3E 20 is the LUA prompt.
> uart.write(0, 0x0) {Ctrl-Enter}
00 3E 20 {Enter}
> uart.write(0, 0) {Ctrl-Enter}
00 3E 20 {Enter}
> =adc.read(0) {Enter}
0
>


Why do I get an 01 instead of a 00?
User avatar
By dilettante
#75529 Ok, perhaps the value was merely drifting between attempts and I happened to get 1 at times I tried sending binary data.

Some new trials, this time dealing with the whole 16-bit value as 2 bytes:

Code: Select all> a=adc.read(0)
> uart.write(0, a/256, a%256)
00 00 3E 20
> a=adc.read(0)
> uart.write(0, a/256, a%256)
00 01 3E 20
>


So perhaps there is no issue here after all, aside from noting that this works for an integer firmware build and if you use a float build you'll probably want to use:

Code: Select all> uart.write(0, math.floor(a/256), a%256)


.. unless somebody knows that the conversion to Byte values always handles that. I haven't experimented yet with a float build or I'd test it myself.
User avatar
By dilettante
#75534 Considering LUA's loose and floppy notion of a "number" I suppose to be safe you'd even have to do something like:

Code: Select alla=adc.read(0)
uart.write(0, a/256%256, a%256)


... in order to write the value as a two-byte big-endian output stream reliably.

Looking at adc.c (the library source code) this isn't necessary though.
User avatar
By Alexsander Magalhães
#75560
dilettante wrote:Nothing connected to A0 pin.


Hi friend, have you tried to pull down/up this pin before reading adc?
If not, the most comon case for those fluctuations are noises from your very environment.

Look:
https://imgur.com/a/u4c0ETT
You can see the pin fluctuates alot when I put my finger on it but it almost never becomes zero.

To the flopiness :D of Lua, you may try this:
Code: Select all(0.00000000000000001 > 0) == true


And you'll see it still recognises a one on the 17th decimal place as true.
Hope this helps somehow