Jump to content




[Question] Redstone input


  • You cannot reply to this topic
13 replies to this topic

#1 SMD

  • New Members
  • 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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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...e=Category:APIs

Edited by TheOriginalBIT, 11 January 2013 - 05:03 PM.


#3 SMD

  • New Members
  • 6 posts

Posted 11 January 2013 - 05:09 PM

ok thx for the answer
but for the redstone api which function do i use?

#4 Heracles421

  • Members
  • 258 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

#5 SMD

  • New Members
  • 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?

#6 Heracles421

  • Members
  • 258 posts

Posted 11 January 2013 - 05:24 PM

View PostSMD, on 11 January 2013 - 05:20 PM, said:

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

#7 SMD

  • New Members
  • 6 posts

Posted 11 January 2013 - 05:25 PM

i typed that into the computer and it did nothing

#8 Heracles421

  • Members
  • 258 posts

Posted 11 January 2013 - 05:26 PM

View PostSMD, on 11 January 2013 - 05:25 PM, said:

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


#9 SMD

  • New Members
  • 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

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 11 January 2013 - 05:37 PM.


#11 Heracles421

  • Members
  • 258 posts

Posted 11 January 2013 - 05:42 PM

View PostTheOriginalBIT, on 11 January 2013 - 05:35 PM, said:

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

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 January 2013 - 05:47 PM

View PostHeracles421, on 11 January 2013 - 05:42 PM, said:

View PostTheOriginalBIT, on 11 January 2013 - 05:35 PM, said:

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 by TheOriginalBIT, 11 January 2013 - 05:47 PM.


#13 SMD

  • New Members
  • 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

#14 Heracles421

  • Members
  • 258 posts

Posted 11 January 2013 - 05:50 PM

View PostTheOriginalBIT, on 11 January 2013 - 05:47 PM, said:

View PostHeracles421, on 11 January 2013 - 05:42 PM, said:

View PostTheOriginalBIT, on 11 January 2013 - 05:35 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users