Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- color_control.lua
- -- 1. Define color-to-relay mapping
- local colorToRelay = {
- red = { relayName = "redstone_relay_0", side = "front" },
- blue = { relayName = "redstone_relay_1", side = "front" },
- green = { relayName = "redstone_relay_2", side = "front" },
- purple = { relayName = "redstone_relay_3", side = "front" },
- pink = { relayName = "redstone_relay_4", side = "front" },
- yellow = { relayName = "redstone_relay_5", side = "front" },
- }
- -- Optional: If you only have fewer relays, remove unused colors,
- -- or if you have more relays, add more color mappings.
- -- 2. Helper: turn off all relays
- local function turnOffAll()
- for _, info in pairs(colorToRelay) do
- local relay = peripheral.wrap(info.relayName)
- if relay then
- relay.setOutput(info.side, false)
- end
- end
- end
- turnOffAll() -- ensure everything is off at start
- -- Keep track of what's currently active
- local currentColor = nil
- local function activateColor(newColor)
- -- If the incoming argument isn't a string, log and return
- if type(newColor) ~= "string" then
- print("activateColor received a non-string value:", tostring(newColor))
- return
- end
- -- 3A. Turn off the old color's relay
- if currentColor and colorToRelay[currentColor] then
- local oldInfo = colorToRelay[currentColor]
- local oldRelay = peripheral.wrap(oldInfo.relayName)
- if oldRelay then
- oldRelay.setOutput(oldInfo.side, false)
- end
- end
- -- 3B. Turn on the new color's relay
- if colorToRelay[newColor] then
- local info = colorToRelay[newColor]
- local relay = peripheral.wrap(info.relayName)
- if relay then
- relay.setOutput(info.side, true)
- currentColor = newColor
- print("Activated color: "..newColor)
- else
- print("Relay peripheral not found for color: "..newColor)
- end
- else
- print("Unknown color: "..newColor)
- end
- end
- -- 4. Connect to your Node.js WebSocket server
- local url = "ws://kev.unoriginal.studio:8080"
- print("Connecting to "..url)
- http.websocketAsync(url)
- -- 5. Event loop to handle WebSocket messages
- while true do
- local event, url, message = os.pullEvent()
- if event == "websocket_success" then
- print("WebSocket connected!")
- -- Optionally send a greeting
- message.send("Hello from CC: Tweaked")
- elseif event == "websocket_failure" then
- print("Failed to connect:", message)
- break -- exit the program, or handle reconnection logic
- elseif event == "websocket_message" then
- -- p2 = the websocket handle, p3 = the messag content (color)
- local colorMsg = message
- print("Received color:", colorMsg)
- activateColor(colorMsg)
- elseif event == "websocket_closed" then
- print("WebSocket closed.")
- break
- end
- end
- print("Exiting program.")
Add Comment
Please, Sign In to add comment