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

help by Counter Program

Started by fussel01, 07 October 2012 - 08:30 AM
fussel01 #1
Posted 07 October 2012 - 10:30 AM
Dear experts.
I have a program that is not written by me.
I use it as Item Counter.
Now I would like to have the following changes in the program.
At the moment I start the program as follows:
Monitor top counter
I would like to save the program as startup and it should still appear on the top screen.
Problem 2:
I would like to have a function in the program that will be deducted for a Redstone signal from behind the computer.
The variable for this I have created above already but I do not know exactly where in the loop the query must be inserted.
And lastly, the problem is that the value should be stored. Because if I restart the server, the value is reset to 0
I know this is a bit much but I would be glad if someone could help me here!
Here is the code:


-- ItemCounter by redled72 5/2012
cdel  = ("right")		 -- Reset Counter (Button, Switch...)
cin   = ("left")		  -- Input Counter (RP-Signal, Redstonepipe...)
cout  = ("back")		  -- Input Counter (RP-Signal, Redstonepipe...)
local s	  = 0
local inp	= false
local z	  = 0
local stack  = 0
local stueck = 0
local count  = 0
function _latch(inp)	  -- Counter
		 if inp == true then
	  count = count + 1
	  while rs.getInput(cin) == inp do
		 sleep(0.05)
	  end
	  end
   if rs.getInput(cdel) == true then
   count = 0
   end
   sleep(0.05)
   return count
end
function _str(z)		  -- Stack/Stueckrechnung
		 sta = math.floor(z / 64)
   stu = math.fmod(z, 64)
   return sta, stu
end
function _ascii()
		  out = string.char(98,121,32,82,101,100,108,101,100,55,50)
	return out
end

while s == 0 do		   -- Loop Hauptschleife  
		 inp = rs.getInput(cin)
		 z = _latch(inp)  
   stack, stueck = _str(z)
   shell.run("clear")
		 print("Samen  Stueckzahl: " ..z.. " ")
   print("Anzahl in Stack   : " ..stack.." & " ..stueck.. " Stueck")
end
print(newline)
print(""CC-ItemCounter" " .._ascii().. "")



Sincerely yours
Fussel01
jag #2
Posted 07 October 2012 - 11:11 AM
Your alignment is a bit weird, could you put this on pastebin?
fussel01 #3
Posted 07 October 2012 - 12:18 PM
Pastbin: http://pastebin.com/1JmcK6Mg
KaoS #4
Posted 07 October 2012 - 01:35 PM
ok, I just rewrote it. seemed simpler (don't like using other people's code lol)


if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
local tSides={input='left';reset='right'}
local bInput=rs.getInput(tSides['input'])
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   bInput=rs.getInput(tSides['input'])
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
  end
end

