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

timer program help

Started by buckChoi, 20 April 2014 - 02:45 AM
buckChoi #1
Posted 20 April 2014 - 04:45 AM
Hi I just started to use computercraft and spend 2 hours learning but I can't get this program to work properly
Here is what I am trying to achieve
1.redstone signal input left true
2.redstone output right will turn off
3.wait 30 seconds
4.redstone output right turns back on
5.repeat this program (so back to #1)
This program will be for shooting range that I am making
CometWolf #2
Posted 20 April 2014 - 08:38 PM
What have you managed to make so far, post your code. And what specifically isn't working?
buckChoi #3
Posted 21 April 2014 - 09:36 AM
Well I have managed to make 30 sec timer without the input and loops
Also when I try to add if rs.getInput ("left" true)~=then it says this line won't work
Also when I try to add while statement it saids nil
(Sorry for the bad English I live in Korea)

This is what I got so far
While rs.getInput ("bottom" , true) do
If rs.getInput("top") then
Rs.setOutput ("left", false)
Sleep (30)
Rs.setOutput ("left", true)
End
When I load this it keeps giving me
End expected(to close while at line 1)
CometWolf #4
Posted 21 April 2014 - 04:13 PM
Makes sense, since while loops need to be closed with an end :P/>
Anyways, getInput dosen't do the comparison for you, it merely returns the redstone state. Which is either true or false, so simply while rs.getInput(side) do would suffice. Aside from your directions being wrong, it looks okay. Although you'll need to add an else statement that makes it sleep if there is no redstone input, otherwise it will crash.
buckChoi #5
Posted 21 April 2014 - 05:00 PM
I see, thanks but how do I make it sleep until the redstone signal is applied
I thought I can only set sleep function like "sleep (seconds)" not sleep (rs signal)
CometWolf #6
Posted 21 April 2014 - 05:44 PM
Well you can actually, though you wouldn't use sleep to do so. You would use os.pullEvent"redstone".
http://computercraft...ki/Os.pullEvent
Read into it if you want, but what i was talking about was a simple sleep. rs.getInput checks the current state of the redstone, not wether it has changed. So it dosen't matter if the computer is sleeping when it changes, provided it's checked afterwards.
Something like

while true do
  if rs.getInput"left" then
	rs.setOutput"right"
	sleep(30)
  else
	sleep(0.1)
  end
end
The sleep function itself does infact use the pullEvent function mentioned above, except it waits for a timer instead. Now the point here is that the pullEvent function yields, meaning it passes of it's control to whatever other programs might be running. This is important, if your program does not yield every once in a while, you will get an error. Ergo, a constant while loop without yielding would crash.
Edited on 21 April 2014 - 03:45 PM
Bomb Bloke #7
Posted 22 April 2014 - 12:31 AM
rs.setOutput("right",true)

Bear in mind that if you want that output to ever turn off, you'll need to occasionally run:

rs.setOutput("right",false)
buckChoi #8
Posted 22 April 2014 - 02:07 PM
Thanks for helping
it works!
buckChoi #9
Posted 22 April 2014 - 02:50 PM
Hi I am still kinda new to lua and I need some help programing a counting machanism
This program will be for my shooting ranfe that I am working on
This is what I want it to do
1.wait until rs signal is applied at the bottom
2.when it does that the program starts
3.when it starts displays 0 on the moniter
4.when it detects rs signal left the value gets added by 10 and it gets displayed on the moniter
5.repeat number 4 until there is no input for 60 seconds and display thx for playing
Sorry for my bad English but I have no clue how to program this so little guideline should be great

Thx
CometWolf #10
Posted 22 April 2014 - 02:58 PM
Well yeah, forgot to turn it off in my example :P/>
CometWolf #11
Posted 22 April 2014 - 03:08 PM
You're probably gonna get told to stick to one thread per program, but anyways…
This is gonna require os.pullEvent like i was talking about earlier. You'll have to start a timer and save it's ID to a variable when you get the bottom input, then use os.pullEvent() without any filter to pull all events, and use if statements to sort out wether the event is a timer or if it's a redstone change. When you get a redstone change, you check if it's on the right, add 10 to your score variable, and then start another timer, saving it's ID into the save variable. If it's the timer that's fired, you end the loop, reset the score and go back to waiting for a bottom input.
http://computercraft.info/wiki/Os.pullEvent
computercraft.info/wiki/index.php?title=Os.startTimer

Oh what the hell, i threw together a quick example :P/>

while true do
  os.pullEvent"redstone"
  if rs.getInput"bottom" then
    local timerID = os.startTimer(60)
    while true do
	  local event, id = os.pullEvent()
	  if event == "redstone" then
	    --redstone stuff
	    timerID = os.startTimer(60) --restart timer
	  elseif event == "timer" and id == timerID then
	    break --60 secs without input, end the loop
	  end
    end
    --program ended, clear your score or whatever here
  end
end
Lyqyd #12
Posted 22 April 2014 - 03:49 PM
Threads merged. Please use only one topic for all questions on a given piece of code.