6 posts
Posted 11 January 2013 - 04:55 PM
hello
first timer at computer-craft
so it might be a stupid question
is it possible for to have a redstone signal as an input and start a program?
what i want to do is send a redstone signal to the computer and the computer picks up the signal from front side or the back side and it starts a program that opens or closes the door
7508 posts
Location
Australia
Posted 11 January 2013 - 05:02 PM
yes it is possible to do this :)/>
take a look at the os API, mainly at the os.pullEvent function, redstone API and the shell api, mainly shell.run
EDIT: apis page
http://computercraft.info/wiki/index.php?title=Category:APIs
Edited on 11 January 2013 - 04:03 PM
6 posts
Posted 11 January 2013 - 05:09 PM
ok thx for the answer
but for the redstone api which function do i use?
248 posts
Posted 11 January 2013 - 05:13 PM
if redstone.getInput("front") then
do stuff
end
Just do the same for the back, but changing front for back
6 posts
Posted 11 January 2013 - 05:20 PM
if redstone.getInput("front") then
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("right", true)
end
if redstone.getInput("back") then
redstone.setOutput("left", false)
sleep(2)
redstone.setOutput("right", false)
end
like this?
248 posts
Posted 11 January 2013 - 05:24 PM
if redstone.getInput("front") then
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("right", true)
end
if redstone.getInput("back") then
redstone.setOutput("left", false)
sleep(2)
redstone.setOutput("right", false)
end
like this?
Yup
6 posts
Posted 11 January 2013 - 05:25 PM
i typed that into the computer and it did nothing
248 posts
Posted 11 January 2013 - 05:26 PM
i typed that into the computer and it did nothing
Add a while true do loop, forgot to tell you that
while true do
Stuff here
end
6 posts
Posted 11 January 2013 - 05:35 PM
thx for the help
i have another question how do i set it so it wait for the signals and doesn't terminate the program?
the too long without yielding
7508 posts
Location
Australia
Posted 11 January 2013 - 05:35 PM
the reason it does that is because the program needs to yield so other programs can run…
i suggest adding this just after the while true do
os.pullEvent("redstone")
alternatively you could do this
sleep(0)
however using this, means that it will constantly be running and checking and wasting resources on your non-cc computer… using os.pullEvent("redstone") means that it wont run UNTIL it gets a redstone signal
Edited on 11 January 2013 - 04:37 PM
248 posts
Posted 11 January 2013 - 05:42 PM
the reason it does that is because the program needs to yield so other programs can run…
i suggest adding this just after the while true do
os.pullEvent("redstone")
alternatively you could do this
sleep(0)
however using this, means that it will constantly be running and checking and wasting resources on your non-cc computer… using os.pullEvent("redstone") means that it wont run UNTIL it gets a redstone signal
Yup, in your case I'd suggest using os.pullEvent
while true do
e = os.pullEvent()
if e = "redstone" then
do stuff
end
end
end
7508 posts
Location
Australia
Posted 11 January 2013 - 05:47 PM
the reason it does that is because the program needs to yield so other programs can run…
i suggest adding this just after the while true do
os.pullEvent("redstone")
alternatively you could do this
sleep(0)
however using this, means that it will constantly be running and checking and wasting resources on your non-cc computer… using os.pullEvent("redstone") means that it wont run UNTIL it gets a redstone signal
Yup, in your case I'd suggest using os.pullEvent
while true do
e = os.pullEvent()
if e = "redstone" then
do stuff
end
end
end
even that is over the top… that pulls all the events and then checks if its the one we want, meaning it runs more than it needs to… using os.pullEvent( "redstone" ) will ignore all events UNTIL a redstone event occurs :)/>
Edited on 11 January 2013 - 04:47 PM
6 posts
Posted 11 January 2013 - 05:50 PM
thx for all the help
while true do
os.pullEvent("redstone")
if redstone.getInput("front") then
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("right", true)
end
if redstone.getInput("back") then
redstone.setOutput("right", false)
sleep(2)
redstone.setOutput("left", false)
end
end
this is this code i made with your all help
please point out mistakes or places that i can improve if you can
248 posts
Posted 11 January 2013 - 05:50 PM
the reason it does that is because the program needs to yield so other programs can run…
i suggest adding this just after the while true do
os.pullEvent("redstone")
alternatively you could do this
sleep(0)
however using this, means that it will constantly be running and checking and wasting resources on your non-cc computer… using os.pullEvent("redstone") means that it wont run UNTIL it gets a redstone signal
Yup, in your case I'd suggest using os.pullEvent
while true do
e = os.pullEvent()
if e = "redstone" then
do stuff
end
end
end
even that is over the top… that pulls all the events and then checks if its the one we want, meaning it runs more than it needs to… using os.pullEvent( "redstone" ) will ignore all events UNTIL a redstone event occurs :)/>/>
>.< Wait a lil until you point out my fails :P/>