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

[Question] Redstone input

Started by SMD, 11 January 2013 - 03:55 PM
SMD #1
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
theoriginalbit #2
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
SMD #3
Posted 11 January 2013 - 05:09 PM
ok thx for the answer
but for the redstone api which function do i use?
Heracles421 #4
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
SMD #5
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?
Heracles421 #6
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
SMD #7
Posted 11 January 2013 - 05:25 PM
i typed that into the computer and it did nothing
Heracles421 #8
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
SMD #9
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
theoriginalbit #10
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
Heracles421 #11
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
theoriginalbit #12
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
SMD #13
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
Heracles421 #14
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/>