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

Rednet miner

Started by jarifle, 13 May 2015 - 12:32 PM
jarifle #1
Posted 13 May 2015 - 02:32 PM
Hello,

Quick thing i wanted to make with the compact computer.
The problem is that the program does not continue after i have given an input it just stays there but does the rednet sending thing just fine.


rednet.open("back")
term.clear()
term.setCursorPos(1,1)print("Welcome to the miner!")
print("How far do you want to mine?")
m = read()
rednet.send(7, m)print("Mining".. m .."Blocks")
sleep(2)
os.reboot()

Thanks for the help.
KingofGamesYami #2
Posted 13 May 2015 - 02:37 PM
Could you post the receiving script as well? There is nothing wrong with your current script, other than the lack of a loop and the os.reboot (really shouldn't be necessary).
Creator #3
Posted 13 May 2015 - 02:39 PM
1. You mean PDA?

2.

while true do
rednet.open("back")
term.clear()
term.setCursorPos(1,1)
print("Welcome to the miner!")
print("How far do you want to mine?")
m = tonumber(read()) -- added tonumber since you want the output to be a number
rednet.send(7, m)
print("Mining".. m .."Blocks")
sleep(2)
end

Added a tonumber. Else your code seems fine.

ninja'd and Yami, how do you manage to be so fast?

Added a loop
Edited on 13 May 2015 - 12:39 PM
flaghacker #4
Posted 13 May 2015 - 03:03 PM
1. You mean PDA?

2.

while true do
rednet.open("back")
term.clear()
term.setCursorPos(1,1)
print("Welcome to the miner!")
print("How far do you want to mine?")
m = tonumber(read()) -- added tonumber since you want the output to be a number
rednet.send(7, m)
print("Mining".. m .."Blocks")
sleep(2)
end

Added a tonumber. Else your code seems fine.

ninja'd and Yami, how do you manage to be so fast?

Added a loop

I would move the rednet.open out of the loop.
jarifle #5
Posted 13 May 2015 - 03:41 PM
and the loop isnt needed its not supposed to run all the time.

This is the recieving code.


rednet.open("top")
rs.setOutput("right", false)
rs.setOutput("left", false)
i = 0
id, message = rednet.receive()
m = tonumber ( message )
while true do
    if m then
	  repeat
	    print(m)
	    print(i)
	    rs.setOutput("right", true)
	    sleep(1)
	    rs.setOutput("right", false)
	    sleep(3)
	    rs.setOutput("left", true)
	    sleep(10)
	    rs.setOutput("left", false)
	    i = i + 1
	  until i == m
	 os.reboot()
    end
end

Thanks for the help though.
KingofGamesYami #6
Posted 13 May 2015 - 03:57 PM
Well, it looks like your receiving program is going to run until you get a rednet message - not necessarily the message you are sending, if other computers are sending information over rednet. You may want to add some checks to ensure you are getting the data you want.

Also, take a look at for loops. This can simplify your receiving program somewhat.


for i = 0, m do
 ...
end
jarifle #7
Posted 13 May 2015 - 06:26 PM
So how would i go about fixing it?
How would you do it?
This has to been able to run by 2 computers if possible.
HPWebcamAble #8
Posted 14 May 2015 - 01:43 AM
So how would i go about fixing it?
How would you do it?
This has to been able to run by 2 computers if possible.

Well, you can use my protocol API to make sure each computer only receives what it is supposed to

The basic idea of the API is to ensure that your program receives messages from itself (on another computer)
You give the API a name, and it will only listen for messages from computers using the same name

You could achieve the same effect by sending a table: (The rednet API supports that right?)

toSend = { "unique name" , m }

Then the receiving computer listens for messages, and only uses the 'm' value if the first value in the table is the unique name



Although, if you are using rednet, your receiving computer will only get messages that are sent to its ID (Unless there are other programs that aren't using rednet, but just the Modem API)
Edited on 13 May 2015 - 11:45 PM
Bomb Bloke #9
Posted 14 May 2015 - 01:46 AM
By the way, what was the actual problem?

Is it that the program up the top of the thread "doesn't continue", as stated, or is it the receiving computer that's not doing its job?
jarifle #10
Posted 15 May 2015 - 08:52 PM
Its oke now i forgot how i fixed it but i did.

But now i have another rednet problem
How would i go about making a computer recieve a message and then respond with a variable?
Basicly i want to be able to click a button to get the info of my reactor (not in real time just once) for about ten seconds.
But the monitor wont clear and then just reboots (the message reactor gets to the receiving computer and it prints test like expected).

these are the codes.

Sending.

rednet.open("back")
term.clear()
term.setCursorPos(1,1)
print("Reactor status")
rednet.send(8, "reactor")
id, message = rednet.receive()
if message == a then
  print(a)
  sleep(3)
end
os.reboot()

Receiving.

mon = peripheral.wrap("monitor_0")
r = peripheral.wrap("BigReactors-Reactor_0")
cell = peripheral.wrap("tile_thermalexpansion_cell_resonant_name_0")

function active()
  if r.getActive() == true then
    a = "On"
    ca = colors.lime
  else
    a = "Off"
    ca = colors.red
  end
end
function lastTick()
  if r.getEnergyProducedLastTick() == 0 then
    b = "0"
    ct = colors.red
  else
    b = math.floor(r.getEnergyProducedLastTick())
    ct = colors.lime
  end
end
function cellEnergy()
  energy = math.floor(math.floor(cell.getEnergyStored())/math.floor(cell.getMaxEnergyStored())*100)
  if energy >= 60 then
    ce = colors.lime
  elseif energy <= 20 then
    ce = colors.red
  else
    ce = colors.orange
  end
end
function main()
  mon.clear()
  mon.setCursorPos(3,1)
  mon.setTextScale(4)
  mon.setTextColor(colors.yellow)
  mon.write(" Reactor")
  active()
  mon.setCursorPos(1,2)
  mon.setTextColor(ca)
  mon.write(a)
  lastTick()
  mon.setCursorPos(1,3)
  mon.setTextColor(ct)
  mon.write(B)/>
  if r.getEnergyStored() <= 0 then
    r.setActive(true)
  end
  if r.getEnergyStored() >= 5000000 then
    r.setActive(false)
  end
 
  mon.setCursorPos(5,4)
  mon.setTextColor(colors.cyan)
  mon.write(" Cell")
 
  cellEnergy()
  mon.setCursorPos(1,5)
  mon.setTextColor(ce)
  mon.write(energy)
  mon.write(" %")
  sleep(0.5)
end
function test()
  rednet.open("right")
  id, message = rednet.receive(2)
  if message == "reactor" then
    print("test")
    rednet.send(6, a)
  end
  sleep(0.5)
end
while true do
  parallel.waitForAll(test, main)
end   

Thank you guys.
HPWebcamAble #11
Posted 16 May 2015 - 02:44 AM
Sending.

rednet.open("back")
term.clear()
term.setCursorPos(1,1)
print("Reactor status")
rednet.send(8, "reactor")
id, message = rednet.receive()
if message == a then
  print(a)
  sleep(3)
end
os.reboot()

Did you ever define 'a' on this line?

if message == a then

If you never did, then 'a' will always be nil
jarifle #12
Posted 16 May 2015 - 07:48 AM
I did on the recieving computer.
Basicly i want the recieving computer to send some variables when it gets a rednet signal just to make sure you understand.
The recieving computer uses a to define if reactor is active.
valithor #13
Posted 16 May 2015 - 08:22 AM
I did on the recieving computer.
Basicly i want the recieving computer to send some variables when it gets a rednet signal just to make sure you understand.
The recieving computer uses a to define if reactor is active.

The problem is when you send the variable a you are not sending the actual variable you are sending what the variable equals. So, what the computer receives is what a equaled, but it does not mean that the variable a is available on the receiving (sending program) computer.

There are a few things you can do.
1. Have the sending program only recieve messages from the receivng program, so you know for 100% the message is always a
2. Have something in the message that makes it obvious that variable a is in the message. ex: you could send ("RednetMiner: "..a), and you could then use the string api to get a from the string.
3. This option is the one I would personally do. Make a table and for the first entry make something that is identifiable. For the second entry have the variable. I will put a example of this one below as it is the one I would do.


rednet.send(6,{msgID = "RednetMiner",val = a}) --# sending message to computer 6

id, message = rednet.receive() --# receiving and handling

if id == someID and type(message) == "table" then --# making sure the message is from the right comp and is a table
  if message["msgID"] == "RednetMiner" then --# checking to see what the msgID is
	a = message["val"]
	--# do stuff with a
  elseif message["msgID"] == somethingElse then --# if you wanted to send multiple messages you would want multiple values
	--# do other stuff
  end
end

edit:

Sorry if this completely missed what was wrong… It seems all of my posts that I make after 2 am are off topic… lol
Edited on 16 May 2015 - 03:19 PM
jarifle #14
Posted 16 May 2015 - 12:04 PM
So i cant send a = false and make the other computer print the a?0
flaghacker #15
Posted 16 May 2015 - 12:23 PM
So i cant send a = false and make the other computer print the a?0

Nope. You can hack something like that together using loadstring , but I don't recommend it.

Think a bit about your program logic. Is it really necessary you set a? Maybe you can use the rednet message directly…
Edited on 16 May 2015 - 10:24 AM
jarifle #16
Posted 16 May 2015 - 12:58 PM
Well it kinda is becaus its for a reactor program over rednet so the numbers vary quite alot and i want to be able to request the status of the reactor.
HPWebcamAble #17
Posted 16 May 2015 - 04:33 PM
Like valithor suggested, you can send a table that has the values you need.

That way, you don't have to send multiple messages for 20 different numbers

When the receiving computer gets a table, it can assume it's the right one, and access the keys that the sending computer made


Valithor has a great example, except for this:

rednet.send(6,{msgID = "RednetMiner",val = a})

id, message = rednet.receive()

if id == someID and type(message) == "table" then
  if message[msgID] = "RednetMiner" then --# Should be 'message["msgID"]', the quotes are important
	a = message[val] --# Again, you need quotes around 'val'
	--# do stuff with a
  elseif message[msgID] = somethingElse then --# And again here around 'msgID'
Edited on 16 May 2015 - 02:34 PM
valithor #18
Posted 16 May 2015 - 05:17 PM
Like valithor suggested, you can send a table that has the values you need.

That way, you don't have to send multiple messages for 20 different numbers

When the receiving computer gets a table, it can assume it's the right one, and access the keys that the sending computer made


Valithor has a great example, except for this:

rednet.send(6,{msgID = "RednetMiner",val = a})

id, message = rednet.receive()

if id == someID and type(message) == "table" then
  if message[msgID] = "RednetMiner" then --# Should be 'message["msgID"]', the quotes are important
	a = message[val] --# Again, you need quotes around 'val'
	--# do stuff with a
  elseif message[msgID] = somethingElse then --# And again here around 'msgID'

Now that I am reading over it there are a few other bugs in it as well. Guess that is what happens when I post stuff at 2 am…

Here is it fixed.


id, message = rednet.receive() --# receiving and handling

if id == someID and type(message) == "table" then --# making sure the message is from the right comp and is a table
  if message["msgID"] == "RednetMiner" then --# checking to see what the value is
        a = message["val"]
        --# do stuff with a
  elseif message["msgID"] == somethingElse then --# if you wanted to send multiple messages you would want multiple msgID's
        --# do other stuff
  end
end

Missed the "" for key values in the table, and was using single = for checking conditions.

So i cant send a = false and make the other computer print the a?0

You are trying to make this much more complicated than it needs to be. With rednet you can not send actual variables but you can send what they equal, which is good enough.

So in this example:

a = 5
rednet.send(id, a) --# where id is the id of some computer

The computer that receives this message will receive "5" not a variable only the value. This is why you need a system of determining what that value is, which is what the code above was for.
Edited on 16 May 2015 - 03:30 PM
flaghacker #19
Posted 16 May 2015 - 05:39 PM
Well it kinda is becaus its for a reactor program over rednet so the numbers vary quite alot and i want to be able to request the status of the reactor.

I think you misunderstood me. Can't you for example assume that every message you receive is a? Are you sending anything but the value of a?
jarifle #20
Posted 17 May 2015 - 02:09 AM
Thats the point yes.
Sorry