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

Countdown timer

Started by --Kaos--, 02 November 2015 - 01:35 PM
--Kaos-- #1
Posted 02 November 2015 - 02:35 PM
i have searched high and low for a useable script that works and because i struggle to understand LUA i cant write my own, so can i have some guidance PLEASE!

i would like a script for the following


A countdown timer thats displayed to a monitor ( to the left )
When the timer hits 0 then program stops, then the redstone signal is switched off ( default redstone should be on)



its so i can control my combustion engines, run them a variable amount of minutes and once the timer is up , the redstone signal is switched off and then the entire machine stops

i find it so difficult , ive searched google and it completely baffles me beyond belief

can anyone be amazing and show me your expert skills and write me some code ,
Lyqyd #2
Posted 02 November 2015 - 03:56 PM
Moved to General.
Creator #3
Posted 02 November 2015 - 04:52 PM
This is some commented code I wrote for you. Hope it helps!


mon = peripheral.wrap("left") -- I get a handle for the monitor
rs.setOutput(side,true) --put the redstone on
for i=10,0,-1 do -- we start the countdown loop
   mon.clear()-- clear the monitor
   mon.write(i) -- write the current number
   sleep(1) -- let a second pass
   if i == 0 then --check if we have reached 0
	 rs.setOutput(side,false) -- make it off
   end --close the if thingy :P/>/>
end --we close the loop
Edited on 02 November 2015 - 03:52 PM
Reddy360 #4
Posted 02 November 2015 - 05:57 PM
Thought I'd improve on Creator's example to make it easy for you to modify


local timerLength = 10 --Time in seconds
local monitorLocation = "left" --The monitor location, it can be monitor_XXX or the side the monitor is on
local redstoneSide = "right" --What side of the computer the redstone is on


local monitor = peripheral.wrap(monitorLocation) -- I get a handle for the monitor
rs.setOutput(redstoneSide, true) --put the redstone on
for i=timerLength, 0, -1 do -- we start the countdown loop
   mon.clear()-- clear the monitor
   mon.write(i) -- write the current number
   sleep(1) -- let a second pass
   if i == 0 then --check if we have reached 0
	 rs.setOutput(redstoneSide, false) -- make it off
   end --close the if
end --we close the loop
Edited on 02 November 2015 - 04:58 PM
Creator #5
Posted 02 November 2015 - 06:07 PM
Thought I'd improve on Creator's example to make it easy for you to modify


local timerLength = 10 --Time in seconds
local monitorLocation = "left" --The monitor location, it can be monitor_XXX or the side the monitor is on
local redstoneSide = "right" --What side of the computer the redstone is on


local monitor = peripheral.wrap(monitorLocation) -- I get a handle for the monitor
rs.setOutput(redstoneSide, true) --put the redstone on
for i=timerLength, 0, -1 do -- we start the countdown loop
   mon.clear()-- clear the monitor
   mon.write(i) -- write the current number
   sleep(1) -- let a second pass
   if i == 0 then --check if we have reached 0
	 rs.setOutput(redstoneSide, false) -- make it off
   end --close the if
end --we close the loop

Oh my god! I write the whole code, you move some variables around and get the +1. Totally acceptable. Else, NICE!
KingofGamesYami #6
Posted 02 November 2015 - 06:08 PM
I think I'll improve it further, and move the if statement out of the loop.

local side = "right"
local mon = peripheral.wrap( "left" )
rs.setOutput( side, true )
for i=10, 0, -1 do
  mon.clear()
  mon.setCursorPos( 1, 1 )
  mon.write( i )
  sleep( 1 )
end
rs.setOutput( side, false )
Creator #7
Posted 02 November 2015 - 06:16 PM
I think I'll improve it further, and move the if statement out of the loop.

local side = "right"
local mon = peripheral.wrap( "left" )
rs.setOutput( side, true )
for i=10, 0, -1 do
  mon.clear()
  mon.setCursorPos( 1, 1 )
  mon.write( i )
  sleep( 1 )
end
rs.setOutput( side, false )

Your improvement is worthy. No, seriously, I don't know why I didn't think of that.
--Kaos-- #8
Posted 03 November 2015 - 03:00 PM
Omg wow thank you so much the response is awsome

Some unbelievable stuff. …yoy guys just wow me haha

I had a thought but I don't no if I'm being too adventurous. … could this work by a floppy disk inserted…the program runs. And then the floppy pops out… every time it's inserted the programs starts

Would this also work for minutes and seconds ?
Creator #9
Posted 03 November 2015 - 04:43 PM
You're welcome
--Kaos-- #10
Posted 03 November 2015 - 05:08 PM
I had a thought but I don't no if I'm being too adventurous. … could this work by a floppy disk inserted…the program runs. And then the floppy pops out… every time it's inserted the programs starts


Creator ,is this plausable?

ive also been doing some "working out" about the above code,

i apologise for my ignorance if im wrong , however from what i BELIEVE to be correct is this


local timerLength = 10 –Time in seconds
local monitorLocation = "left" –The monitor location, it can be monitor_XXX or the side the monitor is on
local redstoneSide = "right" –What side of the computer the redstone is on

