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

[lua] blocking programs until....

Started by Nolorwolf, 22 January 2013 - 05:33 AM
Nolorwolf #1
Posted 22 January 2013 - 06:33 AM
If i for instance use


while true do
  os.pullEvent("redstone")
  if rs.getInput("left") then -- check the LEFT input
	-- do something
  end
  if rs.getInput("right") then -- check the RIGHT input
	-- do something
  end
end

in startup, how do I prevent one from running while another one is on?

Basically, I have 2 elevators using same blocks of code, for opposite directions (when one goes up, another one goes down),
and 4 buttons with 2 rs signals. I want, when one button is clicked (one side is activated) that there is no chance that program will receive other input
for the duration of program, and possibly few seconds more, as opposite signals can mess up frames badly.

2 blocks: (breakers, deployers, motors)


term.clear()
term.setCursorPos(1,1)
--[[
for i=1,18 do
rs.setBundledOutput("back", 0)
sleep(0.1)
rs.setBundledOutput("back", 16)
sleep(0.1)
rs.setBundledOutput("back", 0)
rs.setBundledOutput("back", 2048)
sleep (0.5)
rs.setBundledOutput("back", 0)
sleep(0.3)
i=i+1
end
--]]

--[[
for i=1,18 do
rs.setBundledOutput("back", 0)
sleep(0.1)
rs.setBundledOutput("back", 32768)
sleep(0.1)
rs.setBundledOutput("back", 0)  ---- idk if this is needed also, second 0 should affect both
rs.setBundledOutput("back", 8192)
sleep (0.5)
rs.setBundledOutput("back", 0)
sleep(0.3)
i=i+1 ------ idk if this is needed
end
--]]

Oh, and if possible, tell me how to change headline after posting, so i can put [closed] when needed, tY :)/>
DrLancer #2
Posted 22 January 2013 - 06:47 AM
i would use a variable at the beginning, checking whether it's allowed to activate or not.



canActivate = true
while true do
os.pullEvent("redstone")
  if canActivate == true then
    if rs.getInput("left") then -- check the LEFT input
      -- do something
      canActivate = false -- Change to true when done
    end
    if rs.getInput("right") then -- check the RIGHT input
      -- do something
      canActivate = false -- Change to true when done
    end
  end
end
Willibilly19 #3
Posted 22 January 2013 - 06:52 AM
Not sure if this will work, but time how long it takes for your elevator to run, then add sleep into your code. That seems as if it would work.


while true do
  os.pullEvent("redstone")
  if rs.getInput("left") then -- check the LEFT input
	    -- do something
        sleep(5) --adjust to how long your elevator takes...+ a few seconds if you want
  end
  if rs.getInput("right") then -- check the RIGHT input
	    -- do something
        sleep(5) -- same thing here
  end
end
Nolorwolf #4
Posted 22 January 2013 - 07:19 AM
Well, I tried 1st comment, not working for some reason:


term.clear()
term.setCursorPos(1,1)


print ("Running")
available = true

while true do
  os.pullEvent("redstone")
    if available == true then
	  if rs.getInput("left") then


		  for i=1,18 do
		   rs.setBundledOutput("back", 0)
		   sleep(0.1)
		   rs.setBundledOutput("back", 16)
		   sleep(0.1)
		   rs.setBundledOutput("back", 0)
		   rs.setBundledOutput("back", 2048)
		   sleep (0.5)
		   rs.setBundledOutput("back", 0)
		   sleep(0.3)
		   i=i+1
		  end

		  available = false

	  end
	  if rs.getInput("right") then


		  for i=1,18 do
		   rs.setBundledOutput("back", 0)
		   sleep(0.1)
		   rs.setBundledOutput("back", 32768)
		   sleep(0.1)
		   rs.setBundledOutput("back", 0)
		   rs.setBundledOutput("back", 8192)
		   sleep (0.5)
		   rs.setBundledOutput("back", 0)
		   sleep(0.3)
		   i=i+1
		  end
		 
		  available = false
	  end
    end
end

Adding a pause comes easy once this is working :/
remiX #5
Posted 22 January 2013 - 07:22 AM
Have a variable which is the state of the elevator, i.e. is it at the bottom or top?


--[[ Let's say the elevator is at the top now
     Although, the other computer will need to have false set if this one is
     true because the other one will be at the bottom.
     So what you could do is allow the program to have
     arguments and if the file for the state of the elevator
     (yes, for this type of program, you will definantely need
     to save the state) does not exist, it will use the default
     of the argument
]]--
tArgs = {...}
if not fs.exists("stateFile") then
    print("State file does not exist! Attempting to use arguments for default value...")
    if not tArgs[1] then
        print("No argument given.")
        return
    else
        print("Default state set to: " .. tArgs[1])
        AtTop = tArgs[1] == "true" -- because arguments are strings, this will check if it's equal to true and if it is it will set it to true, else false.
    end
else
    -- state file exists!
    fRead = fs.open("stateFile", "r")
    AtTop = fRead.readLine() == "true"
    fRead.close()
end


while true do
  os.pullEvent("redstone")
  if rs.getInput("left") and AtTop then -- check the LEFT input
        -- do something
        AtTop = false
  end
  if rs.getInput("right") and not AtTop then -- check the RIGHT input
        -- do something
        AtTop = true
  end
end


Something like that…
Nolorwolf #6
Posted 22 January 2013 - 07:29 AM
Ah, but the way I made my elevator is supposed to avoid that

–2 |0|—| | 1
—–| |—| |
—–| |—| |
—–| |—| |
–1 | |—-|0| 2

1 - [comp] - 2

so when you press 2 you know that one block should be activated, cause then you know where elevator is (as button is inside elevator) xD Sry if design is a bit complicated….
remiX #7
Posted 22 January 2013 - 07:35 AM
The picture: 0's are the elevators, 1 and 2's are the computers?

I don't understand, what is your elevator supposed to avoid?

Any chance you could give me the map with your elevator so I could test it and understand more?
Nolorwolf #8
Posted 22 January 2013 - 07:50 AM
0 are elevators, 1 and 2 are buttons that send wireless signals to one computer.

I would, but map is just too large…. :/

It is supposed to avoid pressing 2 buttons…

example:

I want to go up in that picture, i press a button in elevator that is now down (frequency 2).
Someone presses one other button (no use rly, as elevators are not there, but malicious intent…)
1) If that someone presses button with frequency 1, motors will go crazy, any maybe start eating frames.
2) If that someone presses button with frequency 2, duration of program could extend, which is not a probles as it lasts 10 secs.

So I wan't to avoid situation 1).

Edit: Other numbers (colors) in code are Block Breakers, Deployers and Frame Motors, as elevators use piston-frame movement (tm xD)
when elevator goes up, deployer adds frames, and reverse BB removes frame in the go.
remiX #9
Posted 22 January 2013 - 08:26 AM
Oh, you're only using one computer for all of it.

Well, each lever has a different colour, right? So I'm sure the way I suggested could work with variables… but have a variable for each lever (colour)
Make it so you can set the default value in arguments or whatever and save the state to a file and read it upon startup.

I can''t think of any other way :/

Map: Could you not re-create it on a new fresh map?
Nolorwolf #10
Posted 22 January 2013 - 08:28 AM
I can try, give me 10 mins, took me a day on survival and making it fit, but i can probably make it much faster in creative.