you can then pull the count as a total number (that's what it is at the moment), if you want a stack counter:


local stacks=math.floor(count/64)
local amount_In_Unclomplete_Stack=count%64

if you want to include the uncomplete stack in the stack count then:


local stacks=math.floor(count/64)+((count%64==0 and 1) or 0)
local amount_In_Unclomplete_Stack=count%64
fussel01 #5
Posted 07 October 2012 - 02:17 PM
First of all thanks for your work.
if I am not mistaken I have this loop
in my code with the Counter Function Latch
. replace Then the value is stored as a local file.
The function at the redstone signal from behind one of the worth
deducted is not yet there.
Is that Right ?

excuse me if I do something stupid instead. But it's my first contact with LUA. And I actually need just this one program for inventory control on my server.
fussel01 #6
Posted 07 October 2012 - 05:59 PM
now I've tried to use the tips.
Success is not so good.
Here is the code I've put together.


cdel  = ("right")		 -- Reset Counter (Button, Switch...)
cin   = ("left")		  -- Input Counter (RP-Signal, Redstonepipe...)
cout  = ("back")		  -- Input Counter (RP-Signal, Redstonepipe...)
local s	  = 0
local inp	= false
local z	  = 0
local stack  = 0
local stueck = 0
local count  = 0
if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
local tSides={input='left';reset='right';output='back'}
local bInput=rs.getInput(tSides['input'])
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   bInput=rs.getInput(tSides['input'])
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
  elseif rs.getInput(tSides['back']) then
	count=count-1
	local oFile=io.open('count','w')
	oFile:write(count)
	oFile:close()
  end
function _str(z)		  -- Stack/Stueckrechnung
		 sta = math.floor(z / 64)
   stu = math.fmod(z, 64)
   return sta, stu
end
function _ascii()
		  out = string.char(98,121,32,82,101,100,108,101,100,55,50)
	return out
end

while s == 0 do		   -- Loop Hauptschleife  
		 inp = rs.getInput(cin)
		 z = _latch(inp)  
   stack, stueck = _str(z)
   shell.run("clear")
   print("		Item = XXX")
   print("	   Stueckzahl: " ..z.. " ")
   print("	   Anzahl in Stack")
   print("	  " ..stack.." Stack & " ..stueck.. " Stück")
end
end
print(newline)
print(""CC-ItemCounter" " .._ascii().. "")

http://pastebin.com/DQCguuUK

The problems are now:
First : When the Program starts there is no no monitor output (comes after first Redstone signal)
Second : Negative signal from the backside does not work.

Then I have restarted the Server while the Counter stands by "6" . After the start
again no monitor output. At Redstone Signal left comes the following error message:
: 53: Attempt to call nil
KaoS #7
Posted 08 October 2012 - 07:27 AM
oh right, forgot about the monitor bit. the reason that does not work is because you are not using your variables for the sides, you are using mine. let me explain:

in my code I used

local tSides={input='left';reset='right'}
to set which sides do what, in your code you used

cdel  = ("right")				-- Reset Counter (Button, Switch...)
cin   = ("left")				  -- Input Counter (RP-Signal, Redstonepipe...)
cout  = ("back")				  -- Input Counter (RP-Signal, Redstonepipe...)
so you need to use those variables in your code, not sSides['input'] but rather cin

in order for it to show on the monitor in stead:

term.redirect(peripheral.wrap('top')) --if your monitor is on top of the computer
at the top
fussel01 #8
Posted 08 October 2012 - 11:32 AM
Thank you, here is my new Version:
I adjusted the variables.

http://pastebin.com/pdUGSQr1


local s	  = 0
local inp    = false
local z	  = 0
local stack  = 0
local stueck = 0
local count  = 0
if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
local tSides={input='left';reset='right';output='back'}
local bInput=rs.getInput(tSides['input'])
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   bInput=rs.getInput(tSides['input'])
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
  elseif rs.getInput(tSides['output']) then
    count=count-1
    local oFile=io.open('count','w')
    oFile:write(count)
    oFile:close()
  end
function _str(z)		  -- Stack/Stueckrechnung
		 sta = math.floor(z / 64)
   stu = math.fmod(z, 64)
   return sta, stu
end
function _ascii()
		  out = string.char(98,121,32,82,101,100,108,101,100,55,50)
    return out
end

while s == 0 do		   -- Loop Hauptschleife  
		 z = count
   stack, stueck = _str(z)
		 term.redirect(peripheral.wrap('top'))
   shell.run("clear")
   print("	    Item = XXX")
   print("	   Stueckzahl: " ..z.. " ")
   print("	   Anzahl in Stack")
   print("	  " ..stack.." Stack & " ..stueck.. " Stück")
end
end
print(newline)
print(""CC-ItemCounter" " .._ascii().. "")


Monitor Output on Top : ok
first Display after first redstone signal : no !
in Display after a view Seconds following Error :
bios :82: too long without yielding

Sincerely yours
KaoS #9
Posted 08 October 2012 - 11:47 AM
ok, you need a sleep(0.1) in your loop to let it yield and if it is not printing try without the term.redirect and see if it prints to the terminal
fussel01 #10
Posted 09 October 2012 - 11:13 AM
so far so good,
now i have tried to give the Program a Structure.
But something goes wrong.
This is my Version 2_3


---------------------------------
-- Variablen
----------------------------------
local s	  = 0
local inp    = false
local z	  = 0
local stack  = 0
local stueck = 0
local count  = 0
----------------------------------
-- Variablen end
----------------------------------
--
----------------------------------
-- file read
----------------------------------
if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
----------------------------------
-- File read end
----------------------------------
--
----------------------------------
-- Variablen Input
----------------------------------
local tSides={input='left';reset='right';output='back'}
local bInput=rs.getInput(tSides['input'])
----------------------------------
-- Variablen Input end
----------------------------------
--
----------------------------------
-- Counter Section / File write
----------------------------------
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   bInput=rs.getInput(tSides['input'])
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['output']) then
    count=count-1
    local oFile=io.open('count','w')
    oFile:write(count)
    oFile:close()
  end
---------------------------------------
-- Counter Section / File Write end
---------------------------------------
--
---------------------------------------
--  Stack / Stueck Calc
---------------------------------------
function _str(z)
		 sta = math.floor(z / 64)
   stu = math.fmod(z, 64)
   return sta, stu
end
-----------------------------------------
-- Stack / Stueck Calc end
-----------------------------------------
function _ascii()
		  out = string.char(98,121,32,82,101,100,108,101,100,55,50)
    return out
end
-----------------------------------------
-- Monitor Loop
-----------------------------------------
while s == 0 do
		 z = count
   stack, stueck = _str(z)
   term.redirect(peripheral.wrap('top'))
   shell.run("clear")
   print("	    Item = XXX")
   print("	   Stueckzahl: "..z.." ")
   print("	   Anzahl in Stack")
   print("	  " ..stack.." Stack & " ..stueck.. " Stück")
	   sleep(0.1)
