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

Helping with Modem

Started by gustavowizard, 05 June 2014 - 02:24 AM
gustavowizard #1
Posted 05 June 2014 - 04:24 AM
hello guys!

im trying to set a simple rednet here but im not experienced enought yet, what im trying to set is this:

-A rednet with 1 host computer displaying a monitor text WHEN it gets a message from another computers, using Wireless Modem, so the host would display something like this:

Computer Redstone Signal
1 YES
2 NO
3 YES
4 YES

in this case only computer ID 2 would being receve redstone signal, can anybody help me code this?

i was wandering if i use the rednet.open() command i still need to call the modem on a peripheral and set a channel for it? i cant even begin to work with modens yet :D/>
Bomb Bloke #2
Posted 05 June 2014 - 04:48 AM
When using the rednet API, you do not need to interact with the modem directly (you don't have to wrap it as a peripheral, nor use the modem's API). The rednet API handles all of that for you, making it somewhat easier to get things up and running at the expense of fine control.

What code do you have thus far?
theoriginalbit #3
Posted 05 June 2014 - 04:49 AM
Firstly this is not what Ask a Pro is for, we do not code things for you, we help people when they have problems with the code they've made, making suggestions for them to try, helping them with problems, or just pointing them in the right direction of the answer.

Now to the question…

No, if you use the Rednet API there is no need to use the Modem API, the Rednet API will open channels for you, it is simply a wrapper for newbies, so use one or the other. Since you're new I suggest using the Rednet API. You'll definitely want to also take a look into the Redstone API so that you can get the status of redstone signals.

EDIT: Ninja'd, nice work Bomb Bloke, didn't even see you come in :P/>
Edited on 05 June 2014 - 02:49 AM
gustavowizard #4
Posted 05 June 2014 - 09:59 PM
This is the code so far, im close to make it works, the problem now is this; when it do not detect redstone on the main room, it does jump to the next check, and check if there is redstone on the next room, however when it does detect the signal on the first room, it do not check for the messages that comes from the second room, i dont have a clue what im doing wrong, i try to use 'while/do' instead of 'if/then' and replace 'else' for other stuff it aint work.
  • term.clear()
  • monitor = peripheral.wrap("top")
  • monitor.setBackgroundColor(colors.black)
  • monitor.clear()
  • monitor.setTextColor(colors.lightBlue)
  • monitor.setCursorPos(3,1)
  • monitor.write("Oxygen Central")
  • monitor.setTextColor(colors.white)
  • monitor.setCursorPos(1,3)
  • monitor.write("Main Control")
  • monitor.setCursorPos(1,4)
  • monitor.write("Oxygen Module")
  • term.setCursorPos(1,1)
  • print("OXYGEN SYSTEMS")
  • while true do
  • redstone = rs.getInput("right")
  • if rs.getInput("right") == true then
  • monitor.setCursorPos(15,3)
  • monitor.setBackgroundColor(colors.lime)
  • monitor.write("ON ")
  • sleep(1)
  • else
  • redstone = rs.getInput("right")
  • if rs.getInput("right") == false then
  • monitor.setCursorPos(15,3)
  • monitor.setBackgroundColor(colors.red)
  • monitor.write("OFF")
  • sleep(1)
  • rednet.open("left")
  • local id, message = rednet.receive()
  • if message == "o1" then
  • monitor.setCursorPos(15,4)
  • monitor.setBackgroundColor(colors.blue)
  • monitor.write("ON ")
  • sleep(1)
  • else
  • if message == "o1n" then
  • monitor.setCursorPos(15,4)
  • monitor.setBackgroundColor(colors.red)
  • monitor.write("OFF")
  • sleep(1)
  • end
  • end
  • end
  • end
  • end

same code on Raw paste data:

term.clear()
monitor = peripheral.wrap("top")
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setTextColor(colors.lightBlue)
monitor.setCursorPos(3,1)
monitor.write("Oxygen Central")
monitor.setTextColor(colors.white)
monitor.setCursorPos(1,3)
monitor.write("Main Control")
monitor.setCursorPos(1,4)
monitor.write("Oxygen Module")
term.setCursorPos(1,1)
print("OXYGEN SYSTEMS")

while true do
redstone = rs.getInput("right")
if rs.getInput("right") == true then
monitor.setCursorPos(15,3)
monitor.setBackgroundColor(colors.lime)
monitor.write("ON ")
sleep(1)
else
redstone = rs.getInput("right")
if rs.getInput("right") == false then
monitor.setCursorPos(15,3)
monitor.setBackgroundColor(colors.red)
monitor.write("OFF")
sleep(1)


