Advertisement
lucasmontec

tankReporter

Jul 6th, 2025 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. -- Setup
  2. term.clear()
  3. term.setCursorPos(1, 1)
  4.  
  5. local configFile = "tank_config.txt"
  6. local tankName, tankColor, refreshInterval
  7.  
  8. -- Function to load config from file
  9. local function loadConfig()
  10. if fs.exists(configFile) then
  11. local file = fs.open(configFile, "r")
  12. local name = file.readLine()
  13. local color = file.readLine()
  14. local interval = tonumber(file.readLine())
  15. file.close()
  16. return name, color, interval
  17. end
  18. return nil
  19. end
  20.  
  21. -- Function to save config to file
  22. local function saveConfig(name, color, interval)
  23. local file = fs.open(configFile, "w")
  24. file.writeLine(name)
  25. file.writeLine(color)
  26. file.writeLine(tostring(interval))
  27. file.close()
  28. end
  29.  
  30. -- Try to load existing config
  31. tankName, tankColor, refreshInterval = loadConfig()
  32.  
  33. if not tankName or not tankColor or not refreshInterval then
  34. -- Ask for tank name
  35. write("Enter tank name: ")
  36. tankName = read()
  37.  
  38. -- Ask for color name
  39. write("Enter tank color (e.g. red, blue): ")
  40. tankColor = read()
  41.  
  42. -- Ask for refresh interval (default 1s)
  43. write("Enter refresh interval (seconds, default 1): ")
  44. local intervalInput = read()
  45. refreshInterval = tonumber(intervalInput)
  46. if not refreshInterval then refreshInterval = 1 end
  47.  
  48. -- Save config
  49. saveConfig(tankName, tankColor, refreshInterval)
  50. end
  51.  
  52. -- Wrap tank peripheral at the back
  53. local tankPeripheral = peripheral.wrap("back")
  54. if not tankPeripheral then
  55. print("No peripheral found at back!")
  56. return
  57. end
  58.  
  59. -- Wrap modem (adjust side if needed)
  60. local modem = peripheral.wrap("left")
  61. if not modem then
  62. print("No modem found on left!")
  63. return
  64. end
  65.  
  66. print("Starting tank update sender for tank '" .. tankName .. "' every " .. refreshInterval .. "s...")
  67.  
  68. -- Main loop
  69. while true do
  70. -- Read tank data from peripheral
  71. local tankData = tankPeripheral.getInfo and tankPeripheral.getInfo() or nil
  72. if tankData then
  73. -- Add name and color fields
  74. tankData.name = tankName
  75. tankData.color = tankColor
  76.  
  77. -- Send the message on channel 1
  78. modem.transmit(1, 1, tankData)
  79. print("Sent update for " .. tankName)
  80. else
  81. print("Failed to read tank data.")
  82. end
  83.  
  84. sleep(refreshInterval)
  85. end
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement