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

computer night sensor

Started by Theroboticrobot, 04 October 2012 - 07:57 PM
Theroboticrobot #1
Posted 04 October 2012 - 09:57 PM
i need a program that will automatically sense that it is night, and turn on lights by emmiting a redstone signal when it is night, then when it is day again, turn them back off. this is what i have so far


if time >= 12000 then
redstone.setOutput ("back", true)
elseif time <= 0 then
redstone.setOutput ("back", false)
end
Luanub #2
Posted 04 October 2012 - 10:11 PM
If you have RP2 there is a light sensor that you can just hook up to your lights so you dont have to use a computer.

Otherwise try this:

local onTime = 18.00
local offTime = 6.00

while true do
local t = os.time()
if t >= offTime and t < onTime then
os.setAlarm( onTime )
os.pullEvent("alarm")
rs.setOutput ("back", true)
else
os.setAlarm( offTime )
os.pullEvent("alarm")
rs.setOutput ("back", false)
end
end

You need to capture the time using os.time() and then set alarms to trigger the redstone actions. Also the raw time format from os.time() is hrs.mins(minecraft hrs and mins).
Cranium #3
Posted 04 October 2012 - 10:27 PM
You can use os.time() to check the minecraft time. It is 0-23.999 in digits.

while true do
local time = os.time()
  if time <=6 and time >=18 then
    rs.setOutput("side",true) --if it's night, send out a signal
  else
    rs.setOutput("side",false)
  end
sleep(1)
end
Not tested, but that should work, if I recall.
Orwell #4
Posted 04 October 2012 - 10:41 PM
It's way more efficient to use pullEvent():

while true do
  local onAlarm = onAlarm or os.setAlarm(18)
  local offAlarm = offAlarm or os.setAlarm(6)
  local e,alarm = os.pullEvent('alarm')
  if (alarm == onAlarm) then
	rs.setOutput('back',true)
	  onAlarm = nil
  else
	rs.setOutput('back',false)
	offAlarm = nil
  end
end