Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sides = require("sides")
- local event = require("event")
- local allowAnySide = true
- local startSide = sides.north
- local stopSide = sides.south
- local startTime = nil
- function timeFormat(seconds)
- seconds = tonumber(seconds)
- if seconds <= 0 then
- return "00:00:00";
- else
- hours = string.format("%02.f", math.floor(seconds/3600));
- mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
- secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
- return hours..":"..mins..":"..secs
- end
- end
- local function onStart()
- if startTime == nil then
- startTime = computer.uptime()
- print("Timer is running...")
- end
- end
- local function onStop()
- local time = computer.uptime()
- if startTime ~= nil then
- local diff = time - startTime
- print("Stop! Duration: " .. timeFormat(diff))
- startTime = nil
- end
- end
- local function onRedstone(e, address, side, oldValue, newValue)
- if oldValue == 0 then
- local signal = false
- if (allowAnySide == true and startTime == nil) or side == startSide then
- onStart()
- signal = true
- else
- if (allowAnySide == true and startTime ~= nil) or side == stopSide then
- onStop()
- signal = true
- end
- end
- if signal == false then
- if sides[side] ~= nil then
- side = sides[side]
- end
- print("Received redstone signal on side " .. side .. " matching no inputs.")
- end
- end
- end
- local run = true
- local function onInterrupt()
- run = false
- end
- event.listen("redstone_changed", onRedstone)
- event.listen("interrupted", onInterrupt)
- print("Program started, waiting for input...")
- while run do
- os.sleep(1)
- end
- event.ignore("redstone_changed", onRedstone)
- event.ignore("interrupted", onInterrupt)
- print("Program stopped")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement