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

ComputerCraft simple program not working correctly

Started by GrumpyCrouton, 01 August 2017 - 02:39 AM
GrumpyCrouton #1
Posted 01 August 2017 - 04:39 AM
I'm trying to write some code that has 1 computer send a message to another computer where it will process and turn on the proper light.

The problem is, I don't get any errors, but the computer never receives the message and I am at a loss here.

Here is my code:

Main computer that sends message:

This generates a number between 20-29, I have 5 computers connected so if it generates 20-25 it uses it as the ID and sends the message "back5000" to be handled by the if statement to enable the back redstoneOut for 5 seconds, but if it's above that, it subtracts 5 and uses it as the id and returns "bottom5000" - so basically 1 computer controls 2 outputs (back or bottom)


 
rednet.open("right")
math.randomseed(os.time())

while true do
  os.pullEvent("redstone")
  if rs.getInput("back") then
    lightid = math.random(20,29)

    if(lightid < 26) then
      idfinal = lightid
      output = "back5000"
    else
      idfinal = lightid - 5
      output = "bottom5000"
    end

    print("Output for ID: "..idfinal..") "..output)
    rednet.send(idfinal, output)
  end
end

Recieving computers:

function on(direction)
  rs.setOutput(direction, true)
end

function off(direction)
  rs.setOutput(direction, false)
end

rednet.open("top")

while true do
  print("Waiting for input...")
  id, message = rednet.receive()

  if message == "back5000" then
    on("back")
    os.sleep(5000)
    off("back")
  elseif message == "bottom5000" then
on("bottom")
    os.sleep(5000)
    off("bottom")
  else
on(message)
os.sleep(.50)
off(message)
  end

end

The first computer outputs correctly, but I am unsure if it's sending the message.

The receiving computers don't receive anything, they just say "Waiting on input…"

Could someone help me figure out why this isn't working?
Edited on 01 August 2017 - 09:24 PM
Dave-ee Jones #2
Posted 01 August 2017 - 06:42 AM
The second computer never opens it's modem.

rednet.open(<side>)


Never mind.
Check to make sure the modems are on the right side. In the meantime I'll keep looking at it.

EDIT: Os.wait() doesn't exist. Use sleep(seconds). E.g.

sleep(5) -- waits 5 seconds
It does take decimals as well.

sleep(0.5)

os.clock() doesn't exist (as far as I know), try os.time().

Also, when you call "break" it exits out of the "while loop", meaning that the program stops. I'm not sure if this is what you want but if you want the loop to keep going then don't use "break".

Another thing, lower down in the script you do the "on" "off" thing again, using the message as a parameter. Think about it. If you send a message to the receiver that looks like this:

"back5000"
It's going to try and do a "rs.setOutput("back5000")" which is going to break it. Don't do that :P/>

Having ~10 computers for this seems a bit overkill..Just use 2-3 with lots of redstone coming out of each? :P/>
Edited on 01 August 2017 - 04:55 AM
Bomb Bloke #3
Posted 01 August 2017 - 06:42 AM
Code looks ok to me - how are these computers "connected"? Modems have limited range, varying according to their type and, in the case of wireless modems, world height plus weather conditions are additional factors.

Can you transmit between these systems via the Lua prompt?
Dave-ee Jones #4
Posted 01 August 2017 - 06:57 AM
Code looks ok to me - how are these computers "connected"? Modems have limited range, varying according to their type and, in the case of wireless modems, world height plus weather conditions are additional factors.

Can you transmit between these systems via the Lua prompt?
WHUT?
How did you miss every line in there? lol.
Bomb Bloke #5
Posted 01 August 2017 - 07:14 AM
Sure there're plenty of other issues in the code, but they're not related to the one discussed here.
KingofGamesYami #6
Posted 01 August 2017 - 01:30 PM
os.clock() doesn't exist (as far as I know), try os.time().

It exists.
GrumpyCrouton #7
Posted 01 August 2017 - 01:34 PM
The second computer never opens it's modem.

rednet.open(<side>)

Never mind.
Check to make sure the modems are on the right side. In the meantime I'll keep looking at it.
EDIT: Os.wait() doesn't exist. Use sleep(seconds). E.g.

sleep(5) -- waits 5 seconds
It does take decimals as well.

sleep(0.5)
os.clock() doesn't exist (as far as I know), try os.time().
Also, when you call "break" it exits out of the "while loop", meaning that the program stops. I'm not sure if this is what you want but if you want the loop to keep going then don't use "break".
Another thing, lower down in the script you do the "on" "off" thing again, using the message as a parameter. Think about it. If you send a message to the receiver that looks like this:

"back5000"
It's going to try and do a "rs.setOutput("back5000")" which is going to break it. Don't do that :P/>/>/>
Having ~10 computers for this seems a bit overkill..Just use 2-3 with lots of redstone coming out of each? :P/>/>/>

Thanks for the advice. I have 5 receivers and 1 sender, this powers 10 redstone lamps for a game, I don't think I can compact it anymore, nor do I really wish to or think I need to.

I will look into these issues that you posted about, most of them happened during troubleshooting at 11pm ^.^ but I don't think these are related to the problem.

Code looks ok to me - how are these computers "connected"? Modems have limited range, varying according to their type and, in the case of wireless modems, world height plus weather conditions are additional factors.
Can you transmit between these systems via the Lua prompt?

I'm using wired modem and I also have a disk drive connected at the very end via a modem and I can interact with it fine with my sending computer, so I don't think it's an issue with distance. Everything is within about 10 blocks of each other
GrumpyCrouton #8
Posted 01 August 2017 - 11:24 PM
The second computer never opens it's modem.

rednet.open(<side>)


Never mind.
Check to make sure the modems are on the right side. In the meantime I'll keep looking at it.

EDIT: Os.wait() doesn't exist. Use sleep(seconds). E.g.

sleep(5) -- waits 5 seconds
It does take decimals as well.

sleep(0.5)

os.clock() doesn't exist (as far as I know), try os.time().

Also, when you call "break" it exits out of the "while loop", meaning that the program stops. I'm not sure if this is what you want but if you want the loop to keep going then don't use "break".

Another thing, lower down in the script you do the "on" "off" thing again, using the message as a parameter. Think about it. If you send a message to the receiver that looks like this:

"back5000"
It's going to try and do a "rs.setOutput("back5000")" which is going to break it. Don't do that :P/>

Having ~10 computers for this seems a bit overkill..Just use 2-3 with lots of redstone coming out of each? :P/>

For the on() and off() functions I'm passing "back" or "bottom, I'm not passing the message variable to them. And I did mean to use os.time, that was my bad. And I will fix the os.sleep() thing.

I fixed a few things in my code and I will update my post, but the issue is still not fixed.

Sure there're plenty of other issues in the code, but they're not related to the one discussed here.

I'm pretty new to ComputerCraft and LUA in general. I am a PHP developer though so I know most concepts of how things work/should work. I'm still having this same issue though.
Dave-ee Jones #9
Posted 02 August 2017 - 12:44 AM
os.clock() doesn't exist (as far as I know), try os.time().

It exists.
I just called that function in my computer and it says it doesn't exist. What version is that for? A really old one or the newest 1.8?
EDIT: And os.time() works better for what he's using it for anyway..
Edited on 01 August 2017 - 10:44 PM
KingofGamesYami #10
Posted 02 August 2017 - 01:27 AM
What version is that for? A really old one or the newest 1.8?

ComputerCraft 1.2 and higher. source

In fact, os.time() was added in the same version, so every version with os.time has os.clock as well.
Bomb Bloke #11
Posted 02 August 2017 - 01:42 AM
I'm using wired modem and I also have a disk drive connected at the very end via a modem and I can interact with it fine with my sending computer, so I don't think it's an issue with distance. Everything is within about 10 blocks of each other

Again, are you able to send between these systems via the Lua command prompt?

Typing "lua" into the regular shell brings you to this prompt. If you open your modems, and then on one system type:

rednet.receive()

… and on the other system type:

rednet.broadcast("test")

… does the first system output the message?

If so, are you then also able to get a message through that way using rednet.send?
Dave-ee Jones #12
Posted 02 August 2017 - 02:57 AM
What version is that for? A really old one or the newest 1.8?

ComputerCraft 1.2 and higher. source

In fact, os.time() was added in the same version, so every version with os.time has os.clock as well.

Interesting. I must have made a typo then, my bad.
GrumpyCrouton #13
Posted 03 August 2017 - 12:35 AM
I did receive the message through lua prompt.

http://prntscr.com/g3nm68
http://prntscr.com/g3nm1m
http://prntscr.com/g3nmdb
http://prntscr.com/g3nmzx
http://prntscr.com/g3nmoz
Edited on 02 August 2017 - 10:36 PM
Bomb Bloke #14
Posted 03 August 2017 - 02:33 AM
It may be worth double-checking your computer ID numbers. That "703" makes me doubt wonder whether any of them are within the 20-29 range.

Note that rednet doesn't use the numbers you're shown when activating / de-activating wired modems - those labels are purely for use with the peripheral API.
Edited on 03 August 2017 - 12:35 AM
GrumpyCrouton #15
Posted 03 August 2017 - 06:40 PM
It may be worth double-checking your computer ID numbers. That "703" makes me doubt wonder whether any of them are within the 20-29 range.

Note that rednet doesn't use the numbers you're shown when activating / de-activating wired modems - those labels are purely for use with the peripheral API.

That is the problem then. What numbers do they use? How can I check?
Dog #16
Posted 03 August 2017 - 06:49 PM
Type id at each computer to get its assigned number and label information (if the computer is labeled). I also recommend you label your computers (using the label command) so they don't lose their programs if you break them and place them somewhere else (unlabeled computers lose all their programs when they're broken).
Edited on 03 August 2017 - 04:52 PM
Bomb Bloke #17
Posted 04 August 2017 - 02:43 AM
You're better off getting your main controller script to check the IDs for you - this way you don't need to modify the scripts further if you later want to move them onto different computers.

rednet.lookup() is the easy way to do this. You first label each receiver, and add a rednet.host() call to the top of their scripts:

rednet.open("top")
rednet.host("lightControl", os.getComputerLabel())  --# Content of the second parameter doesn't really matter so long as it's unique

while true do
	print("Waiting for input...")
	local id, message = rednet.receive()

	if message == "back5000" then
		rs.setOutput("back", true)
		sleep(5) --# Sleep takes a time in seconds, 5000 = about an hour and twenty minutes
		rs.setOutput("back", false)
	elseif message == "bottom5000" then
		rs.setOutput("bottom", true)
		sleep(5)
		rs.setOutput("bottom", false)
	end
end

Then you rig up your sender along these lines:

sleep(2) --# Give the other computers time to turn on.
rednet.open("right")

--# Gather the ids of all receiving computers into a table.
--# ids[1] = first computer id, ids[2] = second computer id, etc
local ids = {rednet.lookup("lightControl")}

while true do
	if rs.getInput("back") then
		local output = (math.random(2) == 1) and "back5000" or "bottom5000"  --# http://lua-users.org/wiki/TernaryOperator
		local idfinal = ids[math.random(#ids)]  --# Note that we're pre-"randomly"-seeded in ComputerCraft

		print("Output for ID: " .. idfinal .. ", " .. output)
		rednet.send(idfinal, output)
	end
	
	os.pullEvent("redstone")
end
Edited on 04 August 2017 - 12:44 AM