simple program help
#1
Posted 14 July 2014 - 04:35 PM
i also want to make a system where turtles check for blocks above them and if its gone they send a signal to another turtle which then puts out redstone. can someone help me?
#2
Posted 14 July 2014 - 11:48 PM
However if it has to be a computer, try something like this:
while true do
os.pullEvent("redstone")
if rs.getInput("left") or rs.getInput("right") then
rs.setOutput("back",true)
end
--wasnt sure if you wanted it to pulse high or just go high while one of the inputs was high, so if you want a pulse, uncomment the next couple of lines
--sleep(0.5) change this to change the pulse length
--rs.setOutput("back",false)
end
However I really do highly recommend using just basic redstone for something like this
Edited by hilburn, 14 July 2014 - 11:49 PM.
#3
Posted 15 July 2014 - 01:46 AM
while true do --#infinite loop
os.pullEvent( "redstone" ) --#wait for redstone change
if rs.getInput( "left" ) and rs.getInput( "right" ) then --#check the inputs
rs.setOutput( "back", true ) --#toggle redstone
sleep( 0.5 )
rs.setOutput( "back", false )
end
end
For turtle checking:rednet.open( "left" ) --#assuming modem is on the left
while true do --#infinate loop
if not turtle.detectUp() then --#checking for *not* block
rednet.broadcast( "rs", "rs" ) --#send message
else
sleep( 1 ) --#sleep to avoid error too long without yielding.
end
end
--and the receiving...
rednet.open( "left" ) --#assuming modem is on the left of course
while true do
rednet.receive( "rs" ) --#wait for message
rs.setOutput( "back", true ) --#toggle redstone
sleep( 0.5 )
rs.setOutput( "back", false )
end
Don't hesitate to ask any further questions.
#4
Posted 15 July 2014 - 08:42 AM
hilburn, on 14 July 2014 - 11:48 PM, said:
i want to make it so if a block gets broken (explosion) a couple of doors close, thats why.
and there is too much redstone to use more then a couple of 1 blocks there, thats why i want to use turtles/computers.
and is there a way to copy the codes easyly? i know pastebin but i dont know how to use it like this.
KingofGamesYami, on 15 July 2014 - 01:46 AM, said:
how can you make it so that it repeats? i would like it to check every 2 seconds for 2 signals and then put out one from the back (until 1 or 2 signals turn off. i tried adding a repeat to it but it needs a until. can you add a print that counts up too? that would be so i know it works.
thank you fore the codes so far
KingofGamesYami, on 15 July 2014 - 01:46 AM, said:
btw, i want the sleep on 2 seconds becaus of the lag it creates otherwise
Edited by theoriginalbit, 15 July 2014 - 08:46 AM.
#5
Posted 15 July 2014 - 09:05 AM
while true do ... endis for, it loops for as long as true is true
We also both used
os.pullEvent("redstone")
which is a little more advanced than just a sleep loop, basically it "sleeps" the turtle until the redstone inputs change, then it checks whether the redstone inputs are the ones we care about, then either does something about it, or sleeps again. Just trust me when I say this is better for lag than sleep(2).--#i=1 --uncomment for your debug output
while true do --#loop forever
os.pullEvent("redstone") --#waits for the redstone to change
if rs.getInput("left") and rs.getInput("right") then --#if both inputs are high
rs.setOutput("back",true) --#output high at the back
else --#otherwise
rs.setOutput("back",false) --#output low
end
--#print(i)
--#i=i+1
end
You can uncomment the debug lines, but remember it will only actually loop through this if the redstone signals change, so maybe use a lever or a button to test it (you will get 2 loops per on-off as the event triggers on high and on low)
As for getting it into your turtle via pastebin - copy the code onto it and make a paste, you will get a heinous code looking something like "QJju77jw" - copy that
Open up your turtle and type "pastebin get <heinous code> startup" - this will copy it into the startup program which will mean the programs will run automatically if the computers restart (chunks become unloaded, server resets/crashes etc)
If you are playing single player or are a server admin it's a bit easier, go into <your Minecraft folder>\saves\<whichever save this is stored in>\computer\<computer id>\ and then save it there. Again I would recommend saving it as startup (no extension)
Also, fair enough with the explosions, normally I would suggest a Project Red AND gate or similar, but turtles are explosion proof so it does create extra robustness
#6
Posted 15 July 2014 - 11:03 AM
does someone know what the error means?
#7
Posted 15 July 2014 - 12:59 PM
#9
Posted 15 July 2014 - 01:24 PM
#10
Posted 15 July 2014 - 01:36 PM
but the wireles sensor turtle has problems with this code
Edited by HugoCoin, 15 July 2014 - 01:42 PM.
#11
Posted 15 July 2014 - 01:50 PM
#12
Posted 15 July 2014 - 02:32 PM
can you help me out with one more thing?
i want to add a monitor to the program that checks for 2 signals, but the monitor is a couple blocks away, how can i do that? (i modified the code so it prints nothing or 'door open' and then after 5 sec clears the message
#13
Posted 15 July 2014 - 02:58 PM
You then right click the modem attached to the monitor to activate it and you will get something like "Peripheral monitor_0 connected"
Into your code at the top you add in
monitor=peripheral.wrap("monitor_0") --#change this if it is a different name when you activate the modem
monitor.setTextScale(2) --# looks good on a monitor that is 2 wide, scale it back if you have something smaller
--if it's an advanced monitor you can change the background and text colours but you should look that up in the wiki
where you want to print Door Open you should put
monitor.setCursorPosition(1,2) --# change this so it looks central and nice
monitor.write("Door Open")
where you want to clear it use
monitor.clear()
#14
Posted 15 July 2014 - 03:28 PM
i am using sleep but then it stops everything
Edited by HugoCoin, 15 July 2014 - 03:31 PM.
#15
Posted 15 July 2014 - 03:43 PM
monitor=peripheral.wrap("monitor_0")
monitor.setTextScale(2)
while true do
local event = os.pullEvent()
if event == "redstone" then
if rs.getInput("left") and rs.getInput("right") then --#if both inputs are high
rs.setOutput("back",true) --#output high at the back
monitor.setCursorPos(1,2)
monitor.write("Door Open")
os.startTimer(5)
else --#otherwise
rs.setOutput("back",false) --#output low
monitor.clear() --#clears it if the the inputs go low again
end
elseif event=="timer" then
monitor.clear() --#clears it after 5 seconds
end
end
This will set the monitor to display Door Open when both signals go high
It will be cleared after 5 seconds or if both signals are no longer high, and you can just comment out those lines to change it if you don't want them in effect
The reason this works is that in addition to the "redstone" event we were using before, we are now listening for a "timer" event as well, which we set up to occur 5 seconds after the Door Open message appears. It's a little bit more complicated than the previous code as we can't filter for 2 different events (at least not simply)
Edited by hilburn, 15 July 2014 - 03:43 PM.
#16
Posted 15 July 2014 - 03:48 PM
local function pullRedstone()
while true do --#infinite loop
writing = false
os.pullEvent( "redstone" ) --#wait for redstone change
if rs.getInput( "left" ) and rs.getInput( "right" ) then --#check the inputs
os.queueEvent( "write" ) --#create an event
rs.setOutput( "back", true ) --#toggle redstone
sleep( 0.5 )
rs.setOutput( "back", false )
end
end
end
local mon = peripheral.wrap( "monitor_0" ) --#as said before, change this accordingly
mon.setTextScale( 2 ) --#can be anything between 0.5 and 5
local function writeMon()
while true do
os.pullEvent( "write" ) --#pull our custom event
mon.write( "stuff" )
sleep( 5 )
mon.clear()
end
end
parallel.waitForAll( pullRedstone, writeMon )
Ninja'd by hildburn, but I have a different way of doing it.
#17
Posted 15 July 2014 - 03:58 PM
#18
Posted 15 July 2014 - 04:49 PM
but king's code is made in 2 parts using the enter, am i right?
which one should i use?
and, how do you guys make these? out of experience and then test them?
just wondering cause to me this is very complicated
Edited by HugoCoin, 15 July 2014 - 04:50 PM.
#19
Posted 15 July 2014 - 05:05 PM
HugoCoin, on 15 July 2014 - 04:49 PM, said:
but king's code is made in 2 parts using the enter, am i right?
which one should i use?
and, how do you guys make these? out of experience and then test them?
In my version, I create two functions, one of which "calls" the other by sending a custom event, through the parallel api. It's a little hard to explain how this works without going into coroutines which I probably shouldn't have mentioned anyway. The program that sent the event runs in the time the other is sleeping for 5 seconds, satisfying the requirements you wanted.
In his version, he makes use of a timer event which will be fired 5 seconds after it is called, creating a similar thing but without parallel/coroutines/event creation. His version is probably more efficient, whereas mine may be more precise. I can't say for certain though.
How do I make these? Depends on what program you're talking about. This one I didn't even test, and didn't look at the CC wiki either. Longer/more complex programs I'll double check stuff on the wiki, and then post. If it's a really complicated and I haven't wrote anything similar before, I'll test it in an emulator ( gravlann.github.io ).
Edited by KingofGamesYami, 15 July 2014 - 05:11 PM.
#20
Posted 15 July 2014 - 05:14 PM
Neither should induce any lag or have any issues so really which you use is up to your personal preference, result of a coin flip, eenie meenie miney moe, or however else you want to do it.
I can't speak for anyone else, but I've been coding for about 15 years now, although really only as a hobby and my LUA experience is lacking. With practice I've got better at breaking a problem down into sub-problems to the point where the computer can handle them. The next step is to look at and learn the APIs as this tells you what the computer can actually do. These vary between languages, for example I do a lot of stuff in MATLAB and if I wanted to add 1 to every item in an array in that it is very simple: Array+1, in LUA I would have to loop through every item in the array and add 1 to it individually, it's just a different way of getting the same result based on the way the language is set up. However the most important things are your control structures, your while and for loops and if conditionals, you get the hang of those and coding is your bitch (am I allowed to say bitch? whoops I did it again)
As for the code I wrote for you, It's pretty simple, a while true to loop forever, a wait for event, a conditional based on the type of event and then do stuff in different branches of the conditional
KingofGamesYami, on 15 July 2014 - 05:05 PM, said:
Holy Ballsack of Hades, how did I not know about this?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











