Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Setup wireless modem
- local modem = peripheral.wrap("top") -- Wireless modem attached to 'top'
- modem.open(1) -- Open the modem on channel 1 for communication
- -- Function to set the speed for each rotational speed controller
- local function setControllerSpeeds(left, right, front, back)
- -- Get connected controllers
- local leftController = peripheral.wrap("left")
- local rightController = peripheral.wrap("right")
- local frontController = peripheral.wrap("front")
- local backController = peripheral.wrap("back")
- -- Set the speeds for each controller
- leftController.setTargetSpeed(left)
- rightController.setTargetSpeed(right)
- frontController.setTargetSpeed(front)
- backController.setTargetSpeed(back)
- -- Print speeds to console
- print(
- "Left: " .. left .. " RPM | Right: " .. right .. " RPM | " ..
- "Front: " .. front .. " RPM | Back: " .. back .. " RPM"
- )
- end
- -- Main loop to listen for transmitted data
- while true do
- -- Listen for data on channel 1
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if channel == 1 then
- -- Extract the speed data
- local leftSpeed, rightSpeed, frontSpeed, backSpeed = table.unpack(message)
- -- Set the controller speeds based on the received data
- setControllerSpeeds(leftSpeed, rightSpeed, frontSpeed, backSpeed)
- end
- -- Small delay to prevent excessive looping
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment