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

[lua] Dispenser Program Only Dispenses One?

Started by SNWLeader, 22 January 2013 - 11:33 AM
SNWLeader #1
Posted 22 January 2013 - 12:33 PM
I am trying to make a dispenser program for foods in a cafeteria.

The program once totalled up and the signal to start the dispensing will only dispense one of each item only if there is any in the que for it.

I cannot get it to dispense the correct amount.

Here is the coding I have.

inside = "back"
outside = "bottom"
p = 0
pstat = 0
b = 0
bstat = 0
c = 0
cstat = 0
term.clear()
term.setCursorPos(1,1)
print("Order Calculations")
print("Porkchops: "..p)
print("Beef: "..B)/>
print("Chicken: "..c)
while true do
	 
	  os.pullEvent("redstone")
	  if rs.testBundledInput(inside, colours.white) then
	  p = p + 1
	  pstat = pstat + 1
	  elseif rs.testBundledInput(inside, colours.orange) then
	  b = b + 1
	  bstat = bstat + 1
	  elseif rs.testBundledInput(inside, colours.magenta) then
	  c = c + 1
	  cstat = cstat + 1
	  elseif rs.testBundledInput(inside, colours.yellow) then
	  p = 0
	  b = 0
	  c = 0
	  elseif rs.testBundledInput(inside, colours.lightBlue) then
	  repeat
	  if pstat > 0 then
	  p = 0
	  for i=1, pstat do
	  rs.setBundledOutput(outside, 1)
	  sleep(1)
	  rs.setBundledOutput(outside, 0)
	  end
	  pstat = 0
	  elseif bstat > 0 then
	  b = 0
	  for i=1, bstat do
	  rs.setBundledOutput(outside, 2)
	  sleep(1)
	  rs.setBundledOutput(outside, 0)
	  end
	  bstat = 0
	  elseif cstat > 0 then
	  c = 0
	  for i=1, cstat do
	  rs.setBundledOutput(outside, 4)
	  sleep(1)
	  rs.setBundledOutput(outside, 0)
	  end
	  cstat = 0
	  elseif cstat == 0 and bstat == 0 and pstat == 0 then
	  print("Finished Order. New Order may be made.")
	  sleep(1)
	  end
	  until pstat == 0 and bstat == 0 and cstat == 0
	  else
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Order Calculations")
	  print("Porkchops: "..p)
	  print("Beef: "..B)/>
	  print("Chicken: "..c)
	  end
end
Willibilly19 #2
Posted 22 January 2013 - 02:02 PM
Hmm, I was just looking over your code and I couldn't figure out why it's not working for you. I did however add it to notepad++ and work out the spacing while I was reading it, so maybe it will help someone more advanced see the issue.



inside = "back"
outside = "bottom"
p = 0
pstat = 0
b = 0
bstat = 0
c = 0
cstat = 0
term.clear()
term.setCursorPos(1,1)
print("Order Calculations")
print("Porkchops: "..p)
print("Beef: "..B)/>
print("Chicken: "..c)


while true do	
  os.pullEvent("redstone")
  if rs.testBundledInput(inside, colours.white) then
	p = p + 1
	pstat = pstat + 1
  elseif rs.testBundledInput(inside, colours.orange) then
	b = b + 1
	bstat = bstat + 1
  elseif rs.testBundledInput(inside, colours.magenta) then
	c = c + 1
	cstat = cstat + 1
  elseif rs.testBundledInput(inside, colours.yellow) then
	p = 0
	b = 0
	c = 0
  elseif rs.testBundledInput(inside, colours.lightBlue) then
	repeat
	  if pstat > 0 then
		p = 0
		for i=1, pstat do
		  rs.setBundledOutput(outside, 1)
		  sleep(1)
		  rs.setBundledOutput(outside, 0)
		end
		pstat = 0
	   elseif bstat > 0 then
		b = 0
		for i=1, bstat do
		  rs.setBundledOutput(outside, 2)
		  sleep(1)
		  rs.setBundledOutput(outside, 0)
		end
		bstat = 0
	  elseif cstat > 0 then
		c = 0
		for i=1, cstat do
		  rs.setBundledOutput(outside, 4)
		  sleep(1)
		  rs.setBundledOutput(outside, 0)
		end
		cstat = 0
	  elseif cstat == 0 and bstat == 0 and pstat == 0 then
		print("Finished Order. New Order may be made.")
		sleep(1)
	  end
	until pstat == 0 and bstat == 0 and cstat == 0
  else
	term.clear()
	term.setCursorPos(1,1)
	print("Order Calculations")
	print("Porkchops: "..p)
	print("Beef: "..B)/>
	print("Chicken: "..c)
  end
