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

Redstone Pulse Alarm at 6:30 PM and 7:00 AM

Started by HoboSlasher, 01 May 2013 - 02:59 PM
HoboSlasher #1
Posted 01 May 2013 - 04:59 PM
Title: [Question] Redstone Pulse Alarm at 6:30 PM and 7:00 AM

I understand how to make a basic clock, but I am unable to get any form of alarm working. Help a guy out? :)/>
Lyqyd #2
Posted 02 May 2013 - 12:45 PM
You'll want to use the value os.time returns and check it against (I believe) 7.0 and 18.5 (for 0700 and 1830 respectively). I haven't worked with world time in CC extensively, so someone else may have a better grasp of the actual values you need to check against. Bear in mind that you are highly unlikely to hit those values exactly.
GravityScore #3
Posted 02 May 2013 - 01:11 PM
Instead of checking the exact value os.time, I would put os.time though textutils.formatTime, and check the output of that. For example, to pulse redstone on a specified side 3 times at 6:30 AM and 7AM do:


local side = "left"
while true do
  local time = textutils.formatTime(os.time())
  if time == "6:30 PM" or time == "7:00 AM" then
	for i = 1, 3 do
	  redstone.setOutput(side, true)
	  sleep(0.5)
	  redstone.setOutput(side, false)
	  sleep(0.5)
	end
  end
  sleep(0.75)
end
Pharap #4
Posted 02 May 2013 - 01:36 PM
This code isn't tested, you will have to fiddle with it, but if you run this on a dedicated computer or using parallel.waitAny() then this is probably the most logical way to handle and alarm. People sometimes forget there is a built in alarm event.


while true do
local alarm = os.setAlarm(time) -- I'm not sure about the time formatting, you'll have to check the wiki, but I believe it's the same as what you'd type into the minecraft command prompt
while true do
local e, p = os.pullEvent("alarm")
if(p == alarm)then
--react to alarm here
break -- break out of inner loop
end
end
while true do
alarm = os.setAlarm(time) -- and for pm
local e, p = os.pullEvent("alarm")
if(p == alarm)then
--react to alarm here
break -- break out of inner loop
end
end
end
HoboSlasher #5
Posted 02 May 2013 - 05:17 PM
Thank you guys so much for replying, I'll test them out when I can get onto my computer soon. :)/> thanks again,

Slasher
GravityScore #6
Posted 03 May 2013 - 12:53 AM
This code isn't tested, you will have to fiddle with it, but if you run this on a dedicated computer or using parallel.waitAny() then this is probably the most logical way to handle and alarm. People sometimes forget there is a built in alarm event.


while true do
local alarm = os.setAlarm(time) -- I'm not sure about the time formatting, you'll have to check the wiki, but I believe it's the same as what you'd type into the minecraft command prompt
while true do
local e, p = os.pullEvent("alarm")
if(p == alarm)then
--react to alarm here
break -- break out of inner loop
end
end
while true do
alarm = os.setAlarm(time) -- and for pm
local e, p = os.pullEvent("alarm")
if(p == alarm)then
--react to alarm here
break -- break out of inner loop
end
end
end

Completely forgot there's an alarm event! :P/> That's probably a way better method of doing it than mine :P/>

The time formatting is like what's returned by os.time. For example:

-- 6:30 PM
os.setAlarm(18.5)

-- 7 PM
os.setAlarm(19.0)

-- 6 AM
os.setAlarm(6.0)
Pharap #7
Posted 03 May 2013 - 03:11 AM
Completely forgot there's an alarm event! :P/> That's probably a way better method of doing it than mine :P/>

The time formatting is like what's returned by os.time. For example:

-- 6:30 PM
os.setAlarm(18.5)

-- 7 PM
os.setAlarm(19.0)

-- 6 AM
os.setAlarm(6.0)

Thanks for clearing up the formatting thing. Time is not something I work with often, but events are one thing I know in depth. I had a feeling you two had forgotten about it, It's arguably the most underused event available. (Tbh I'd use it more if it were able to handle real world time.)
theoriginalbit #8
Posted 03 May 2013 - 03:18 AM
Completely forgot there's an alarm event! :P/> That's probably a way better method of doing it than mine :P/>
Haha wow! me too, I always see it and think, I must get around to using this at some point… never do, always do the long way. seems that I have relearnt/remembered something today too.