PDA

View Full Version : read GPIO output



jeanclaudej971
April 28th, 2017, 08:04 PM
Hello

I just noticed that the fact of reading a GPIO output, made it go to 0 is this a bug where I did not understand something?
* "Print (raspi read (24))" always gives as output regardless of the state of output 0.
So scripting does not work.

local raspi = require("raspi")
raspi.export( 18 )
raspi.direction( 18, true )
raspi.read( 18 )

local pin = raspi.read( 18 )
if pin == 1 then
raspi.write( 18, 0 )
else
raspi.write( 18, 1 )
end


Then i have help please?

Ron
April 30th, 2017, 05:12 PM
Probably trying to read from a GPIO pin that is set to output doesn't produce what you expected. What are you trying to do?

jeanclaudej971
April 30th, 2017, 05:46 PM
Hello Ron
*
I am learning lua and I want to change the state of output gpio 18.
If gpio18 is set to 0 make it pass 1 and if to 1 pass to 0.
Thank you for your reply.

:)cordially

Ron
April 30th, 2017, 05:54 PM
I see you wish to toggle the output. OK the problem you had is that calling export on a pin resets it's values. So only call export once per Girder session. Like so:



raspi = require("raspi")

if not raspi.pin18exported then
raspi.export( 18 )
raspi.direction( 18, true )
raspi.pin18exported = true
end


local pin = raspi.read( 18 )
print("Before Toggle", pin)


if pin == 1 then
raspi.write( 18, 0 )
else
raspi.write( 18, 1 )
end


print("After Toggle", raspi.read(18))

jeanclaudej971
April 30th, 2017, 06:19 PM
Thank you Ron :)

I will try as soon as possible.
I still learn something about lua and girder.

best regards.