ramdor

BigBen

Dec 28th, 2012
1,892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. -- BigBen Chimes
  2. -- (c) Richard Samphire aka Ramdor 2012
  3. -- v1.1
  4. --
  5. -- changes
  6. -- v1.1 - changed timer code
  7. -- v1.0 - initial release
  8. --
  9. -- You may edit/copy/redistribute/whatever this
  10. -- code in any way.  Just a credit listing this
  11. -- pastebin link (pastebin.com/bJEmGyeD) will be fine :)
  12.  
  13. local bHourChime = true -- chime on the hour
  14. local bChimeEachHour = true -- chime out the hours if bHourChime=true
  15. local b1stQuarterChime = false -- chime on the quarter past hour
  16. local bHalfHourChime = true -- chime on the half hour
  17. local b3rdQuarterChime = false -- chime on the quarter to the hour
  18.  
  19. local nBell1 = 0  -- piano
  20. local nBell2 = 4  -- bass guitar
  21. local nSlow = 0.5 -- long note lengh
  22. local nFast = 0.25 -- short note lengh
  23.  
  24. -- side the ironnote block is attached
  25. local objNote = peripheral.wrap("back")
  26.  
  27. local nState = 1
  28. local bDone = true
  29. local h = 0
  30. local m = 0
  31.  
  32. local function parseTime()
  33.   local t = os.time()
  34.   local sT = textutils.formatTime(t, true)
  35.  
  36.   local n = string.find(sT, ":")
  37.  
  38.   if(n>0) then
  39.     h = tonumber(string.sub(sT,1,n-1))
  40.     m = tonumber(string.sub(sT,n+1))
  41.   end
  42. end
  43.  
  44. local function s1()
  45.   objNote.playNote(nBell1,14) -- g#4
  46.   os.sleep(nFast)
  47.   objNote.playNote(nBell1,12) -- f#4
  48.   os.sleep(nFast)
  49.   objNote.playNote(nBell1,10) -- e4
  50.   os.sleep(nFast)
  51.   objNote.playNote(nBell1,5) -- b3
  52.   os.sleep(nSlow)
  53. end
  54.  
  55. local function s2()
  56.   objNote.playNote(nBell1,10) -- e4
  57.   os.sleep(nFast)
  58.   objNote.playNote(nBell1,14) -- g#4
  59.   os.sleep(nFast)
  60.   objNote.playNote(nBell1,12) -- f#4
  61.   os.sleep(nFast)
  62.   objNote.playNote(nBell1,5) -- b3
  63.   os.sleep(nSlow)
  64. end
  65.  
  66. local function s3()
  67.   objNote.playNote(nBell1,10) -- e4
  68.   os.sleep(nFast)
  69.   objNote.playNote(nBell1,12) -- f#4
  70.   os.sleep(nFast)
  71.   objNote.playNote(nBell1,14) -- g#4
  72.   os.sleep(nFast)
  73.   objNote.playNote(nBell1,10) -- e4
  74.   os.sleep(nSlow)
  75. end
  76.  
  77. local function s4()
  78.   objNote.playNote(nBell1,14) -- g#4
  79.   os.sleep(nFast)
  80.   objNote.playNote(nBell1,10) -- e4
  81.   os.sleep(nFast)
  82.   objNote.playNote(nBell1,12) -- f#4
  83.   os.sleep(nFast)
  84.   objNote.playNote(nBell1,5) -- b3
  85.   os.sleep(nSlow)
  86. end
  87.  
  88. local function s5()
  89.   objNote.playNote(nBell1,5) -- b3
  90.   os.sleep(nFast)
  91.   objNote.playNote(nBell1,12) -- f#4
  92.   os.sleep(nFast)
  93.   objNote.playNote(nBell1,14) -- g#4
  94.   os.sleep(nFast)
  95.   objNote.playNote(nBell1,10) -- e4
  96.   os.sleep(nSlow)
  97. end
  98.  
  99. local function fristQuarter()
  100.   s1()
  101. end
  102.  
  103. local function halfHour()
  104.   s2()
  105.   s3()
  106. end
  107.  
  108. local function thirdQuarter()
  109.   s4()
  110.   s5()
  111.   s1()
  112. end
  113.  
  114. local function hour()
  115.   s2()
  116.   s3()
  117.   s4()
  118.   s5()
  119. end
  120.  
  121. local function bigBen()
  122. -- hour chime should be e3
  123. -- closest we can go down to is f#3
  124. -- which is 0. Will use 10 instead
  125. -- with nBell2 instrument
  126.   objNote.playNote(nBell2,10)
  127.   os.sleep(nSlow)
  128. end
  129.  
  130. parseTime()
  131. if (m>=0 and m<=14)  then
  132.   nState = 1
  133. elseif (m>=15 and m<=29) then
  134.   nState = 2
  135. elseif (m>=30 and m<=44)  then
  136.   nState = 3
  137. elseif (m>=45 and m<=59) then
  138.   nState = 4
  139. else
  140.   -- nothing to do
  141. end
  142.  
  143. term.clear()
  144. term.setCursorPos(1,1)
  145. print("Big Ben Chimes - by Ramdor (c) 2012")
  146. print("Press 'Q' to terminate.")
  147.  
  148. local bRunning = true
  149. local nTimer
  150.  
  151. while(bRunning) do
  152.   parseTime()
  153.  
  154.   if (m>=0 and m<=14) and nState==1 then
  155.     bDone = false
  156.   elseif (m>=15 and m<=29) and nState==2 then
  157.     bDone = false
  158.   elseif (m>=30 and m<=44) and nState==3 then
  159.     bDone = false
  160.   elseif (m>=45 and m<=59) and nState==4 then
  161.     bDone = false
  162.   else
  163.     -- nothing to do
  164.   end
  165.  
  166.   if bDone == false then
  167.     if nState==1 then
  168.       if bHourChime then
  169.         hour()
  170.  
  171.         if bChimeEachHour then
  172.           local hh
  173.           hh = h % 12
  174.  
  175.           if (h==12) then
  176.             hh=12
  177.           end
  178.  
  179.           for x=1,hh do
  180.             bigBen()
  181.           end
  182.         end
  183.       end
  184.  
  185.       nState=2
  186.     elseif nState==2 then
  187.       if b1stQuarterChime then
  188.         fristQuarter()
  189.       end
  190.       nState=3
  191.     elseif nState==3 then
  192.       if bHalfHourChime then
  193.         halfHour()
  194.       end
  195.       nState=4
  196.     elseif nState==4 then
  197.       if b3rdQuarterChime then
  198.         thirdQuarter()
  199.       end
  200.       nState=1
  201.     else
  202.       -- should never get here
  203.     end
  204.  
  205.     bDone = true
  206.   end
  207.  
  208.   nTimer = os.startTimer(1)
  209.  
  210.   -- wait for a keypress or timer event
  211.   local event, param1 = os.pullEvent()
  212.   while (event~="char" and not (event=="timer" and param1==nTimer)) do
  213.     event, param1 = os.pullEvent()
  214.   end
  215.  
  216.   if(event=="char") then
  217.      if(param1=="q" or param1=="Q") then
  218.        bRunning=false
  219.      end
  220.   end
  221. end
  222. print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment