bobmarley12345

16-step colour sequencer

Dec 12th, 2021 (edited)
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. SIDE_INPUT = "left"
  2. SIDE_OUTPUT = "right"
  3. COLOUR_OFFSET = 1
  4. COLOUR_MAX = 16
  5. --DELAY_TIME = 1
  6. CURRENT_COLOUR = 0
  7.  
  8. function save_config()
  9.     local fHandle = fs.open("config.txt", "w")
  10.     fHandle.writeLine(SIDE_INPUT)
  11.     fHandle.writeLine(SIDE_OUTPUT)
  12.     fHandle.writeLine(COLOUR_OFFSET)
  13.     fHandle.writeLine(COLOUR_MAX)
  14.     --fHandle.writeLine(DELAY_TIME)
  15.     fHandle.close()
  16. end
  17.  
  18. function load_config()
  19.     local fHandle = fs.open("config.txt", "r")
  20.     SIDE_INPUT = fHandle.readLine()
  21.     SIDE_OUTPUT = fHandle.readLine()
  22.     COLOUR_OFFSET = tonumber(fHandle.readLine())
  23.     COLOUR_MAX = tonumber(fHandle.readLine())
  24.     --DELAY_TIME = tonumber(fHandle.readLine())
  25.     fHandle.close()
  26. end
  27.  
  28. function verify_side(side)
  29.     if (side == "left") then
  30.         return true
  31.     elseif (side == "right") then
  32.         return true
  33.     elseif (side == "front") then
  34.         return true
  35.     elseif (side == "back") then
  36.         return true
  37.     elseif (side == "top") then
  38.         return true
  39.     elseif (side == "bottom") then
  40.         return true
  41.     else
  42.         return false
  43.     end
  44. end
  45.  
  46. function numberToColour(num)
  47.     if (num == 1) then
  48.         return colours.white
  49.     elseif (num == 2) then
  50.         return colours.orange
  51.     elseif (num == 3) then
  52.         return colours.magenta
  53.     elseif (num == 4) then
  54.         return colours.lightBlue
  55.     elseif (num == 5) then
  56.         return colours.yellow
  57.     elseif (num == 6) then
  58.         return colours.lime
  59.     elseif (num == 7) then
  60.         return colours.pink
  61.     elseif (num == 8) then
  62.         return colours.grey
  63.     elseif (num == 9) then
  64.         return colours.lightGrey
  65.     elseif (num == 10) then
  66.         return colours.cyan
  67.     elseif (num == 11) then
  68.         return colours.purple
  69.     elseif (num == 12) then
  70.         return colours.blue
  71.     elseif (num == 13) then
  72.         return colours.brown
  73.     elseif (num == 14) then
  74.         return colours.green
  75.     elseif (num == 15) then
  76.         return colours.red
  77.     else
  78.         return colours.black
  79.     end
  80. end
  81.  
  82. term.setCursorPos(1,1)
  83. term.clear()
  84. if (fs.exists("config.txt")) then
  85.     load_config()
  86.     print("Loaded config!")
  87. else
  88.     print("Enter the input side: ")
  89.     SIDE_INPUT = read();
  90.     if (verify_side(SIDE_INPUT) == false) then
  91.         print("Unknown side: " .. SIDE_INPUT)
  92.         return
  93.     end
  94.    
  95.     print("Enter the output rednet side: ")
  96.     SIDE_OUTPUT = read()
  97.     if (verify_side(SIDE_OUTPUT) == false) then
  98.         print("Unknown side: " .. SIDE_OUTPUT)
  99.         return
  100.     end
  101.    
  102.     print("Enter the starting colour number (1-16)")
  103.     COLOUR_OFFSET = tonumber(read());
  104.     if (COLOUR_OFFSET < 1 or COLOUR_OFFSET > 16) then
  105.         print("Colour number (" .. COLOUR_OFFSET .. ") was not between 1 and 16")
  106.         return
  107.     end
  108.    
  109.     print("Enter the end colour number (" .. COLOUR_OFFSET .. "-16)")
  110.     COLOUR_MAX = tonumber(read());
  111.     if (COLOUR_MAX < 1 or COLOUR_MAX > 16) then
  112.         print("Colour number (" .. COLOUR_MAX .. ") was not between 1 and 16")
  113.         return
  114.     end
  115.    
  116.     if (COLOUR_OFFSET == COLOUR_MAX) then
  117.         print("Colour offset and max value are the same??? (" .. COLOUR_OFFSET .. ")")
  118.         return
  119.     end
  120.  
  121.     CURRENT_COLOUR = COLOUR_OFFSET
  122.  
  123.     --print("Enter the delay time between steps (in seconds)")
  124.     --DELAY_TIME = tonumber(read());
  125.     --if (DELAY_TIME < 0) then
  126.     --    print("Delay cannot be negative!!!")
  127.     --    return
  128.     --end
  129.  
  130.     save_config()
  131.     print("Saved configuration to file!")
  132. end
  133.  
  134. while (true) do
  135.     local event = os.pullEvent("redstone")
  136.     if (rs.getInput(SIDE_INPUT)) then    
  137.         local col = numberToColour(CURRENT_COLOUR)
  138.         --print("Colour type = " .. type(col) .. ", out type = " .. type(SIDE_OUTPUT))
  139.         -- print("Set colour number: " .. CURRENT_COLOUR .. " (" .. col .. ")")
  140.         redstone.setBundledOutput(SIDE_OUTPUT, col)
  141.  
  142.         CURRENT_COLOUR = CURRENT_COLOUR + 1
  143.         if (CURRENT_COLOUR > COLOUR_MAX) then
  144.             CURRENT_COLOUR = COLOUR_OFFSET
  145.         end
  146.     end
  147. end
Add Comment
Please, Sign In to add comment