rednet.open("left")
local id, message = rednet.receive()
if message == "o1" then
monitor.setCursorPos(15,4)
monitor.setBackgroundColor(colors.blue)
monitor.write("ON ")
sleep(1)
else
if message == "o1n" then
monitor.setCursorPos(15,4)
monitor.setBackgroundColor(colors.red)
monitor.write("OFF")
sleep(1)

end
end
end
end
end

i forgot to mentionate that there is another computer sending those messages, it send message 'o1' when it have a redstone signal and the message 'o1n' when it dont
gustavowizard #5
Posted 06 June 2014 - 01:35 AM
Ok i managed to make the 1st and 2nd room work, but when i try to read the 3rd room, it only show the redstone ON/OFF IF the 2nd Room redstone is OFF, lol anybody can help me figger why?

this is the code now:

term.clear()
rednet.open("left")
monitor = peripheral.wrap("top")
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setTextColor(colors.lightBlue)
monitor.setCursorPos(3,1)
monitor.write("Oxygen Central")
monitor.setTextColor(colors.white)
monitor.setCursorPos(1,3)
monitor.write("Main Control")
monitor.setCursorPos(1,4)
monitor.write("Oxygen Module")
monitor.setCursorPos(1,5)
monitor.write("Right Wing")
term.setCursorPos(1,1)
print("OXYGEN SYSTEMS")

while true do

redstone = rs.getInput("right")
if rs.getInput("right") == true then
monitor.setCursorPos(15,3)
monitor.setBackgroundColor(colors.lime)
monitor.write("ON ")
sleep(1)
end

if rs.getInput("right") == false then
monitor.setCursorPos(15,3)
monitor.setBackgroundColor(colors.red)
monitor.write("OFF")
sleep(1)
end

local id, message = rednet.receive()
if message == "o1" and id == 63 then
monitor.setCursorPos(15,4)
monitor.setBackgroundColor(colors.blue)
monitor.write("ON ")
sleep(1)
end

local id, message = rednet.receive()
if message == "o1n" and id == 63 then
monitor.setCursorPos(15,4)
monitor.setBackgroundColor(colors.red)
monitor.write("OFF")
sleep(1)
end

local id, message = rednet.receive()
if message == "o2" and id == 77 then
monitor.setCursorPos(15,5)
monitor.setBackgroundColor(colors.lime)
monitor.write("ON ")
sleep(1)
end

local id, message = rednet.receive()
if message == "o2n" and id == 77 then
monitor.setCursorPos(15,5)
monitor.setBackgroundColor(colors.orange)
monitor.write("OFF")
sleep(1)
end
end
gustavowizard #6
Posted 06 June 2014 - 02:19 AM
what happens is that the program only check for the 3rd room (last 2 paragraphs) IF the 2nd room signal is OFF, i know there is something to do with the 'end' on the end of each paragraph, but i dont know what code use instead, i want it to continue to check each IF until the end of the 'while true do' line
theoriginalbit #7
Posted 06 June 2014 - 03:42 AM
For the future gustavowizard please surround your code in [code][/code] tags, as you'll see below it makes the code much more readable and with long code please post it to pastebin; it helps us help you quicker. :)/>

I'm not too sure the problem is exactly what you think it is. I've added some comments to your code to explain some areas of improvement.
Your Code

term.clear()
rednet.open("left")
local monitor = peripheral.wrap("top") --# you should localise variables
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setTextColor(colors.lightBlue)
monitor.setCursorPos(3,1)
monitor.write("Oxygen Central")
monitor.setTextColor(colors.white)
monitor.setCursorPos(1,3)
monitor.write("Main Control")
monitor.setCursorPos(1,4)
monitor.write("Oxygen Module")
monitor.setCursorPos(1,5)
monitor.write("Right Wing")
term.setCursorPos(1,1)
print("OXYGEN SYSTEMS")

--# indentation helps with readability
while true do

  redstone = rs.getInput("right") --# this is a useless line, a problem you also have is it overrides the `redstone` API, lucky you use the `rs` alias
  if rs.getInput("right") == true then --# you don't need to have `== true` here, getInput returns true/false
    monitor.setCursorPos(15,3) --# this could be outside the if statements
    monitor.setBackgroundColor(colors.lime)
    monitor.write("ON ")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end

  if rs.getInput("right") == false then --# this could just be an `else` for the above if statement
    monitor.setCursorPos(15,3)
    monitor.setBackgroundColor(colors.red)
    monitor.write("OFF")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end

  local id, message = rednet.receive()
  if message == "o1" and id == 63 then
    monitor.setCursorPos(15,4)
    monitor.setBackgroundColor(colors.blue)
    monitor.write("ON ")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end

  local id, message = rednet.receive()
  if message == "o1n" and id == 63 then
    monitor.setCursorPos(15,4)
    monitor.setBackgroundColor(colors.red)
    monitor.write("OFF")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end

  local id, message = rednet.receive()
  if message == "o2" and id == 77 then
    monitor.setCursorPos(15,5)
    monitor.setBackgroundColor(colors.lime)
    monitor.write("ON ")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end

  local id, message = rednet.receive()
  if message == "o2n" and id == 77 then
    monitor.setCursorPos(15,5)
    monitor.setBackgroundColor(colors.orange)
    monitor.write("OFF")
    sleep(1) --# this is not needed, and means that you'll most likely miss rednet messages
  end
