Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Setup
- term.clear()
- term.setCursorPos(1, 1)
- local configFile = "tank_config.txt"
- local tankName, tankColor, refreshInterval
- -- Function to load config from file
- local function loadConfig()
- if fs.exists(configFile) then
- local file = fs.open(configFile, "r")
- local name = file.readLine()
- local color = file.readLine()
- local interval = tonumber(file.readLine())
- file.close()
- return name, color, interval
- end
- return nil
- end
- -- Function to save config to file
- local function saveConfig(name, color, interval)
- local file = fs.open(configFile, "w")
- file.writeLine(name)
- file.writeLine(color)
- file.writeLine(tostring(interval))
- file.close()
- end
- -- Try to load existing config
- tankName, tankColor, refreshInterval = loadConfig()
- if not tankName or not tankColor or not refreshInterval then
- -- Ask for tank name
- write("Enter tank name: ")
- tankName = read()
- -- Ask for color name
- write("Enter tank color (e.g. red, blue): ")
- tankColor = read()
- -- Ask for refresh interval (default 1s)
- write("Enter refresh interval (seconds, default 1): ")
- local intervalInput = read()
- refreshInterval = tonumber(intervalInput)
- if not refreshInterval then refreshInterval = 1 end
- -- Save config
- saveConfig(tankName, tankColor, refreshInterval)
- end
- -- Wrap tank peripheral at the back
- local tankPeripheral = peripheral.wrap("back")
- if not tankPeripheral then
- print("No peripheral found at back!")
- return
- end
- -- Wrap modem (adjust side if needed)
- local modem = peripheral.wrap("left")
- if not modem then
- print("No modem found on left!")
- return
- end
- print("Starting tank update sender for tank '" .. tankName .. "' every " .. refreshInterval .. "s...")
- -- Main loop
- while true do
- -- Read tank data from peripheral
- local tankData = tankPeripheral.getInfo and tankPeripheral.getInfo() or nil
- if tankData then
- -- Add name and color fields
- tankData.name = tankName
- tankData.color = tankColor
- -- Send the message on channel 1
- modem.transmit(1, 1, tankData)
- print("Sent update for " .. tankName)
- else
- print("Failed to read tank data.")
- end
- sleep(refreshInterval)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement