This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wipiid's profile picture

Run a program when a wood button is pressed

Started by wipiid, 21 January 2013 - 01:27 PM
wipiid #1
Posted 21 January 2013 - 02:27 PM
Hello!
this is my very first venture into computer craft!!! and sofar its fun. i am far from a programmer, but have written this so far

edit Bridge



rs.setOutput("bottom", true)
sleep(12)
rs.setoutput("bottom", false)


then when i type bridge it will send a RS signal and close my trapdoor drawbridge for 12 seconds and let us run across then open again 12 seconds later.

what i would like to have is instead of having to open the computer and typing bridge i would like to have a wooden button on a block above it, then when we push it it runs "bridge"

i tried

if rs.getInput("top", true)
then

rs.setOutput("bottom", true)
sleep(12)
rs.setoutput("bottom", false)
end





the last one i tried fails as expected. but i am not sure what to do at this point.
any advice would be greatly appreciated.

Thanks
Wipiid
SuicidalSTDz #2
Posted 21 January 2013 - 02:31 PM
Hello!
this is my very first venture into computer craft!!! and sofar its fun. i am far from a programmer, but have written this so far

edit Bridge



rs.setOutput("bottom", true)
sleep(12)
rs.setoutput("bottom", false)


then when i type bridge it will send a RS signal and close my trapdoor drawbridge for 12 seconds and let us run across then open again 12 seconds later.

what i would like to have is instead of having to open the computer and typing bridge i would like to have a wooden button on a block above it, then when we push it it runs "bridge"

i tried

if rs.getInput("top", true)
then

rs.setOutput("bottom", true)
sleep(12)
rs.setoutput("bottom", false)
end





the last one i tried fails as expected. but i am not sure what to do at this point.
any advice would be greatly appreciated.

Thanks
Wipiid
function detect()
side = –Side the button is on in the form of a string " "
while true do
if rs.getInput(side) == true then
shell.run(program name) –The name being in the form of a string " "
else
detect()
end
detect()

This should work but I am not certain

So in conclusion:
function detect()
side = "right"
while true do
if rs.getInput(side) == true then
shell.run("bridge")
else
detect()
end
detect()
ChunLing #3
Posted 21 January 2013 - 02:56 PM
You want an os.pullEvent("redstone") so that the loop only checks the input when there is a change in the redstone input, otherwise…well.

Some illustration:
local side = "top" -- or whatever
while true do
    os.pullEvent("redstone")
    if rs.getInput(side) then
        rs.setOutput("bottom", true)
        sleep(12)
        rs.setoutput("bottom", false)
    end
end
Edited on 21 January 2013 - 01:58 PM
SuicidalSTDz #4
Posted 21 January 2013 - 02:58 PM
You want an os.pullEvent("redstone") so that the loop only checks the input when there is a change in the redstone input, otherwise…well.

Forgot about that… Not the best when it comes to redstone detection so :P/>
ChunLing #5
Posted 21 January 2013 - 03:25 PM
It's always a good idea to write a program to yield as often and as long as will permit it to carry out the essential functions you require. Otherwise you can get a lot of low performance and eventually unexpected and buggy behavior from your programs.