Why is my variable nil and does not increment?

Hey guys,

I’m create a grid traveling sequence which would home all axis every time the farmbot changes row.

The script is quite simple, but I can’t figure out why, the variable seed_count does not increment and stays at nil value.

Here’s a snippet of my code

grid_points = variable("Number of points")
tray = variable("Seed Tray")

depart = variable("Starting location")
off_x = tonumber(env("padding_x"))
off_y = tonumber(env("padding_y"))

depart.x = depart.x + off_x
depart.y = depart.y + off_y

cell_depth = 15
vacuum_pump_pin = 9
seed_depth = tonumber(env("planting_depth"))

-- if verify_tool() then
--     send_message("info", "Dismounting tool")
--     dismount_tool()
-- end

-- send_message("info", "Mounting Seeder tool")
-- mount_tool("Seeder")

local grid = grid ({
  grid_points = grid_points,
  start = depart,
  spacing = variable("Spacing")
})

job = "Pick and Plant x" .. grid.total

set_job(job)

local seed_count = 0

grid.each(function(cell)
    
    seed_count = seed_count or 0
    seed_count = seed_count + 1

    -- error here : seed_count is nil. Why
    send_message("Info", "Going for seed : " .. tostring(seed_count))

    -- This part is ignored because seed_count is nil
    if ((seed_count % grid_points.y)== 0) then
        send_message("info", "recalibration, " .. "seed_count:" .. seed_count)
        move_absolute({z = safe_z()})
        find_home("all")
    end

   --- More code 

end)

-- some code

Thank you

grid.each is run in a separate process without access to seed_count. You may try using cell.count instead.

2 Likes

Thanks! Is that a Lua thing or specific to the Farmbot API?

I’ll fork the documentation repo to add this info.