end
-----------------------------------------
--  Monitor Loop end
-----------------------------------------
end
print(newline)
print(""CC-ItemCounter" " .._ascii().. "")

http://pastebin.com/4rhWJ5XV

Now i have no Error Messages, but no count Effect !?!
If i start the Program the Monitor on Top is black (no Output)
if i place a timer at left Side of Computer to simulate a Counter Signal, the Monitor
show the screen. But there is no Counting !!!
No positiv Counting on the left side and no negativ counting on back !?!
Seems to be an Error in my Loops.
KaoS #11
Posted 10 October 2012 - 07:33 AM
you have a while true loop in a while true loop lol… I don't understand why you are using that other one when a display function is so easy. try this


----------------------------------
-- file read
----------------------------------
if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
----------------------------------
-- File read end
----------------------------------
--
----------------------------------
-- Variables and Functions
----------------------------------
local tSides={input='left';reset='right';output='back';monitor='top'}
local bInput=rs.getInput(tSides['input'])
local count  = 0
local function display(count)
  term.clear()
  term.setCursorPos(1,1)
  print('Full Stacks: '..math.floor(count/64))
  print('Amount Remaining: '..count%64)
end
----------------------------------
-- Variablen Input end
----------------------------------
--
----------------------------------
-- Counter Section / File write
----------------------------------
term.redirect(tSides['monitor'])
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   bInput=rs.getInput(tSides['input'])
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['output']) then
   count=count-1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  end
  display(count)
end
---------------------------------------
-- Counter Section / File Write end
---------------------------------------

EDIT: Pastebin: http://pastebin.com/WtSzvBwf
fussel01 #12
Posted 10 October 2012 - 09:12 AM
After I have adapted the term.redirect line monitor output works on the top screen. But the Count function does not work. The first signal is then counted no more.
All other Signals (right = Reset) (back = reduce).
Your Code is much better than my try. But i am learning to prgramming Lua only since 5 Days.
PS.
The Screen keeps black on startup the Program. After the first Redstone Signal the Monitor works.
KaoS #13
Posted 10 October 2012 - 09:44 AM
lol. stupid stupid me :P/>/> I made a textbook mistake. try this


----------------------------------
-- file read
----------------------------------
if fs.exists('count') then
  local oFile=io.open('count','r')
  count=tonumber(oFile:read())
  oFile:close()
else
  local count=0
end
----------------------------------
-- File read end
----------------------------------
--
----------------------------------
-- Variables and Functions
----------------------------------
local tSides={input='left';reset='right';output='back';monitor='top'}
local bInput=rs.getInput(tSides['input'])
local bInput2=rs.getInput(tSides['output'])
local count  = 0
local function display(count)
  term.clear()
  term.setCursorPos(1,1)
  print('Full Stacks: '..math.floor(count/64))
  print('Amount Remaining: '..count%64)
end
----------------------------------
-- Variablen Input end
----------------------------------
--
----------------------------------
-- Counter Section / File write
----------------------------------
term.redirect(tSides['monitor'])
while true do
  os.pullEvent('redstone')
  if rs.getInput(tSides['input']) and rs.getInput(tSides['input'])~=bInput then
   count=count+1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['reset']) then
   fs.delete('count')
   count=0
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  elseif rs.getInput(tSides['output']) and rs.getInput(tSides['output'])~=bInput2 then
   count=count-1
   local oFile=io.open('count','w')
   oFile:write(count)
   oFile:close()
  end
  bInput=rs.getInput(tSides['input'])
  bInput2=rs.getInput(tSides['output'])
  display(count)
end
---------------------------------------
-- Counter Section / File Write end
---------------------------------------
fussel01 #14
Posted 10 October 2012 - 09:51 AM
Ok. Now the counter functions are clear. Is there a way to make the screen appear direct on startup the Program ?
KaoS #15
Posted 10 October 2012 - 09:58 AM
sure. just add display() before the os.pullEvent('redstone') instead of at the end of the loop
fussel01 #16
Posted 10 October 2012 - 05:31 PM
ok. now it seems everything is ok.
Oh no, another Problem.
If i restart the server ther is a "0" on the Screen.
the File "count" contains the right Number but on the display is a "0"
Here is the actual Code :

http://pastebin.com/edan5jqr

I start the Program with a shell.run in startup.


Stop i found the Problem by myself. in the top section i read out the count File and store it in the variable count, and in
the Variablen und Functions Section i set count on "0". :P/>/> ;)/>/>
Oh my God !!!!
KaoS #17
Posted 11 October 2012 - 06:19 AM
right, only noticed that now. is it working?
fussel01 #18
Posted 11 October 2012 - 04:40 PM
yes now it works great.
thank you very much for your help !

fussel01