mon.clear()
mon.setCursorPos( 1, 1 )
mon.write( i )

monitor clear -
setcursorpos is the position of where the text start
mon.write .- write the value of i

im not sure what local is , however, the timerlength , monitorlocation and redtstone are variables declared later in the code?

rs.setOutput - side of redstone output ( self explanatory )


for i=10,0,-1 do

i is a variable, a thing. or and object that is decalred as a value , the 10 is the value of i , 0 is lowest the value of i can go to , and the -1 is the value to subtract ??

DO - is the the action,
Creator #11
Posted 03 November 2015 - 07:48 PM
Yes it is. When a floppy disk is inserted, a disk event is queued. You can use it to determine a floppy disk was inserted and start the program.

Yami's code is the best one. Also, the local keyword serves to declare variables as local. This makes their access faster. Don't know the specifics behind it, but it is faster, and can't be modified outside the function.
--Kaos-- #12
Posted 03 November 2015 - 07:48 PM
Yes it is. When a floppy disk is inserted, a disk event is queued. You can use it to determine a floppy disk was inserted and start the program.

i can always break things down logically and say

the computer needs to continously check for the floppy to be inserted and if true then run the command
the problem is the writing of the script
Creator #13
Posted 03 November 2015 - 07:51 PM
The code would look like this:


local path = "your/path/here"
while true do
  local event  = {os.pullEvent()} -- if you want to avoid the if, add "disk" as an argument to the function.
  if event[1] == "disk" then
	local finalPath = disk.getMountPath(event[2]).."/"..path - this need to be done since you don't know where the disk will be mounted.
	shell.run(finalPath)
  end
end
Edited on 03 November 2015 - 06:54 PM
--Kaos-- #14
Posted 03 November 2015 - 07:55 PM
so Local path woudl be relevant to the disk to the path of the disk i.e disk/ < prog name >
Creator #15
Posted 03 November 2015 - 08:10 PM
Yes, it would.

Congrats on your first +1. :P/>
--Kaos-- #16
Posted 03 November 2015 - 08:56 PM
woop woop

im learning hahaha, thanks for the +1 too :P/>

lets see how well this goes when i implement this into the code :P/>
Creator #17
Posted 03 November 2015 - 09:11 PM
Just do it.
--Kaos-- #18
Posted 04 November 2015 - 08:56 AM
well i have to say

the coding worked wonders :)/>

thanks again :P/>
Creator #19
Posted 04 November 2015 - 09:39 AM
You're welcome again.
--Kaos-- #20
Posted 06 November 2015 - 08:23 PM
so ive been trying for a couple of days to tweak the disk thing

cant do it

so heres my setup

[==] ( DISK DRIVE)
[\\][***]_____________ (MONITOR)(COMUTER) (REDSTONE)


the startup of the computer is

local path = "disk/EngineProg10"
while true do
local event = {os.pullEvent()} – if you want to avoid the if, add "disk" as an argument to the function.
if event[1] == "disk" then
local finalPath = disk.getMountPath(event[2]).."/"..path - this need to be done since you don't know where the disk will be mounted.
shell.run(finalPath)
end
end

i reboot the computer, but i get the error

BIOS 14 string startup = expected

where am i going wrong :S
Creator #21
Posted 06 November 2015 - 08:33 PM
remove disk from localPath
--Kaos-- #22
Posted 06 November 2015 - 08:35 PM
you are kidding me……………………………… that simple lol

would this be a variable string?

so i could multiple outomces depending on disk
Creator #23
Posted 06 November 2015 - 08:47 PM
When you have the disk the final path is disk/disk/stuff. Also, could you please put the code in [code.] [/code.] tags (without dots), so I can now whch line is 14.
--Kaos-- #24
Posted 06 November 2015 - 08:49 PM
so does the code have to be the same name on eahc disk

but the code just changes the actual output


yeah sorry will do from now on , :P/>
Creator #25
Posted 06 November 2015 - 08:57 PM
I believe you will be using the same disk, but lets say there already is another disk connected, so the disk with the code will be mounted at disk1/stuff. You know, better safe than sorry.

What is the exact error message because I believe that the message above is not valid. I can tell you what's wrong by looking at the error message.
--Kaos-- #26
Posted 06 November 2015 - 09:17 PM
i actually have amazed my self and fixed it :)/>


i just named the progam default as "Engine" and then all disks have this as the programme name

then the coding has been changed to reflect the time difference

i.e 10 mins

BUT now i broken the F***in monitor

i broke them onitor then its not displaying the time despite being placed in the same place
Creator #27
Posted 06 November 2015 - 10:48 PM
Can you post the code? Without it I can't possibly help you. Even better, post it on pastebin and provide a link.
Bomb Bloke #28
Posted 07 November 2015 - 01:27 AM
BUT now i broken the F***in monitor

i broke them onitor then its not displaying the time despite being placed in the same place

As you wrap the monitor, try doing this:

local mon = peripheral.wrap(monitorLocation)
mon.setTextScale(0.5)
mon.setTextScale(1)

CC 1.74 can be a bit temperamental with monitors, but changing the scale like this seems to kick it back into gear.