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

Run sleep on the sideline

Started by deletedaccouint, 14 October 2012 - 01:31 PM
deletedaccouint #1
Posted 14 October 2012 - 03:31 PM
Hey, is there any way to run a sleep command on the side, meaning like:

You have a program that outputs to a redstone output, sleeps for 5 seconds, then turns the output off.
Then you have code that should run when the redstone output starts, (for example when a door opens), but not be affected by the sleep delay, (because then when the sleep is over, the door would be closed. If theres any way to do this, please tell me

Thanks
JoshhT #2
Posted 14 October 2012 - 03:33 PM
You may want to look into the parallel API.
I haven't done any testing with it yet, but I believe, based upon what I've read, it should be what you're looking for.
http://computercraft...=Parallel_(API)

Edit:
Or, you could do something like.


rs.setOutput("back", true)
for i = 1, 5 do
  --Unaffected sleep code.
  sleep(1)
end
rs.setOutput("back", false)
Edited on 14 October 2012 - 01:37 PM
deletedaccouint #3
Posted 14 October 2012 - 03:35 PM
Yeah, I was looking into it, but it seemed to do the exact same thing and not really work
JoshhT #4
Posted 14 October 2012 - 03:37 PM
Check my edit, perhaps that solves your problem?
deletedaccouint #5
Posted 14 October 2012 - 03:53 PM
Nope, it appears to freeze the code on the for statement and hangs for a while, way past any sleep timer set
Doyle3694 #6
Posted 14 October 2012 - 04:34 PM
the parallel api is not really parallel, it waits for the next coruotine in the function I believe, so it will run one program for some time, then the other program for some time and so on
Lyqyd #7
Posted 14 October 2012 - 07:43 PM
You'd probably want to use a timer or two for this. You start a timer with 'myTimer = os.startTimer(5)', then do whatever else you need to do in the five seconds, then use a simple os.pullEvent loop to check for the timer event. When you get the event, the five seconds are up. Perform whatever operations you need to do then and exit the loop!
deletedaccouint #8
Posted 15 October 2012 - 11:24 PM
You'd probably want to use a timer or two for this. You start a timer with 'myTimer = os.startTimer(5)', then do whatever else you need to do in the five seconds, then use a simple os.pullEvent loop to check for the timer event. When you get the event, the five seconds are up. Perform whatever operations you need to do then and exit the loop!

Can you please pass on some code examples for this? I'm not great at coding.

Thanks
Doyle3694 #9
Posted 16 October 2012 - 08:29 AM
if you know os.pullEvent() it will pass a "timer" event to os.pullEvent() when it's done, so you can for example make a custom read that checks for os.pullEvent(), and then check what the event is, like if it's "char" then read that char and insert it into a string, if it's timer then quit the read. and oh, to start timers use:

os.startTimer(desired timeout)
deletedaccouint #10
Posted 16 October 2012 - 11:09 PM
if you know os.pullEvent() it will pass a "timer" event to os.pullEvent() when it's done, so you can for example make a custom read that checks for os.pullEvent(), and then check what the event is, like if it's "char" then read that char and insert it into a string, if it's timer then quit the read. and oh, to start timers use:

os.startTimer(desired timeout)


Um… still a bit lost
Doyle3694 #11
Posted 16 October 2012 - 11:18 PM
I don't know waht code you want to run, give me it and I can show you a sample.

The syntax would be different depending on what you want to do really
deletedaccouint #12
Posted 17 October 2012 - 02:45 PM
I don't know waht code you want to run, give me it and I can show you a sample.

The syntax would be different depending on what you want to do really

Well, The code is imbeded in different things, but the features are basically


Menu (using Window API) with some options
Door 1
Door 2

Door 2 is behind door 1. You open door 1, and after 6 seconds it closes. However, the door is on it's own timer, instead of mine (in the real area, the door is actually a piston staircase, so this is easier), and then door 2 is a standard door, that is opened and closed by redstone from the computer.

Additionally, theres a monitor that projects, from rednet connections, the stats of the doors.

The sleeping issue is that I have the staircase activation code (or door 1) which is a 1 second pulse, and then it projects the data to rednet, delays 6 seconds, then projects the updated data to rednet.

This causes the issue of when I want to activate the staircase, then activate the door (2) very close together. However, the issue is that when I open the staircase, I have to wait about 7s (door open pulse + 6 rednet send wait) and THEN I can open the door, meaning by the time the staircase is closed.
Doyle3694 #13
Posted 17 October 2012 - 03:29 PM
SO you want to be able to enter a password during that sleep?
deletedaccouint #14
Posted 17 October 2012 - 03:33 PM
SO you want to be able to enter a password during that sleep?

The goal is to trigger the door2 opening before the staircase closes.
deletedaccouint #15
Posted 17 October 2012 - 03:42 PM
Heres the actual code:

--draw window...
rednet.open("top")
shell.run('mem/redworks/apis/redworks')
redworks.showWindow(12,6,25,8,"Security","1. Alarmn2. Staircasen3. Main Doorsn4. Lab Doorsn5. Quit")
term.setCursorPos(1,16)
write"Choose a selection: "

input = read() --After entry, you cannot open a different door until I run doGui() (which is this whole function) again.

elseif input == "2" then --If they choose option 2 (Staircase)
  rs.setBundledOutput("left", colors.red)
  sleep(1) --one second pulse
  rs.setBundledOutput("left", rs.getBundledOutput("left") - colors.red)
  rednet.send(4, "stair on") --Send the Stair On thing
  sleep(6) --Wait 6 seconds for the staircase to shut
  rednet.send(4, "stair off") --Tell monitor that the stairs are closed
  term.setCursorPos(1,16)
  term.clearLine()
  write("Staircase opened.") --Tell the user that the door is opened 
  term.clear()
  doGui()
elseif input == "3" then --Option 3 (Door)
  rs.setBundledOutput("left", colors.orange)
  rednet.send(0, "main on") --Message on
  sleep(5) --Wait 5 seconds (This is also why you cant do maindoor and then staircase)
  rs.setBundledOutput("left", rs.getBundledOutput("left") - colors.orange) --Off
  rednet.send(0, "main off") --doors off,
  term.setCursorPos(1,16)
  term.clearLine()
  write("Main doors opened.") --Print to user
  sleep(2)
  term.clear() 
  doGui() --Reset back to main selection window
else
  term.setCursorPos(1,16)
  term.clearLine()
  write("Invalid Option")
  sleep(0.5)
  term.setCursorPos(1,17)
  write("Logging out")


ShowWindow is from the Redworks API
Doyle3694 #16
Posted 17 October 2012 - 03:49 PM
You should wait for another more proish person then me since I don't think I know how to do this.