View difference between Paste ID: rtGfqdhE and UfRnuCpx
SHOW: | | - or go back to the newest paste.
1
local rs = peripheral.find("redstoneIntegrator")
2
if not rs then error("No redstone integrator") end
3
local dandelifeon = "south"
4
local destructor = "north"
5
local constructor = "up"
6
local placer = "east"
7-
local cancelKey = keys.c
7+
8
local cancellationPending = false
9
10
local function pulse(direction, duration)
11
    if not duration then duration = 1 end
12
    rs.setOutput(direction, true)
13
    sleep(duration)
14
    rs.setOutput(direction, false)
15
end
16
17
local function initOutput(direction)
18
    rs.setOutput(direction, false)
19
end
20
21
initOutput(dandelifeon)
22
initOutput(destructor)
23
initOutput(constructor)
24
initOutput(placer)
25
26
local function main()
27
    print("Cellular Block Farm")
28-
            sleep(1)
28+
    while not cancellationPending do
29
        if rs.getInput(lever) then
30
            print("Running Dandelifeon...")
31
            pulse(placer)
32
            pulse(dandelifeon, 50 + 2)
33
            print("Dandelifon cycle finished")
34
        else
35
            pulse(destructor)
36
            pulse(constructor)
37
            pulse(dandelifeon, 2)
38-
    print("Program is running now, press", keys.getName(cancelKey), "to cancel")
38+
39
    end
40-
        local event, key = os.pullEvent("key")
40+
    print("Okay, finished cleanup, goodbye...")
41-
        if key == cancelKey then
41+
42-
            print("Okay, goodbye...")
42+
43
local function canceller()
44
	local oldPullEvent = os.pullEvent
45
	os.pullEvent = os.pullEventRaw
46
    print("Program is running now, hold Ctrl+T (terminate) to shutdown.")
47
    while true do
48-
parallel.waitForAny(main, canceller)
48+
        local event = os.pullEventRaw("terminate")
49
        if event == "terminate" then
50
			os.pullEvent = oldPullEvent
51
            print("Cancellation was requested, finishing up safely...")
52
			cancellationPending = true
53
            return
54
        end
55
    end
56
end
57
58
parallel.waitForAll(main, canceller)