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

and mylast question...

Started by djhackerr, 16 May 2012 - 05:10 PM
djhackerr #1
Posted 16 May 2012 - 07:10 PM
is there any way for PC to check every 30sec for redstone signal al right?
and if it is false then turns off pc with message
if it is on then it is continuing to work

is this posible? :P/>/>
MysticT #2
Posted 16 May 2012 - 07:38 PM
Yes, it's possible. You need to use a timer:

local nTime = 30 -- time to check for input
local sSide = "right" -- the side to check

local timer = os.startTimer(nTime)
while true do
  local evt, arg = os.pullEvent("timer")
  if arg == timer then
	if rs.getInput(sSide) then
	  print("Goodbye")
	  sleep(1)
	  os.shutdown()
	end
	timer = os.startTimer(nTime) -- restart the timer
  end
end

If you want to use the computer at the same time, you can use the parallel api:

local function checkInput()
  while true do
	local evt, arg = os.pullEvent("timer")
	if arg == timer then
	  if rs.getInput(sSide) then
   	 print("Goodbye")
   	 sleep(1)
   	 break
	  end
	  timer = os.startTimer(nTime) -- restart the timer
	end
  end
end

local function runShell()
  shell.run("shell")
end

parallel.waitForAny(checkInput, runShell)
os.shutdown()
That way the computer will shutdown when you exit the shell or there's a redstone input on the side you choose.
djhackerr #3
Posted 17 May 2012 - 11:48 AM
will try, thanks :P/>/>
djhackerr #4
Posted 17 May 2012 - 12:05 PM
so i typed this:


local function checkInput()
  while true do
		local evt, arg = os.pullEvent("timer")
		if arg == timer then
		  if rs.getInput("right") == false then
		 print("Goodbye")
		 sleep(1)
		 break
		  end
		  timer = os.startTimer(nTime) -- restart the timer
		end
  end
end
local function runShell()
  shell.run("shell")
end
parallel.waitForAny(checkInput, runShell)
os.shutdown()

so what have i done here wrong?
still wont turn off when i disable redstone

–edit
i also used "shell.run("old") to run it, is there any problem with this?"
MysticT #5
Posted 17 May 2012 - 02:09 PM
My bad, never started the timer for the first time. Change the checkInput function to:

local nTime = 30 -- time to check for redstone input
local sSide = "back" -- the side to check for redstone input

local function checkInput()
  local timer = os.startTimer(nTime)
  while true do
    local evt, arg = os.pullEvent("timer")
    if arg == timer then
	  if not rs.getInput(sSide) then
	    print("Goodbye")
	    sleep(1)
	    break
	  end
	  timer = os.startTimer(nTime)
    end
  end
end
djhackerr #6
Posted 17 May 2012 - 07:23 PM
will try thx :P/>/>
djhackerr #7
Posted 17 May 2012 - 07:31 PM

local nTime = 1 -- time to check for redstone input
local sSide = "right" -- the side to check for redstone input
local function checkInput()
  local timer = os.startTimer(nTime)
  while true do
    local evt, arg = os.pullEvent("timer")
    if arg == timer then
		  if rs.getInput(sSide) == false then
		    print("Goodbye")
		    sleep(3)
		    break
		  end
		  timer = os.startTimer(nTime)
    end
  end
end


i have redone some coding part but now
it auto shutdowns without message if redstone torch is here but
when i set to redpower cyan cable then after I turn it on and disable redstone computer is still working :P/>/>
and this program needs to work in background because i will install some games into it :D/>/>
MysticT #8
Posted 17 May 2012 - 07:38 PM
You still need the rest of the previous code (the runShell function and the parallel call):

local nTime = 1
local sSide = "right"

local function checkInput()
  local timer = os.startTimer(nTime)
  while true do
	local evt, arg = os.pullEvent("timer")
	if arg == timer then
	  if not rs.getInput(sSide) then
		print("Goodbye")
		sleep(1)
		break
	  end
	  timer = os.startTimer(nTime)
	end
  end
end

local function runShell()
  shell.run("/rom/programs/shell") -- added the full path (just in case)
end

term.clear()
term.setCursorPos(1, 1) -- clear the screen, so it doesn't show "CraftOS 1.3" twice
parallel.waitForAny(runShell, checkInput)
os.shutdown()
Just save this as startup and it should run when you turn on the computer (unless there's a disk with a startup program).

Edit:
Just realized that this can be done with a redstone event, so it doesn't check all the time:

local sSide = "right"

local function checkInput()
  while true do
	local evt = os.pullEvent("redstone")
	if not rs.getInput(sSide) then
	  print("Goodbye")
	  sleep(1)
	  break
	end
  end
end

local function runShell()
  shell.run("/rom/programs/shell") -- added the full path (just in case)
end

if not rs.getInput(sSide) then -- first check, so it doesn't turn on if there's no redstone input.
  os.shutdown()
end

term.clear()
term.setCursorPos(1, 1) -- clear the screen, so it doesn't show "CraftOS 1.3" twice
parallel.waitForAny(runShell, checkInput)
os.shutdown()
And it's shorter this way :P/>/>
Edited on 17 May 2012 - 05:54 PM
djhackerr #9
Posted 17 May 2012 - 08:30 PM
so when i insert that program in
whatever i program turn on later it will still check for rs output?
MysticT #10
Posted 17 May 2012 - 09:57 PM
Yes, that's what the parallel API is for. It lets you run two things (almost) at the same time. I already tested the code and it works, have fun with it :P/>/>