Jump to content




wireless clock start.

wireless api computer

19 replies to this topic

#1 The_Locust911

  • Members
  • 11 posts

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


#2 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

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.

#3 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

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.

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 29 November 2012 - 01:11 AM

View PostOrwell, on 28 November 2012 - 01:40 PM, said:

Second, you have 'rednet.open("right")' on Computer 2, but not on Computer 1.

He does, in the 'if' statement.

#5 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 29 November 2012 - 02:37 AM

View PostremiX, on 29 November 2012 - 01:11 AM, said:

View PostOrwell, on 28 November 2012 - 01:40 PM, said:

Second, you have 'rednet.open("right")' on Computer 2, but not on Computer 1.

He does, in the 'if' statement.
You, sir, are correct. <_<

#6 The_Locust911

  • Members
  • 11 posts

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.

#7 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 29 November 2012 - 09:15 AM

You always listen for every id. Afterwards you can filter on the id's.

#8 SuPeRMiNoR2

  • Members
  • 8 posts

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


#9 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 30 November 2012 - 03:49 AM

View PostSuPeRMiNoR2, on 30 November 2012 - 03:48 AM, said:

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"


#10 SuPeRMiNoR2

  • Members
  • 8 posts

Posted 30 November 2012 - 03:58 AM

View PostOrwell, on 30 November 2012 - 03:49 AM, said:

View PostSuPeRMiNoR2, on 30 November 2012 - 03:48 AM, said:

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...

#11 ChunLing

  • Members
  • 2,027 posts

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.

#12 The_Locust911

  • Members
  • 11 posts

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


#13 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 02 December 2012 - 12:43 AM

View PostThe_Locust911, on 29 November 2012 - 08:44 AM, said:

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


#14 The_Locust911

  • Members
  • 11 posts

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.

#15 ChunLing

  • Members
  • 2,027 posts

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 by ChunLing, 03 December 2012 - 09:54 AM.


#16 The_Locust911

  • Members
  • 11 posts

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.

#17 ChunLing

  • Members
  • 2,027 posts

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.

#18 The_Locust911

  • Members
  • 11 posts

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?

#19 ChunLing

  • Members
  • 2,027 posts

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.

#20 The_Locust911

  • Members
  • 11 posts

Posted 08 December 2012 - 09:08 AM

It worked, ty all very much, especially Ling. I've learned a lot from this thread! :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users