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

Getting A Variable To Change With A Message, Then Print It.

Started by AppleMan5487, 31 July 2013 - 12:57 PM
AppleMan5487 #1
Posted 31 July 2013 - 02:57 PM
Hello, I am fairly new to ComputerCraft. I have currently made a program detects when my BuildCraft quarry is done mining, then sends it off to another computer, and displays that it is done. The only problem, is that I want to get rid of the scrolling…one way I figured I could do that, was have the message state an if command, and then set/change a variable, and then print it. The only problem, is that because of the repeat, it is pointless and just keeps repeating. It is hard to explain, so take a look at the code. But I was looking to somehow set a variable, and the message change the variable. That way, I could display something like "Quarry Status: *VARIABLE*", and have the variable state the activity. This way, I do not have to have it keep repeatedly updating with "Quarry Running, Quarry Running, etc."

Edit: I explained that rather bad, if you run the code, you can notice that as an item passes over the item detector, and emits a signal, it appears on the monitor, but because it only pulses, it will then display the "Quarry is done" message. Is there an alternative to this? I am okay with it doing this, if it wouldnt appear on the monitor to many times. Which was why i was wondering if it were possible to assign the variables, to avoid the scrolling and have more of "Quarry Status : VAR".

Here is the code.

Receiver code…where I was trying to do this:

rednet.open("top")
message = 0
-- sets a variable called message to 0. This will
-- allow us to store a message later

print("ComputerID")
print(os.getComputerID())
print("Listening...")

function checkMessage() -- start of function
  if message == "RUNNING" then
	print("Quarry - running.")
  elseif message == "NORUNNING" then
	print("Quarry - not running.")
  end
end

repeat
  event,p1,p2,p3 = os.pullEvent()
  if event == "rednet_message" then
	 message=p2 -- overwrites our message variable
	checkMessage() -- this calls our function above
  end
until event == "char" and p1 == "x"

Sender Code:

rednet.open("top")
print("Computer ID")
print(os.getComputerID())

function checkSide() -- start of the function
  if redstone.getInput("right","true")
  then
	rednet.send(36,"RUNNING")
	print("RUNNING sent to ID 36")
  else
	rednet.send(36,"NORUNNING")
	print("NOT RUNNING sent to ID 36")
  end
end -- end of the function
repeat
  event,p1,p2,p3 = os.pullEvent()
  if event=="redstone" then
	checkSide() -- this runs our function
  end
until event=="char" and p1== "x"
Lyqyd #2
Posted 01 August 2013 - 12:24 AM
Split into new topic.
albrat #3
Posted 01 August 2013 - 08:22 AM

rednet.open("top")
message = 0
-- sets a variable called message to 0. This will
-- allow us to store a message later
print("ComputerID")
print(os.getComputerID())
print("Listening...")
function checkMessage(mess) -- start of function
	    term.setCursorPos(6,5) -- example position
	    term.clearLine() -- clear old message line
	    print("Quarry - "..tostring(mess))
end
repeat
  event,p1,p2 = os.pullEvent()  -- we did not use the distance so p3 is not needed.
  if event == "rednet_message" then
		 message=p2 -- overwrites our message variable
	    checkMessage(message) -- this calls our function above
  end
until event == "char" and p1 == "x"
Though technically you would not need the function or the message = p2… as just changing the print to "print(p2)" would work inside the if event == "rednet_message" as this would only print on a rednet message receive…

I would suggest in the sender code Just adding a sleep(5) – to make the code wait a little before sending another message. (do you need a fast update or is it just going to let you know when the quarry finishes when you occasionally look at the screen)

I find that with this sort of thing I only check it myself once every 10 minutes (real time not game time). so a good sleep in the code reduces the traffic, so to speak.
AppleMan5487 #4
Posted 01 August 2013 - 12:17 PM
Thank you so much, that was absolutely perfect. Although, I tried adding the sleep command, and it seems to bug out.
I added it like this:

rednet.open("top")
print("Computer ID")
print(os.getComputerID())
function checkSide() -- start of the function
  if redstone.getInput("right","true")
  then
	    rednet.send(36,"RUNNING")
	    print("RUNNING sent to ID 36")
  else
	    rednet.send(36,"NORUNNING")
	    print("NOT RUNNING sent to ID 36")
  end
end -- end of the function
repeat
  event,p1,p2,p3 = os.pullEvent()
  if event=="redstone" then
	    checkSide() -- this runs our function
	   sleep(5)
  end
until event=="char" and p1== "x"
Sleep 5 seemed to work, perfectly, except for one thing. After it is all finished, it does not send NOT RUNNING. It just updates every 5 with Running, and then it stays at running.

