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

Wireless Rednet Counter

Started by erock1357, 16 December 2014 - 02:52 AM
erock1357 #1
Posted 16 December 2014 - 03:52 AM
I am looking to use two computers to work together to crate a counter.

One computer 1, I have a small program that loops every 7 seconds. at the end of that loop, I would like it to send a message to the second computer. something like "+1" or similar.

Then I want the second computer to take that "+1" and count up to 100, and at that point, send out a REDSTONE signal on one side.

Basically, I want a counter on the second computer to run every 700 seconds, but don't want the counter to get reset when the server gets reset, which is what would happen if I just add a sleep(700) command to the original program. I also would like this other program to run only once for every 100 operations of the other program.

Hopefully this makes sense, as I have no idea where to start with rednet programming.

Thanks in advance :)/>
KingofGamesYami #2
Posted 16 December 2014 - 04:33 AM
You can do this with one computer: The way you said it, it would still be reset. Instead, lets use files


local num = 0
if fs.exists( ".num" ) then --#check for .num
  local file = fs.open( ".num", "r" ) --#open it
  num = tonumber( file.readAll() ) or 0 --#read it
  file.close() --#close it
end

while true do
  sleep( 1 ) --#wait one second
  num = num + 1 --#add one to our variable
  local file = fs.open( ".num", "w" ) --#open the file
  file.write( num ) --#write to the file
  file.close() --#close the file
  if num >= 700 then --#if we have reached our goal
    --#do redstone stuff
    num = 0 --#reset our variable
  end
end
Bomb Bloke #3
Posted 16 December 2014 - 07:29 AM
Assuming there's a reason two computers have to be involved other than counting (in which case there's absolutely no reason to use more than one), the coding to get them to work with each other would go loosely along these lines:

rednet.open("top")  -- Or whatever side the modem's on.

while true do
  sleep(7)
  rednet.send(<otherComputerID>,"+1")
end

rednet.open("top")  -- Or whatever side the modem's on.

local counter = 0

while true do
  local senderID, message = rednet.receive()

  if senderID == <otherComputerID> and message == "+1" then counter = counter + 1 end

  -- Save the value of "counter" to disk here, if that matters.

  if counter == 100 then
    rs.setOutput("back", true)
    sleep(10)
    rs.setOutput("back", false)
    counter = 0
  end
end

Personally I'd consider rigging things to use one computer, though. There may be possibilities for doing so which you're overlooking, such as remote peripheral access via a network cable, or redstone signal transmissions via a wireless transmitter.