end


Also, wasn't sure about this bit. it obviously prints, but the variable should be lowercase (to match your variable above.) What is the /> at the end for?




print("Beef: "..B)/>

Edit* (nevermind…seems the /> is something the forum is adding…don't know why though)
ChunLing #3
Posted 22 January 2013 - 08:02 PM
I'm not sure how your system is set up, but it looks like this is the dispenser control loop:
   	 repeat
            if pstat > 0 then
                p = 0
                for i=1, pstat do
                    rs.setBundledOutput(outside, 1)
                    sleep(1)
                    rs.setBundledOutput(outside, 0)
                end
                pstat = 0
            elseif bstat > 0 then
                b = 0
                for i=1, bstat do
                    rs.setBundledOutput(outside, 2)
                    sleep(1)
                    rs.setBundledOutput(outside, 0)
                end
                bstat = 0
            elseif cstat > 0 then
                c = 0
                for i=1, cstat do
                    rs.setBundledOutput(outside, 4)
                    sleep(1)
                    rs.setBundledOutput(outside, 0)
                end
                cstat = 0
            elseif cstat == 0 and bstat == 0 and pstat == 0 then
                print("Finished Order. New Order may be made.")
                sleep(1)
            end
        until pstat == 0 and bstat == 0 and cstat == 0
If you look at this carefully, when you send a pulse, you hold the pulse for a full second. But when you turn the pulse off, you instantly restart it, because there is no other delay. Try putting a sleep just before the until (perhaps you could move that one after the finished order output down one line).
SNWLeader #4
Posted 22 January 2013 - 11:49 PM
I probably should do that.
SNWLeader #5
Posted 22 January 2013 - 11:56 PM
I'm not sure how your system is set up, but it looks like this is the dispenser control loop:
   	 repeat
			if pstat > 0 then
				p = 0
				for i=1, pstat do
					rs.setBundledOutput(outside, 1)
					sleep(1)
					rs.setBundledOutput(outside, 0)
				end
				pstat = 0
			elseif bstat > 0 then
				b = 0
				for i=1, bstat do
					rs.setBundledOutput(outside, 2)
					sleep(1)
					rs.setBundledOutput(outside, 0)
				end
				bstat = 0
			elseif cstat > 0 then
				c = 0
				for i=1, cstat do
					rs.setBundledOutput(outside, 4)
					sleep(1)
					rs.setBundledOutput(outside, 0)
				end
				cstat = 0
			elseif cstat == 0 and bstat == 0 and pstat == 0 then
				print("Finished Order. New Order may be made.")
				sleep(1)
			end
		until pstat == 0 and bstat == 0 and cstat == 0
If you look at this carefully, when you send a pulse, you hold the pulse for a full second. But when you turn the pulse off, you instantly restart it, because there is no other delay. Try putting a sleep just before the until (perhaps you could move that one after the finished order output down one line).

Yep, that fixed it right up…….. based on what you said it did not have enough time when staying off and flickers right back on again too quickly.
ChunLing #6
Posted 23 January 2013 - 08:01 AM
I'm guessing that you need a pulse to be on/off for at least a game tick (sleep(0.2)) for it to work (more precisely, it registers as on/off at the tick). So you can probably make those on ticks a lot shorter.