Not sure if I could put the sleep somewhere else, or if there needs to be a tweak in code.

Thank you for all the amazing help!
-AppleMan5487
Blazara #5
Posted 01 August 2013 - 01:49 PM
Just quickly; it may be simpler yet if you use os.pullEvent ("redstone") prior to your if statements in your receiver code. Then it'll only fire when a redstone update occurs;


function checkSide()
  os.pullEvent ("redstone")
	if redstone.getInput("right, "true") then
--Your stuff etc etc
    elseif redstone.getInput("right", "false") then
-- Your stuff etc etc
end
albrat #6
Posted 02 August 2013 - 02:03 PM
ahhh, at a guess the redstone is a signal that turns off when the quarry stops…

so the if statement stops the update of the check… ( without the sleep the update happens 2 or 3 times before it realises the redstone is off. ) * I have noticed this "bug" sometimes with my own code. with fast updating code I can get 2 / 3 false readings before it exits a loop sometimes, if I have no sleep command in the code.


rednet.open("top")
print("Computer ID")
print(os.getComputerID())
function checkSide() -- start of the function
  if redstone.getInput("right","true")
  then
			rednet.send(36,"RUNNING")
			print("RUNNING sent to ID 36")
  elseif redstone.getInput("right","false")
  then
			rednet.send(36,"NORUNNING")
			print("NOT RUNNING sent to ID 36")
  end
end -- end of the function
repeat
  os.startTimer(5) -- wait 5 seconds then trigger an event.
  event,p1,p2,p3 = os.pullEvent()
-- if event=="redstone" then  -- commented if
			checkSide() -- this runs our function
if event == "redstone" then sleep(5) end -- 1 line event sleep only if redstone triggers the event.
-- end -- commented end
until event=="char" and p1== "x"
so that on any event we now checkSide()

NB : the sleep(5) clears the stack… That is why the fast running program sometimes gives 2/3 false readings before it exits the loop… Because it has stacked up items then the loop breaks… But the stack still has 2 / 3 stacks of the commands to run so it finishes the stack.
** thought it would clarify the part about the 2/3 extra runs on fast running code…
Edited on 04 August 2013 - 10:44 AM
blaize9 #7
Posted 28 August 2013 - 04:28 PM
Here is my take on it

Sender
pastebin get eDkTWpNj Quarry-Sender


local computerid = 6 -- Set this to your computer ID

rednet.open("top") -- it will use the wireless modem located on the top
--(you can also use left , right , top , down , front, back)
print("Computer ID: "..os.getComputerID())

while true do
os.pullEvent("redstone")  -- when the redstone signal changes run this
if rs.getInput("right") then  -- When there is a signal tell the computer its not running
   rednet.send(computerid,"NORUNNING")
print("NOT RUNNING sent to ID "..computerid)
else  -- If there is no signal then say its running
rednet.send(computerid,"RUNNING")
   print("RUNNING sent to ID "..computerid)
end
end

Information: Attached to the quarry should be a gate that is set to "Work Done" = "Redstone Signal"
The current code has the redstone signal coming in on the right side, you can change that to whatever side you want.



Receiver
pastebin get YAgbAE9t Quarry-Receiver


rednet.open("top") -- it will use the wireless modem located on the top
--(you can also use left , right , top , down , front, back)
message = 0
monitor = peripheral.wrap("right")

print("Computer ID: "..os.getComputerID())
print("Listening...")
function checkMessage() -- start of function
  if message == "RUNNING" then
	monitor.clear()
	monitor.setCursorPos(1,1)
	monitor.setTextColor(1)
	monitor.write("Quarry")
	monitor.setCursorPos(1,2)
	monitor.setTextColor(8192)
	monitor.write("Running")
	print("Quarry - Running")
elseif message == "NORUNNING" then
   monitor.clear()
   monitor.setCursorPos(1,1)
   monitor.setTextColor(1)
   monitor.write("Quarry")
   monitor.setCursorPos(1,2)
   monitor.setTextColor(16384)
   monitor.write("Not running")
   print("Quarry - Not Running")
  end
end

repeat
  event,p1,p2,p3 = os.pullEvent()
	-- this line above says wait for an event to happen
  if event == "rednet_message" then
	-- if the event is a rednet_message then run this
		 message=p2 -- overwrites our message variable
		checkMessage() -- this calls our function above
  end
until event == "char" and p1 == "x"
-- this is the ending for the repeat it will stop the loop
-- if a character is pressed on your keyboard, if it is an X then stop the repeat

Information: The current setup requires advanced monitors on the right side(again this can be changed fairly easy), you can find one that does not need a monitor here.