MegiRicky

SPAADrive_wheeled

Mar 17th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. -- drive Bradley.lua
  2. local modem = peripheral.find("modem")
  3. local engine = peripheral.find("Create_RotationSpeedController")
  4.  
  5. print("Input the controlChannel, default = 2100")
  6. local controlChannel = io.read()
  7. if controlChannel == "" then
  8. controlChannel = 2100
  9. end
  10. controlChannel = tonumber(controlChannel)
  11. modem.open(controlChannel)
  12.  
  13. local RPM = 0
  14. local turnLevel = 0
  15.  
  16. -- Redstone sides for cannon and autocannon
  17. local cannonSide = "top"
  18. local autocannonSide = "bottom"
  19.  
  20. print("Tank controller started.")
  21.  
  22. local controls = {
  23. accelerate = false,
  24. decelerate = false,
  25. turnLeft = false,
  26. turnRight = false,
  27. suspensionUp = false,
  28. suspensionDown = false,
  29. fireCannon = false,
  30. fireAutocannon = false
  31. }
  32.  
  33. local function executeControls()
  34. while true do
  35. if controls.turnLeft then
  36. redstone.setOutput("left",true)
  37. redstone.setOutput("right",false)
  38. elseif controls.turnRight then
  39. redstone.setOutput("left",false)
  40. redstone.setOutput("right",true)
  41. else
  42. redstone.setOutput("left",false)
  43. redstone.setOutput("right",false)
  44. end
  45.  
  46. if controls.accelerate then
  47. RPM = 256
  48. elseif controls.decelerate then
  49. RPM = -256
  50. else
  51. RPM = 0
  52. end
  53.  
  54. -- Fire controls
  55. if controls.fireCannon then
  56. print("Firing Cannon")
  57. redstone.setOutput("bottom", true)
  58. else
  59. redstone.setOutput("bottom", false)
  60. end
  61.  
  62. if controls.fireAutocannon then
  63. print("Firing Autocannon")
  64. redstone.setOutput("front", true)
  65. else
  66. redstone.setOutput("front", false)
  67. end
  68. engine.setTargetSpeed(RPM)
  69. sleep() -- Adjust the delay for control responsiveness
  70. end
  71. end
  72.  
  73. -- Function to listen for modem messages and update controls
  74. local function modem_message()
  75. while true do
  76. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  77. if channel == controlChannel then
  78. controls = message
  79. end
  80. end
  81. end
  82.  
  83. -- Run the functions in parallel
  84. parallel.waitForAll(
  85. modem_message,
  86. executeControls
  87. )
  88.  
Advertisement
Add Comment
Please, Sign In to add comment