end 
as you may have noticed from the comments there is a chance that you're missing the signal from the 3rd room with the calls to sleep, however this would also be the case for the second room. Other than that the only thing I can think is that the 3rd room's ID is not 77, so perhaps check that.

As an extra learning point, here is a rewrite of your code to show how else it could be done, in a more efficient manner.
Rewrite

--# lets clear the main terminal and print output now, as we'll be redirecting the terminal shortly
term.clear()
term.setCursorPos(1,1)
print("OXYGEN SYSTEMS")

--# lets open the rednet if it's not already open
if not rednet.isOpen("left") then
  rednet.open("left")
end

--# wrap the monitor
local monitor = peripheral.wrap("top")
--# redirect the terminal to the monitor, that means any calls to `term.` now go to the monitor
term.redirect(monitor)
--# now `write` and `print` also work on monitors, meaning we can use things such as `\n`
term.setBackgroundColor(colors.black)
term.clear()
term.setTextColor(colors.lightBlue)
term.setCursorPos(3,1)
write("Oxygen Central")
term.setTextColor(colors.white)
--# each title will be written at the start of a new line because of the `\n`
term.setCursorPos(1,3)
write("Main Control   OFF\nOxygen Module  OFF\nRight Wing     OFF")

--# writing whether the redstone signal is on or off is extremely similar code
--# so lets make it a function so we do less typing. we provide this function
--# the state of the redstone and the y position that it should be drawn
local function drawState(state, yPos)
  term.setCursorPos(15, yPos)
  --# if the redstone signal is on, set the colour to lime, otherwise set it to red
  term.setBackgroundColor(state and colors.lime or colors.red)
  --# if the redstone signal is on, write 'ON ', otherwise write 'OFF'
  write(state and "ON " or "OFF")
end

while true do
  --# wait here until an event occurs on the computer these can be found here
  --# http://computercraft.info/wiki/Os.pullEvent#Event_types
  local event, senderId, message = os.pullEvent()

  --# if the event was a redstone event it means there has been a change in
  --# redstone around this computer
  if event[1] == "redstone" then
    --# is the signal on?
    local on = rs.getInput("right")
    --# update room one's output
    drawState(on, 3)

  --# if the event was a rednet message it means a computer sent out a message
  elseif event[1] == "rednet_message" then
    --# if the message came from the room two computer
    if senderId == 63 then
      --# was the message that it was on?
      local on = message == "o1"
      --# update room two's output
      drawState(on, 4)

    --# if the message came from the room three computer
    elseif senderId == 77 then
      --# was the message that it was on?
      local on = message == "o2"
      --# update room three's output
      drawState(on, 5)
    end
  end
end
gustavowizard #8
Posted 06 June 2014 - 04:20 AM
hey man thanks for helping, the second code you made works but do not show the current state, just OFF on all, im trying to fix the first code, i removed all the sleep and that line you marked, im still having this problem, it seams that when it gets a rednet message from 1 computer he do not get from the other, like, if the 3rd room is ON i get data from the 1st and 3rd room, but is its off i get data from the 2nd room and the same with 2nd room, if its off i get data from 3rd.. etc…
strange :/
theoriginalbit #9
Posted 06 June 2014 - 04:30 AM
Are we able to see the scripts or the code you're running on your other computers?
gustavowizard #10
Posted 06 June 2014 - 05:25 AM
i think its working Ok now, its because the room 2 data seams to take more time to be send, i dont have a clue why lol, im on room 6 now all them get signal back fast on rednet except for room 2, it takes some time :D/> i guess its Ok since its working, im gonna put the final code on pastebin when its done so people can get it, its a pretty neat program to show each module/room of my Space Station has or no oxygen, the oxygen detector send a redstone signal to the computer (in each room) that send message (ON/OFF) to a main host computer on my station control room :D/> (im using galacticraft and IC2 too with a nuclear reactor)
gustavowizard #11
Posted 06 June 2014 - 10:27 AM
thanks a lot for the help guys! :D/>

here the program ready to use:

http://pastebin.com/W45Wedj2
Edited on 06 June 2014 - 08:29 AM