6 posts
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
1281 posts
Posted 20 April 2014 - 08:38 PM
What have you managed to make so far, post your code. And what specifically isn't working?
6 posts
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)
1281 posts
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.
6 posts
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)
1281 posts
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.pullEventRead 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
7083 posts
Location
Tasmania (AU)
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)
6 posts
Posted 22 April 2014 - 02:07 PM
Thanks for helping
it works!
6 posts
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
1281 posts
Posted 22 April 2014 - 02:58 PM
Well yeah, forgot to turn it off in my example :P/>
1281 posts
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.pullEventcomputercraft.info/wiki/index.php?title=Os.startTimerOh 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
8543 posts
Posted 22 April 2014 - 03:49 PM
Threads merged. Please use only one topic for all questions on a given piece of code.