Advertisement
JaMaNi133

PowerSwitch

Jan 16th, 2024 (edited)
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1. --pastebin get wA1JZcVA startup.lua
  2. clutch = "right"
  3. monitor = peripheral.wrap("monitor_0")
  4. speedController = peripheral.wrap("back")
  5. meter = peripheral.wrap("left")
  6. dir = -1
  7. mult = 8
  8. name1 = "Press"
  9. name2 = ""
  10.  
  11. autorpm = 0
  12. availableSU = 100
  13. enabled = false
  14. curSpeed = 16
  15. monitor.setBackgroundColor(colors.black)
  16. monitor.clear()
  17.  
  18.  
  19. function loop()
  20.     --loop
  21.     local remainingStr = string.gsub(meter.getLine(1), "%D+", "")
  22.     availableSU = tonumber(remainingStr)
  23.     monitor.setBackgroundColor(colors.orange)
  24.     monitor.setTextColor(colors.black)
  25.     monitor.setTextScale(0.5)
  26.     monitor.setCursorPos(1,1)
  27.     monitor.write(name1 .. "                 ")
  28.     monitor.setCursorPos(1,2)
  29.     monitor.write(name2 .. "                 ")
  30.     monitor.setCursorPos(1,3)
  31.     monitor.setBackgroundColor(colors.black)
  32.     monitor.setTextColor(colors.white)
  33.     monitor.write("enabled: ")
  34.     if enabled then
  35.         monitor.setBackgroundColor(colors.green)
  36.         monitor.setTextColor(colors.black)
  37.         monitor.write("ON ")
  38.     else
  39.         monitor.setBackgroundColor(colors.red)
  40.         monitor.setTextColor(colors.black)
  41.         monitor.write("OFF")
  42.     end
  43.     monitor.setCursorPos(1,4)
  44.     monitor.setBackgroundColor(colors.black)
  45.     monitor.setTextColor(colors.white)
  46.     monitor.write("speed: " .. tostring(curSpeed) .. "rpm           ")
  47.     monitor.setCursorPos(1,5)
  48.     monitor.setBackgroundColor(colors.black)
  49.     monitor.setTextColor(colors.white)
  50.     monitor.write("power: " .. tostring(curSpeed*mult) .. "su           ")
  51.     monitor.setCursorPos(1,6)
  52.     monitor.setBackgroundColor(colors.black)
  53.     monitor.setTextColor(colors.white)
  54.     monitor.write("avai.: " .. tostring(availableSU) .. "su           ")
  55.  
  56.     monitor.setCursorPos(1,8)
  57.     monitor.setBackgroundColor(colors.gray)
  58.     monitor.setTextColor(colors.white)
  59.     monitor.write("  8   16   32  ")
  60.     monitor.setCursorPos(1,9)
  61.     monitor.write(" 64   128  256 ")
  62.    
  63.     monitor.setCursorPos(1,10)
  64.     monitor.setBackgroundColor(colors.red)
  65.     monitor.setTextColor(colors.black)
  66.     if enabled then
  67.         autorpm = math.floor((availableSU + (curSpeed*mult)) / mult)
  68.     else
  69.         autorpm = math.floor(availableSU / mult)
  70.     end
  71.     if autorpm >= 256 then
  72.         autorpm = 256
  73.     end
  74.     monitor.write("Auto: " .. tostring(autorpm) .. "rpm                  ")
  75.    
  76.    
  77.     redstone.setOutput(clutch,enabled)
  78.     speedController.setTargetSpeed(curSpeed*dir)
  79.    
  80. end
  81.  
  82.  
  83.    
  84. function generalUpdate()
  85.     while true do
  86.         loop()
  87.         sleep(0.01)
  88.     end
  89. end
  90.  
  91. function respondMonitorTouched()
  92.     while true do
  93.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  94.         if yPos == 3 then
  95.             enabled = not(enabled)
  96.         end
  97.         if yPos >= 8 and yPos <= 9 then
  98.             if xPos >= 1 and xPos <= 5 then
  99.                 curSpeed = 8
  100.             end
  101.             if xPos >= 6 and xPos <= 10 then
  102.                 curSpeed = 16
  103.             end
  104.             if xPos >= 11 and xPos <= 15 then
  105.                 curSpeed = 32
  106.             end
  107.             if yPos == 9 then
  108.                 curSpeed = curSpeed * 8
  109.             end
  110.         end
  111.         if yPos == 10 then
  112.             curSpeed = autorpm
  113.         end
  114.     end
  115. end
  116. sleep(15)
  117. parallel.waitForAny(respondMonitorTouched,generalUpdate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement