This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Aggermor's profile picture

Dropper X Items; Every Y Seconds.

Started by Aggermor, 12 April 2017 - 06:14 AM
Aggermor #1
Posted 12 April 2017 - 08:18 AM
I'm trying to learn Lua and I feel this is so simple and I'm over complicating it.

This is attempting to use a dropper to drop X amount of items every Y amount of seconds while printing its status in the terminal.
The top is the rudimentary example code that IS working… The code in the –[[ ]]– is NOT working but I would much prefer it did.
I would like the X and Y to be configurable at the top so that I can easily edit the code for my different setups.


while true do
rs.setOutput("back", true) -- dropNum #1
sleep(1)
rs.setOutput("back", false)
sleep(0.5)
rs.setOutput("back", true) -- dropNum #2
sleep(1)
rs.setOutput("back", false)
sleep(360) -- standbyTime
end

--[[
  --CONFIG--
local standbyTime = 5 -- Time to standby until next drop cycle. (in seconds)
local dropNum = 2 -- How many items the dropper will drop after standby.
  ----------
local i
while true do
-- Eject
	term.clear()
setCursorPos(1,1)
	print("Ejecting: ", dropNum, "!")
  i=0
  while i=>dropNum do
	rs.setOutput("back", true)
	 sleep(1)
rs.setOutput("back", false)
  sleep(0.5)
i=i+1
  end

-- Standby
i=0
for i =>standbyTime do
	  term.clear()
   setCursorPos(1,1)
	  print("Standby: ", standbyTime/100,"%")
	sleep(1)
   i=i+1
end
  rs.setOutput("back", false)
  sleep(stanbyTime)
end
--]]

Maybe printing countdown timer in seconds for the standby time would be easier than the % complete.
Edited on 12 April 2017 - 05:50 PM
supernicejohn #2
Posted 12 April 2017 - 10:08 AM
Couple of problems, first:

a=>b
is not valid, I've made this mistake too. You need to change it to

a>=b
, but more likely is that you'd want

a<=b
, or the condition will be false from the start.
Second problem is that the 'for' loop is invalid; A 'for' loop is structured like this:

for [variable] = [starting value] , [maximum value] <, increment value> do
   -- code
end
with the <, increment value> being optional. (also remove '[]<>'s)

And for a percentage,

i (looping variable) / standbyTime*100
will give a percentage.
If you want you could also round that number as to not take up so much space.

Hope this helps! :)/>
Edited on 12 April 2017 - 08:09 AM
Aggermor #3
Posted 12 April 2017 - 12:06 PM
Very helpful! I can't believe how easy you made it supernicejohn. I learned quite a bit to make it shorter as well. I'll hop in game real quick to try out my corrections…


  --CONFIG--
local standbyTime = 5 -- Time to standby until next drop cycle. (in seconds)
local dropNum = 2 -- How many items the dropper will drop after standby.
  ----Agg----
local i
while true do

-- Eject
for i = 1, dropNum, 1 do
term.clear()
term.setCursorPos(1,1)
print("Ejecting: ", i, "/", dropNum, "!")
rs.setOutput("back", true)
sleep(0.5)
rs.setOutput("back", false)
sleep(0.1)
end

-- Standby
rs.setOutput("back", false)
for i = 0, standbyTime, 1 do
term.clear()
term.setCursorPos(1,1)
print("Standby: ", i, "/", standbyTime, " seconds...")
sleep(1)
end
end

Works perfect! (I think) lol maybe off by a second, but who cares.. thanks so much!

SOLVED
Edited on 12 April 2017 - 10:09 AM
Aggermor #4
Posted 12 April 2017 - 12:32 PM
Edited the standby code to print a percentage as well. I learned math.floor()!


-- Standby --
rs.setOutput("back", false)
for i = 0, standbyTime, 1 do
	  term.clear()
   term.setCursorPos(1,1)
	  print("Standby: ", i, "/", standbyTime, " seconds...")
   term.setCursorPos(1,2)
   print ("		 ", math.floor(i/standbyTime*100), "% complete...")
	sleep(1)
end
end

http://puu.sh/vhMia/415b08204d.jpg
EDIT: (old picture, the math is wrong but now fixed)

I drop fuel for my Endoflames by Botania mod. They only accept one fuel at a time and only by items thrown on top of them.
I dispense fuel and wait for it to burn into mana with "standbyTime" (I may use variable fuels)
I have different amounts of flowers requiring more or less fuel to be dispensed at once "dropNum"
Edited on 12 April 2017 - 10:36 AM