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

wireless clock start.

Started by The_Locust911, 28 November 2012 - 11:59 AM
The_Locust911 #1
Posted 28 November 2012 - 12:59 PM
Just as a learning experience and a test model I'm trying to get one computer to tell another to start a redstone clock. All I've made so far is crashed computers.

what I have so far is this…
Computer 1:


while true do
local start = "start"
print("type start")
input = read()
if input == start then
       rednet.open("right")
       rednet.broadcast("start")
       print("sent")
       end
end
Computer 2:

rednet.open("right")
message = rednet.receive()
if message == "start" then
       print("starting clock")
       rs.setOutput("back", true)
       sleep(.25)
       rs.setOutput("back", false)
       end
end
Orwell #2
Posted 28 November 2012 - 01:40 PM
It's much easier for us to correct code if you post the errors you get. :)/> But I spotted a few anyways.
First of, you should remove the double 'end' on Computer 2:

rednet.open("right")
message = rednet.receive()
if message == "start" then
	   print("starting clock")
	   rs.setOutput("back", true)
	   sleep(.25)
	   rs.setOutput("back", false)
	   -- removed an 'end' here
end
Second, you have 'rednet.open("right")' on Computer 2, but not on Computer 1.
Dlcruz129 #3
Posted 28 November 2012 - 06:51 PM
You also have
 message = rednet.receive()

Use
 id, msg = rednet.receive() 

Rednet.receive returns the senders id, then the message, then, if you need it, the computer's distance.
remiX #4
Posted 29 November 2012 - 01:11 AM
Second, you have 'rednet.open("right")' on Computer 2, but not on Computer 1.

He does, in the 'if' statement.
Orwell #5
Posted 29 November 2012 - 02:37 AM
Second, you have 'rednet.open("right")' on Computer 2, but not on Computer 1.

He does, in the 'if' statement.
You, sir, are correct. <_</>
The_Locust911 #6
Posted 29 November 2012 - 08:44 AM
Do I have to specify an id for computer 2 to listen for? Or could I have it listen for anything? I'm hoping eventually to have one computer start various different tasks with several other computers around it.

Also, I would have posted errors but the computer doesn't specify any, it just runs the command but the redstone doesn't start.
Orwell #7
Posted 29 November 2012 - 09:15 AM
You always listen for every id. Afterwards you can filter on the id's.
SuPeRMiNoR2 #8
Posted 30 November 2012 - 03:48 AM
EDIT: Ignore this
Also, on computer number one, you have

if input == start then
However, the read() function returns a string so you need quotes around the start.

if input == "start" then
Orwell #9
Posted 30 November 2012 - 03:49 AM
Also, on computer number one, you have

if input == start then
However, the read() function returns a string so you need quotes around the start.

if input == "start" then
What about his second line?

local start = "start"
SuPeRMiNoR2 #10
Posted 30 November 2012 - 03:58 AM
Also, on computer number one, you have

if input == start then
However, the read() function returns a string so you need quotes around the start.

if input == "start" then
What about his second line?

local start = "start"
Oops…
ChunLing #11
Posted 30 November 2012 - 08:58 AM
It's an excusable oversight, that is really not a well written program.

start should be defined as local to the program, not to inside each iteration of an infinite loop. Meaning that "local start = "start" " should be the first line of the program, where any competent programmer would expect to find it.

For now I'd refrain from offering any more fixes until a new version incorporating suggested fixes is tested.
The_Locust911 #12
Posted 01 December 2012 - 07:55 PM
I applied the changes but still the same results, I suspect I mistook Orwell and assumed I didn't have to specify an I.D., do I replace 'id' in
id, message = receive
with the actual ID to look for?
computer 1

local start = "start"
while true do
    print("type start")
    input = read()
    if input == start then
    rednet.open("right")
    rednet.broadcast("start")
    print("sent")
    end
end
computer 2


rednet.open("right")
    id, msg = rednet.receive()
    if message == "start" then
    rs.setOutput("back", true)
    sleep(.25)
    rs.setOutput("back", false)
    print("clock started")
end
Orwell #13
Posted 02 December 2012 - 12:43 AM
Also, I would have posted errors but the computer doesn't specify any, it just runs the command but the redstone doesn't start.
Are you actually placing redstone?
And no, it's rather:

id,message = rednet.receive()
if id == 1 then
  //
elseif id == 2 then
  //
end
The_Locust911 #14
Posted 02 December 2012 - 08:20 AM
BTW thank you everyone for your help, my programs look like crap because I've been learning myself on and off for a couple weeks. Here is my next revision to computer 2's program. (49 is the current ID of computer 1)

rednet.open("right")
    id, msg = rednet.receive()
    if id == 49 and message == "start" then
    "print("received")
    rs.setOuteput("back", true)
    sleep(.25)
    rs.setOutput("back", false)
end
It seems as though computer two never even prints "received" and simply ends the program after supposedly receiving the signal.
ChunLing #15
Posted 03 December 2012 - 02:59 AM
Do you have a clean rednet environment? Is there any chance that other rednet sources are being received by the second computer? Because it doesn't loop until it gets the right message, if it gets one and only one rednet message and if it's the wrong message then that's it, program over.

You can test this by revising it as so:
rednet.open("right")
id, msg = rednet.receive()
if id == 49 and msg == "start" then
	print("received")
	rs.setOutput("back", true)
	sleep(.25)
	rs.setOutput("back", false)
else print("invalid message: "..msg)
end

If you want to loop it to keep checking messages until it gets the right one, you could use:
rednet.open("right")
id, msg = rednet.receive()
while id ~= 49 or msg ~= "start" do
	print("invalid message: "..msg)
	id, msg = rednet.receive()
end
print("received")
rs.setOutput("back", true)
sleep(.25)
rs.setOutput("back", false)

Code fixes
Edited on 03 December 2012 - 08:54 AM
The_Locust911 #16
Posted 03 December 2012 - 08:38 AM
I used your first suggestion and I got "attempt to concatenate string and nil". Also, yes the environment is clean, I have it set up on a platform in the sky away from the other projects.
ChunLing #17
Posted 03 December 2012 - 09:53 AM
Sorry, that should have been…Ok, I'll just fix the post. I used message rather than msg because I've been looking at so many other similar problems.

Sorry to say this, but if you want a clean environment you should be in a lake of lava deep underground in a distant ocean biome, because rednet range increases with altitude. Max range at max altitude is over 380 meters, so you're just making yourself more likely to be receiving everyone else's broadcasts and GPS pings.

But it's also possible that the reason that your code wasn't working was because you also were trying to match message == "start", which could never happen anyway because msg was the variable that contained the message.
The_Locust911 #18
Posted 04 December 2012 - 07:53 PM
I'm not online so other people's signals shouldn't be interfering, on the other hand. could you elaborate about matching message == "start"? or rather not doing that? What kind of signal should I be using?
ChunLing #19
Posted 04 December 2012 - 08:57 PM
I revised the broken code I posted after you mentioned the error, and on closer examination I realized that the rednet message was stored in a variable named msg, but the comparisons were checking to see if a variable called message was equal to start (which it never was, because message was never assigned a value and was thus nil).

The revised code should work now.
The_Locust911 #20
Posted 08 December 2012 - 09:08 AM
It worked, ty all very much, especially Ling. I've learned a lot from this thread! :D/>