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

[Advanced Computer Timer] Timer with monitor!

Started by plazter, 24 May 2013 - 04:33 PM
plazter #1
Posted 24 May 2013 - 06:33 PM
Hello guys!!

- i'm feeling proud of this code of mine(a timer) that displays on a moniter and is being controlled by a redstone signal :D/>
Code:

-- CC Timer Made by Plazter --
-- 2013 --
-- Download timer with the name "timer" --
-- Coded in Notepad ++ --

local side = "side you wish here"
local args = {...}

-- Do not edit from down here!! --

m = peripheral.wrap(side) --

function timer()
for i = 1,2 do
shell.run("redpulse back 1")
end
end

if args[1] == nil or args[1] == "help" then
print("timer <time>") -- Notice that "timer" is the program so if u call it "lol" it would be lol <time>
return
else

local delay = tonumber(args[1])

while true do
if not rs.getInput("top") then
  term.clear()
  term.setCursorPos(1,1)
  print("Im running timer at: ".. args[1] .." !")
  m.clear()
  m.setCursorPos(1,1)
  m.setTextColor(colors.lime)
  m.write("ON")
  m.setCursorPos(1,2)
  m.write("Timer: ")
  m.setCursorPos(1,3)
  m.write(args[1])
  timer()
  sleep(delay)  -- this is the args[1]
end
if rs.getInput("top") then
  m.clear()
  m.setTextScale(1)
  m.setCursorPos(1,1)
  m.setTextColor(colors.red)
  m.write("OFF")
  m.setCursorPos(1,2)
  m.write("pause")
  term.clear()
  term.setCursorPos(1,1)
  print(" I HAVE PAUSED THE PROGRAM SIR!!")
  shell.run("ls")
  sleep(3)
end
end
end

Pastebin: http://pastebin.com/cwZq9aHt

If you see any mayor fails! please tell me!!

How to use:

When you Download the program it would be "best" to download it with the name "timer" since the help lines types timer, since i have no clue how to make it type the name u downloaded it with, but well!

to activate the timer simply type in: timer <the time you wish it to run at>.
The monitor is standart placed on the left!
tho you can edit the line where it says "local side = "side you wish here"

EXAMPLE:

-Snip-
local side = "left"
local args = {...}
-- Do not edit from down here!! --
m = peripheral.wrap(side) --
-snip-

Another example on how to run it:
"timer 0.2"
This will execute the timer to run at a speed of 0.2 seconds!

Regards Plazter
ArchAngel075 #2
Posted 24 May 2013 - 07:25 PM
For setting running program name :

name = shell.resolveProgram(shell.getRunningProgram())

Should work. Else a lengthier method :

str = string.reverse(shell.getRunningProgram())
s,e string.find(str,"/")
name = string.sub(string.reverse(str),1,e-1)

for automatic side setting :
sides = {"right","bottom","top",left","front","back")

for i = 1,table.maxn(sides) do
if peripheral.isPresent(sides) == true then
if peripheral.getType(sides) == "monitor" then
m = peripheral.wrap(sides)
end
end
end


will only apply to the first side found, so no double monitors but i dont think you worry :)/>
theoriginalbit #3
Posted 24 May 2013 - 09:32 PM
Ok so firstly you need to work on indenting a little better. for example:
for loops

for i = 1, 2 do
  -- code here is indented
end
if statements

if <condition> then
  -- code here is indented
end
while loops

while <condition> do
  -- code here is indented
end
etc…
each time there is another block you much further indent it. example

while <condition> do
  -- new indent level
  if <condition> then
    -- new indent level
    for i = 1, 2 do
      -- new indent level
    end
  end
end

Another improvement that ArchAngel075 commented on is getting the running program name, however they are doing it really weird ways.

local progName = fs.getName(shell.getRunningProgram())
with the above method you can make some modifications to your arg checking and put the name of the program in the usage string

if #args ~= 2 or args[1] == 'help' or not tonumber(args[1]) then
  error(progName.." <time> <side>", 0)
end
this new checking will check that there is 2 arguments, if it is "help" or if it is a number.

now error with a level of 0 will just print the error message in red, but there will be no file or line number.

Now you may also want to make sure its a valid side first so you would use code like this

local function validSide(side)
  for _, s in pairs(rs.getSides()) do
    if s == side then return true end
  end
  return false
end


if #args ~= 2 or args[1] == 'help' or not tonumber(args[1]) or not validSide(args[2]) then
  error(progName.." <time> <side>", 0)
end


The other suggestion that was made was to auto detect the first monitor and display on it. here is some slightly better code.

-- prepare our localised variable
local m
for _, side in pairs(rs.getSides()) do
  if peripheral.getType(side) == "monitor" then
    -- wrap the variable
    m = peripheral.wrap(side)
  end
end

Another thing that you can fix is your if statements, by doing

if not rs.getInput("top") then
  -- code from the statement
else
  -- code from the "if rs.getInput("top") then" statement
end

Where do you use the second argument? Is it meant to be used in the timer function?

there are a few other minor things, but they are all fairly trivial kind of changes.
plazter #4
Posted 25 May 2013 - 07:16 AM
Wow thanks for the response guys!

_, s in pairs

I really dont know what that does could you explain to me please? =)

and i only use 1 argument and that is the time of the timer to run with :P/>