u/Pillow51

Do pullEvents behave differently inside FOR loops ?
▲ 7 r/ComputerCraft+1 crossposts

Do pullEvents behave differently inside FOR loops ?

Some context. I'm making an articulated arm with create Aeronautics controlled by computers.

3 motors to get maximum movement. Highly inefficient, cool as fck

My problem resides in the way i've been controlling the motors.
All 3 motors have individual computers with modems, all ready to recieve instructions from the main pc. They are supposed to receive an instruction, and sleep continuously while the motor is active, and only at the end of the movement will they send a reply to the main pc, which will trigger the next commands.
The arm is supposed to be slow, it's supposed to move only one motor at a time (intentional).
When i make a simple basic list of modem,transmits() ,the arm behaves as intended.

this proof of concept worked as intended

This above is a very simple instruction to make the arm move slightly. My problem was this was ugly and tedious to expand. So i made a much cleaner(?) and more customisable version that was supposed to make it MUCH more simple to make new movement prompts later down the line.

function movementControl(a)
  local actionCount = 0
  local directionValue = 0
  local turnorder = {{1,2},{5,6},{3,4},{1,2},{3,4},{5,6},{1,2}}

  for i, angle in ipairs(a) do
    if angle == 'forward' then
      directionValue = -1

    elseif angle == 'backward' then
      directionValue = 1

    elseif angle =='motorstart' then
      rs.setOutput("back",true)
      sleep(3)

    elseif angle == 'motorstop' then
      rs.setOutput("back",false)
      sleep(5)

    elseif type(angle)=="number" then
      actionCount = actionCount + 1
      modem.transmit(turnorder[actionCount][1],turnorder[actionCount][2],{angle,directionValue})
      sleep(0.1)
      modem.open(turnorder[actionCount][2])
      local event, side ,channel, replyChannel, message, distance = os.pullEvent("modem_message")
      modem.close(turnorder[actionCount][2])
      sleep(0.1)

    end
  end



end


local harvestwheat1 = {'forward',30,20,23,'motorstart',30,'motorstop','backward',23,20,60}
local harvestwheat2 = {'forward',100,20,23,'motorstart',40,'motorstop','backward',23,20,140}
local harvestcarrot1 = {'forward',110,70,47,'motorstart',20,'motorstop','backward',47,70,130}


movementControl(harvestwheat1)

Now the actual problem is that the os.pullEvent i do when in my for loop dont seem to actually pause the computer. The loop kinda just continues on without waiting for the response from the motors.
Is there a specificity to Lua that i'm not getting (not likely) ? is there a specificity that my not good at coding ass doesn't understand (much more likely) ?

Ty for the help, and if people have better ways of doing any of this i might cave in and just redo the code from scratch with some suggestions ><

local gear = peripheral.wrap("right")
local modem = peripheral.wrap("left")
modem.open(1)


while true do
  
  local event, side ,channel, replyChannel, message, distance = os.pullEvent("modem_message")


  gear.rotate(message[1],message[2])


  while gear.isRunning() do
    sleep(0.5)
  end
  modem.transmit(replyChannel,20,gear.isRunning())


end

bonus : this is the code im using on the pc at the motors.

reddit.com
u/Pillow51 — 4 days ago