Advertisement
Guest User

RainbowLampsLinear

a guest
Nov 12th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. local modem = peripheral.wrap("back")
  2. local lamps = modem.getNamesRemote()
  3.  
  4. --Modifiers
  5. local time = 0
  6. local timeScale = 1
  7. local frequency = 0.1
  8. local phaseMod = 0.3
  9. local delay = 0.08
  10.  
  11. --Organizes the list of lamps by the order you activated the modems
  12. local function setupList()
  13.  
  14.   lamps = modem.getNamesRemote()
  15.   local buf = ""
  16.   local cut = 0
  17.  
  18.   while cut+1 < #lamps-1 do
  19.  
  20.     for j=1+cut,#lamps do
  21.  
  22.       if(tonumber(string.sub(tostring(lamps[j]),24)) < tonumber(string.sub(tostring(lamps[cut+1]),24))) then
  23.         buf = tostring(lamps[cut+1])
  24.         lamps[cut+1] = tostring(lamps[j])
  25.         lamps[j] = buf
  26.       end
  27.  
  28.     end
  29.  
  30.     cut = cut+1
  31.   end
  32. end
  33.  
  34. setupList()
  35.  
  36. local function rgbToHex(rgb)
  37.   local hexadecimal = '0x'
  38.  
  39.   for key, value in pairs(rgb) do
  40.     local hex = ''
  41.  
  42.     while(value > 0)do
  43.       local index = math.fmod(value, 16) + 1
  44.       value = math.floor(value / 16)
  45.       hex = string.sub('0123456789ABCDEF', index, index) .. hex
  46.     end
  47.  
  48.     if(string.len(hex) == 0)then
  49.       hex = '00'
  50.  
  51.     elseif(string.len(hex) == 1)then
  52.       hex = '0' .. hex
  53.     end
  54.  
  55.     hexadecimal = hexadecimal .. hex
  56.   end
  57.  
  58.   return hexadecimal
  59. end
  60.  
  61.  
  62. local function RGBSine(redPhase,bluePhase,greenPhase)
  63.  
  64.   local red = math.sin(frequency*time + (redPhase*phaseMod) + 0) * 127 + 128
  65.   local green = math.sin(frequency*time + (bluePhase*phaseMod) + 2) * 127 + 128
  66.   local blue = math.sin(frequency*time + (greenPhase*phaseMod) + 4) * 127 + 128
  67.  
  68.   local colores = rgbToHex({red,green,blue})
  69.  
  70.   return tonumber(colores)
  71. end
  72.  
  73. --run loop
  74. local function start()
  75.  
  76.   for i=1,#lamps do
  77.     modem.callRemote(lamps[i], "setColor", RGBSine(i,i,i))
  78.   end
  79.  
  80.   time = time+(1*timeScale)
  81.  
  82. end
  83.  
  84.  while true do
  85.   sleep(delay)
  86.   if pcall(start) == false then
  87.     setupList()
  88.   end
  89.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement