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

Multiple Alarm Events.

Started by CCJJSax, 25 September 2013 - 07:46 PM
CCJJSax #1
Posted 25 September 2013 - 09:46 PM
Here is my code that I'm working with. The functions that it calls aren't important, and they aren't what's messing up. When the time hits one of these alarms, it says "attempt to call number". I'm pretty sure that the number is the local variables I define early in the following code.

Also, is it possible to make the alarm at say 6.15 or so? do you just put os.setAlarm(6.15) ?


local morning = os.setAlarm(5)
local noon = os.setAlarm(12)
local preNight = os.setAlarm(18)
local night = os.setAlarm(21)
while true do
  local evt, arg = os.pullEvent()
  if arg == morning then
    morning()
  elseif arg == noon then
    noon()
  elseif arg == preNight then
    preNight()
  elseif arg == night then
    night()
  end
end

I based this code off of one from the CC wiki, but that one only dealt with one alarm. I changed the os.pullEvent("alarm") to just os.pullEvent(). I'm pretty sure it has something to do with that.

[Fixed it. Just use actual spacing. No other formatting works in code blocks anyway. -L]
Lyqyd #2
Posted 25 September 2013 - 11:34 PM
You are trying to call a number. You set morning to the ID of the alarm, then attempt to call morning() when that event comes in. You should have actual code to put there instead of calling that number like it's a function.
CCJJSax #3
Posted 25 September 2013 - 11:40 PM
You are trying to call a number. You set morning to the ID of the alarm, then attempt to call morning() when that event comes in. You should have actual code to put there instead of calling that number like it's a function.

I have a function called morning() that I just didn't put in the code. I know that works. I just want it to launch that function once it hits that timer.

I will post the entire code below, but I'm still pretty sure the important part is what I had already posted.

Spoiler



local time = os.time()
time = textutils.formatTime(time, false)
Chime = peripheral.wrap("left")
Chat = peripheral.wrap("right")

local function morning()
rs.setOutput("back", false)
sleep(1.5)
Chat.say("Good Morning.  The main gate is now open.")
Chime.playNote(0, 20)
sleep(0.4)
Chime.playNote(0, 17)
sleep(0.4)
Chime.playNote(0, 15)
sleep(0.4)
Chime.playNote(0, 13)
sleep(0.4)
Chime.playNote(0, 15)
sleep(0.2)
Chime.playNote(0, 17)
sleep(0.2)
Chime.playNote(0, 15)
sleep(0.2)
Chime.playNote(0, 17)
sleep(0.2)
Chime.playNote(0, 20)
sleep(0.4)
Chime.playNote(0, 17)
sleep(0.4)
Chime.playNote(0, 15)
sleep(0.4)
Chime.playNote(0, 13)
sleep(0.4)
Chime.playNote(0, 15)
sleep(0.2)
Chime.playNote(0, 17)
sleep(0.2)
Chime.playNote(0, 15)
sleep(0.2)
Chime.playNote(0, 17)
sleep(0.2)
Chime.playNote(0, 20)
sleep(0.4)
Chime.playNote(0, 17)
sleep(0.4)
Chime.playNote(0, 20)
sleep(0.4)
Chime.playNote(0, 22)
sleep(0.4)
Chime.playNote(0, 17)
sleep(0.4)
Chime.playNote(0, 22)
sleep(0.4)
Chime.playNote(0, 20)
sleep(0.4)
Chime.playNote(0, 17)
sleep(0.4)
Chime.playNote(0, 15)
sleep(0.4)
Chime.playNote(0, 13)
sleep(0.4)

end

function noon()
Chime.playNote(0, 12)
sleep(0.2)
Chime.playNote(0, 16)
sleep(0.1)
Chime.playNote(0, 19)
sleep(0.2)
Chime.playNote(0, 24)
sleep(0.3)
Chime.playNote(0, 24)
sleep(0.3)
Chime.playNote(0, 24)
sleep(0.3)
Chime.playNote(0, 24)
sleep(0.3)
Chime.playNote(0, 24)
sleep(0.1)
Chime.playNote(0, 23)
sleep(0.2)
Chime.playNote(0, 24)
sleep(0.1)
Chime.playNote(0, 19)
sleep(0.2)
Chime.playNote(0, 16)
sleep(0.1)
Chime.playNote(0, 19)
sleep(0.2)
Chime.playNote(0, 18)
sleep(0.1)
Chime.playNote(0, 17)
sleep(0.2)
Chime.playNote(0, 16)
sleep(0.1)
Chime.playNote(0, 15)
sleep(0.2)
Chime.playNote(0, 14)
sleep(0.3)
Chime.playNote(0, 12)
end

function preNight()
Chat.say("Monsters will spawn soon.  Hurry inside!")
Chime.playNote(0, 1)
sleep(0.3)
Chime.playNote(0, 4)
sleep(0.3)
Chime.playNote(0, 7)
end

function night()
Chat.say("Closing the main gate for your protection.")
rs.setOutput("back", true)
sleep(1.5)
Chime.playNote(0, 1)
sleep(0.3)
Chime.playNote(0, 4)
sleep(0.3)
Chime.playNote(0, 7)
sleep(0.3)
Chime.playNote(0, 10)
sleep(0.05)
Chime.playNote(0, 10)
sleep(0.05)
Chime.playNote(0, 10)
sleep(0.05)
Chime.playNote(0, 10)
sleep(0.05)
Chime.playNote(0, 10)
sleep(0.05)
end

local morning = os.setAlarm(5)
local noon = os.setAlarm(12)
local preNight = os.setAlarm(18)
local night = os.setAlarm(21)
while true do
local evt, arg = os.pullEvent()
if arg == morning then
morning()
elseif arg == noon then
noon()
elseif arg == preNight then
preNight()
elseif arg == night then
night()
end
end
Lyqyd #4
Posted 25 September 2013 - 11:43 PM
Why didn't you give us the whole code? Ugh.

Anyway, the line local morning = os.setAlarm(5) redefines the morning variable to a number, so it no longer holds your function. You can't have two different things in the same variable.
CCJJSax #5
Posted 25 September 2013 - 11:46 PM
Why didn't you give us the whole code? Ugh.

Anyway, the line local morning = os.setAlarm(5) redefines the morning variable to a number, so it no longer holds your function. You can't have two different things in the same variable.

I ended up editing my last post to have the entire code. However it seems so far like redefining those variables made it work. I'll post back if I find any issues with it.
CCJJSax #6
Posted 27 September 2013 - 10:56 PM
Well, it did work, but now something is going on with my last timer. It's not activating at time 20. Here is the pastebin link.

Line 160 is where it should be called and the function is at line 115.
BigTwisty #7
Posted 29 September 2013 - 12:48 AM
Well, it did work, but now something is going on with my last timer. It's not activating at time 20. Here is the pastebin link.

Line 160 is where it should be called and the function is at line 115.

First thing I notice is that night() is the only function that turns on redstone. If the other chime functions work, then I'd look at the chime peripheral and see if redstone affects thruway it works. An easy test would be to comment out line 117 and see if it starts working.