Guest User

RainbowLampsRandom

a guest
Nov 12th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. local modem = peripheral.wrap("back")
  2. local lamps = modem.getNamesRemote()
  3.  
  4. local time = 0
  5. local frequency = 0.1
  6. local phaseMod = 0.3
  7. local delay = 0.08
  8.  
  9.  
  10. function rgbToHex(rgb)
  11.   local hexadecimal = '0x'
  12.  
  13.   for key, value in pairs(rgb) do
  14.     local hex = ''
  15.  
  16.     while(value > 0)do
  17.       local index = math.fmod(value, 16) + 1
  18.       value = math.floor(value / 16)
  19.       hex = string.sub('0123456789ABCDEF', index, index) .. hex
  20.     end
  21.  
  22.     if(string.len(hex) == 0)then
  23.       hex = '00'
  24.  
  25.     elseif(string.len(hex) == 1)then
  26.       hex = '0' .. hex
  27.     end
  28.  
  29.     hexadecimal = hexadecimal .. hex
  30.   end
  31.  
  32.   return hexadecimal
  33. end
  34.  
  35.  
  36. function RGBSine(redPhase,bluePhase,greenPhase)
  37.  
  38.   local red = math.sin(frequency*time + (redPhase*phaseMod) + 0) * 127 + 128
  39.   local green = math.sin(frequency*time + (bluePhase*phaseMod) + 2) * 127 + 128
  40.   local blue = math.sin(frequency*time + (greenPhase*phaseMod) + 4) * 127 + 128
  41.  
  42.   local colores = rgbToHex({red,green,blue})
  43.  
  44.   return tonumber(colores)
  45. end
  46.  
  47.  
  48. while true do
  49.  
  50.   sleep(delay)
  51.  
  52.   for i=1,#lamps do
  53.     modem.callRemote(tostring(lamps[i]), "setColor", RGBSine(i,i,i))
  54.   end
  55.  
  56.   time = time+1
  57.  
  58. end
Add Comment
Please, Sign In to add comment