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

looking for a clock(for in game time) that can activate redstone at night and turn it off in the day

Started by rick3333, 13 January 2013 - 08:06 PM
rick3333 #1
Posted 13 January 2013 - 09:06 PM
Hi
I am looking for a clock(for in game time) that can activate redstone at night and turn it off in the day.
because I can not figure out the coding for it.

edit: i figured out the code
os.time() returns the current in-game time. I believe morning is 6 and night is 18. so…

time = os.time()
if time < 6 or time > 18 then
  -- redstone output that says it's night
end
would tell you if it's night. then you just run that when you want to know if it's night…
then i got the code to repeat it self with this code.



while true do
time = os.time()
if time < 6 or time > 18 then
rs.setOutput("back",true)– redstone output that says it's night
end
sleep(0.1)
end

thanks for your help crazyguymgd
crazyguymgd #2
Posted 13 January 2013 - 09:19 PM
os.time() returns the current in-game time. I believe morning is 6 and night is 18. so…

time = os.time()
if time < 6 or time > 18 then
  -- redstone output that says it's night
end
would tell you if it's night. then you just run that when you want to know if it's night…
RunasSudo-AWOLindefinitely #3
Posted 13 January 2013 - 09:20 PM
Use os.time() to get the time and check if it's greater than 19 or something (19 = 7:00PM).

EDIT: Argh, ninja'd!
Edited on 13 January 2013 - 08:20 PM
crazyguymgd #4
Posted 13 January 2013 - 09:21 PM
Use os.time() to get the time and check if it's greater than 19 or something (19 = 7:00PM).

EDIT: Argh, ninja'd!

:)/>
Orwell #5
Posted 13 January 2013 - 09:29 PM
For this purpose I like to use "os.startTimer()" and "os.pullEvent('timer')".
crazyguymgd #6
Posted 13 January 2013 - 09:33 PM
For this purpose I like to use "os.startTimer()" and "os.pullEvent('timer')".

why do you like to do this?
RunasSudo-AWOLindefinitely #7
Posted 13 January 2013 - 09:42 PM
Presumably because it's slightly more accurate. I don't see how that would tell you time time of day though. Are you sure you're posting in the right thread?
Orwell #8
Posted 13 January 2013 - 10:00 PM
Total confusion, I meant os.setAlarm() instead of os.startTimer()..

I couldn't find the other thread where I posted this so I'll remake it quickly:

local dawnTime = 6
local sunsetTime = 18
local dawn = os.setAlarm(dawnTime)
local sunset = os.setAlarm(sunsetTime)

local function atDawn()
  -- Do stuff at dawn.
end

local function atSunset()
  -- Do stuff at sunset
end

while true do
  local e,alarm = os.pullEvent('alarm')
  if alarm == dawn then
	dawn = os.setAlarm(dawnTime)
	atDawn()
  elseif timer == sunset then
	sunset = os.setAlarm(sunsetTime)
	atSunset()
  end
end

It's slightly better than spamming os.time() in a while loop, regardless of how long you'd make your sleep there. This loop essentially does a sleep() with the exact amount of timer until dawn/sunset.
theoriginalbit #9
Posted 13 January 2013 - 10:03 PM
I couldn't find the other thread where I posted this so I'll remake it quickly:

local dawnTime = 6
local sunsetTime = 18
local dawn = os.setAlarm(dawnTime)
local sunset = os.startAlarm(sunsetTime)

local function atDawn()
  -- Do stuff at dawn.
end

local function atSunset()
  -- Do stuff at sunset
end

while true do
  local e,timer = os.pullEvent('timer')
  if timer == dawn then
	dawn = os.startAlarm(dawnTime)
	atDawn()
  elseif timer == sunset then
	sunset = os.startAlarm(sunsetTime)
	atSunset()
  end
end

It's slightly better than spamming os.timer() in a while loop, regardless of how long you'd make your sleep there. This loop essentially does a sleep() with the exact amount of timer until dawn/sunset.
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?
Orwell #10
Posted 13 January 2013 - 10:07 PM
* snip *
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?
Mind that I edited my post. I confused the timer methods with the alarm methods. :P/> Except that I used os.startTimer() instead of os.setTimer() and 'timer' instead of 'alarm' in the version you quoted, that should work. <_</> setAlarm() queues an event at specified minecraft time.

Edit: (damn, early in the morning for me, I've hit the edit button like 20 times by now)
theoriginalbit #11
Posted 13 January 2013 - 10:10 PM
* snip *
Would that not just have a timer that ran for 6 and 18 seconds from startup? what if the computer got unloaded?
Mind that I edited my post. I confused the timer methods with the alarm methods. :P/> Except that I used os.startTimer() instead of os.setTimer() and 'timer' instead of 'alarm' in the version you quoted, that should work. <_</> setAlarm() queues an event at specified minecraft time.

Edit: (damn, early in the morning for me, I've hit the edit button like 20 times by now)
Haha thats ok, I was just making sure I wasn't misunderstanding the code…