Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- BigBen Chimes
- -- (c) Richard Samphire aka Ramdor 2012
- -- v1.1
- --
- -- changes
- -- v1.1 - changed timer code
- -- v1.0 - initial release
- --
- -- You may edit/copy/redistribute/whatever this
- -- code in any way. Just a credit listing this
- -- pastebin link (pastebin.com/bJEmGyeD) will be fine :)
- local bHourChime = true -- chime on the hour
- local bChimeEachHour = true -- chime out the hours if bHourChime=true
- local b1stQuarterChime = false -- chime on the quarter past hour
- local bHalfHourChime = true -- chime on the half hour
- local b3rdQuarterChime = false -- chime on the quarter to the hour
- local nBell1 = 0 -- piano
- local nBell2 = 4 -- bass guitar
- local nSlow = 0.5 -- long note lengh
- local nFast = 0.25 -- short note lengh
- -- side the ironnote block is attached
- local objNote = peripheral.wrap("back")
- local nState = 1
- local bDone = true
- local h = 0
- local m = 0
- local function parseTime()
- local t = os.time()
- local sT = textutils.formatTime(t, true)
- local n = string.find(sT, ":")
- if(n>0) then
- h = tonumber(string.sub(sT,1,n-1))
- m = tonumber(string.sub(sT,n+1))
- end
- end
- local function s1()
- objNote.playNote(nBell1,14) -- g#4
- os.sleep(nFast)
- objNote.playNote(nBell1,12) -- f#4
- os.sleep(nFast)
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nFast)
- objNote.playNote(nBell1,5) -- b3
- os.sleep(nSlow)
- end
- local function s2()
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nFast)
- objNote.playNote(nBell1,14) -- g#4
- os.sleep(nFast)
- objNote.playNote(nBell1,12) -- f#4
- os.sleep(nFast)
- objNote.playNote(nBell1,5) -- b3
- os.sleep(nSlow)
- end
- local function s3()
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nFast)
- objNote.playNote(nBell1,12) -- f#4
- os.sleep(nFast)
- objNote.playNote(nBell1,14) -- g#4
- os.sleep(nFast)
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nSlow)
- end
- local function s4()
- objNote.playNote(nBell1,14) -- g#4
- os.sleep(nFast)
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nFast)
- objNote.playNote(nBell1,12) -- f#4
- os.sleep(nFast)
- objNote.playNote(nBell1,5) -- b3
- os.sleep(nSlow)
- end
- local function s5()
- objNote.playNote(nBell1,5) -- b3
- os.sleep(nFast)
- objNote.playNote(nBell1,12) -- f#4
- os.sleep(nFast)
- objNote.playNote(nBell1,14) -- g#4
- os.sleep(nFast)
- objNote.playNote(nBell1,10) -- e4
- os.sleep(nSlow)
- end
- local function fristQuarter()
- s1()
- end
- local function halfHour()
- s2()
- s3()
- end
- local function thirdQuarter()
- s4()
- s5()
- s1()
- end
- local function hour()
- s2()
- s3()
- s4()
- s5()
- end
- local function bigBen()
- -- hour chime should be e3
- -- closest we can go down to is f#3
- -- which is 0. Will use 10 instead
- -- with nBell2 instrument
- objNote.playNote(nBell2,10)
- os.sleep(nSlow)
- end
- parseTime()
- if (m>=0 and m<=14) then
- nState = 1
- elseif (m>=15 and m<=29) then
- nState = 2
- elseif (m>=30 and m<=44) then
- nState = 3
- elseif (m>=45 and m<=59) then
- nState = 4
- else
- -- nothing to do
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Big Ben Chimes - by Ramdor (c) 2012")
- print("Press 'Q' to terminate.")
- local bRunning = true
- local nTimer
- while(bRunning) do
- parseTime()
- if (m>=0 and m<=14) and nState==1 then
- bDone = false
- elseif (m>=15 and m<=29) and nState==2 then
- bDone = false
- elseif (m>=30 and m<=44) and nState==3 then
- bDone = false
- elseif (m>=45 and m<=59) and nState==4 then
- bDone = false
- else
- -- nothing to do
- end
- if bDone == false then
- if nState==1 then
- if bHourChime then
- hour()
- if bChimeEachHour then
- local hh
- hh = h % 12
- if (h==12) then
- hh=12
- end
- for x=1,hh do
- bigBen()
- end
- end
- end
- nState=2
- elseif nState==2 then
- if b1stQuarterChime then
- fristQuarter()
- end
- nState=3
- elseif nState==3 then
- if bHalfHourChime then
- halfHour()
- end
- nState=4
- elseif nState==4 then
- if b3rdQuarterChime then
- thirdQuarter()
- end
- nState=1
- else
- -- should never get here
- end
- bDone = true
- end
- nTimer = os.startTimer(1)
- -- wait for a keypress or timer event
- local event, param1 = os.pullEvent()
- while (event~="char" and not (event=="timer" and param1==nTimer)) do
- event, param1 = os.pullEvent()
- end
- if(event=="char") then
- if(param1=="q" or param1=="Q") then
- bRunning=false
- end
- end
- end
- print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment