newf0und

get

Mar 28th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. -- Setup wireless modem
  2. local modem = peripheral.wrap("top") -- Wireless modem attached to 'top'
  3. modem.open(1) -- Open the modem on channel 1 for communication
  4.  
  5. -- Function to set the speed for each rotational speed controller
  6. local function setControllerSpeeds(left, right, front, back)
  7. -- Get connected controllers
  8. local leftController = peripheral.wrap("left")
  9. local rightController = peripheral.wrap("right")
  10. local frontController = peripheral.wrap("front")
  11. local backController = peripheral.wrap("back")
  12.  
  13. -- Set the speeds for each controller
  14. leftController.setTargetSpeed(left)
  15. rightController.setTargetSpeed(right)
  16. frontController.setTargetSpeed(front)
  17. backController.setTargetSpeed(back)
  18.  
  19. -- Print speeds to console
  20. print(
  21. "Left: " .. left .. " RPM | Right: " .. right .. " RPM | " ..
  22. "Front: " .. front .. " RPM | Back: " .. back .. " RPM"
  23. )
  24. end
  25.  
  26. -- Main loop to listen for transmitted data
  27. while true do
  28. -- Listen for data on channel 1
  29. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  30.  
  31. if channel == 1 then
  32. -- Extract the speed data
  33. local leftSpeed, rightSpeed, frontSpeed, backSpeed = table.unpack(message)
  34.  
  35. -- Set the controller speeds based on the received data
  36. setControllerSpeeds(leftSpeed, rightSpeed, frontSpeed, backSpeed)
  37. end
  38.  
  39. -- Small delay to prevent excessive looping
  40. sleep(0.1)
  41. end
  42.  
Advertisement
Add Comment
Please, Sign In to add comment