Jump to content




simple program help


  • You cannot reply to this topic
34 replies to this topic

#1 HugoCoin

  • Members
  • 18 posts

Posted 14 July 2014 - 04:35 PM

I am new to computercraft and i searched for a long time but i couldn't find out how to make a program that checks for 2 signals (left and right) and then puts out from the back, repeats. i play in a modpack with no color cables. could someone explain how to do that?
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 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 14 July 2014 - 11:48 PM

Is there any particular reason why you want another computer in the middle to send the signal back? I mean.. a piece of redstone can send a "high" signal around a corner.

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 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 July 2014 - 01:46 AM

For double signals:
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 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 08:42 AM

View Posthilburn, on 14 July 2014 - 11:48 PM, said:

-snip-

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.

View PostKingofGamesYami, on 15 July 2014 - 01:46 AM, said:

-snip-

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 ;)

View PostKingofGamesYami, on 15 July 2014 - 01:46 AM, said:

-snip-

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 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 09:05 AM

Both my and KingofGamesYami's code will repeat forever, that is what the
while true do
   ...
end
is 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 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 11:03 AM

thanks, the first code works :D but the sending computer on the second one has an error, i called the program wireles1 the error: wireles1:3: attempt to index ? (a nil value)
does someone know what the error means?

#7 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 12:59 PM

If you could post a screenshot of the exact setup and the code on the second turtle (assuming it's on one of the "checking" turtles) I'd be happy to have a go at debugging it

#8 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 01:16 PM

this are the 3 pictures i could add.
the receiving comp looks like its working

Attached Thumbnails

  • Attached Image: 2014-07-15_15.12.46.png
  • Attached Image: 2014-07-15_15.13.05.png
  • Attached Image: 2014-07-15_15.12.51.png

Edited by HugoCoin, 11 November 2014 - 04:34 PM.


#9 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 01:24 PM

Ah, that would be because Computers cannot do block detects, only Turtles can do that

#10 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 01:36 PM

ohhhh, so i need to repleace the first comp with a turtle? a specific kind?

but the wireles sensor turtle has problems with this code

Edited by HugoCoin, 15 July 2014 - 01:42 PM.


#11 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 01:50 PM

A wireless turtle would be able to do this, but you'll need to change the first line to rednet.open("right") as that's the side the wireless modem is on

#12 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 02:32 PM

yay ;) it works.
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 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 02:58 PM

You can attach a Wired Modem to both the computer and the monitor and connect them with a wired modem.

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 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 03:28 PM

how can you make the text go away after 5 secons but let the prorgam still be checking for redstone?
i am using sleep but then it stops everything

Edited by HugoCoin, 15 July 2014 - 03:31 PM.


#15 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 03:43 PM

Argh, forgot about that, bit hard to explain, so I did a rewrite of my original code to incorporate it

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 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 July 2014 - 03:48 PM

That's getting a bit difficult, you may want to look at the parallel api.

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 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 03:58 PM

That's neat, I haven't done much parallel work in lua but I have a couple of ideas I haven't got around to doing yet where it might be very handy

#18 HugoCoin

  • Members
  • 18 posts

Posted 15 July 2014 - 04:49 PM

as i read it now these 2 do things exactly the same, right?
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 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 July 2014 - 05:05 PM

View PostHugoCoin, on 15 July 2014 - 04:49 PM, said:

as i read it now these 2 do things exactly the same, right?
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?
They do more or less the same thing, not exactly the same.
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 hilburn

  • Members
  • 153 posts
  • LocationLondon, England

Posted 15 July 2014 - 05:14 PM

They don't do things exactly the same, but as far as the input and output is concerned, yes they do

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

View PostKingofGamesYami, on 15 July 2014 - 05:05 PM, said:

I'll test it in an emulator ( gravlann.github.io ).

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