ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Juliet119 on Jun 03, 2023, 06:26 AM

Title: Make a bell ring at a specific time
Post by: Juliet119 on Jun 03, 2023, 06:26 AM
I'm making a clock tower with a bell and a computer hidden inside. I want the bell to ring 6 times every morning at 6. I know this has something to do with os.time() so I tried this:
while true do
    if os.time() == 6 then
          ringBell(6)
    end
end
I tested the ringBell() function separately and it works fine, and the program doesn't throw any errors when run. I'm new to Lua and coding in general. Can someone tell me what I'm missing here?
Title: Make a bell ring at a specific time
Post by: SamH on Aug 09, 2023, 11:40 AM
I'm assuming you are getting the "Too long without yielding" error. Computers in CC need to let other computers in the world execute their code (even if there are no other computers) so they need to "yield".

To "yield" you can simply do this ...
while true do
    if os.time() == 6 then
          ringBell(6)
    end
    os.sleep(0.01) -- This will end up calling os.pullEventRaw for a timer. os.pullEventRaw will end up calling coroutine.yield on the "master" coroutine.
end