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

Chat via redstone! But its not reading incoming messages...

Started by faissaloo, 21 December 2013 - 09:38 AM
faissaloo #1
Posted 21 December 2013 - 10:38 AM
So I have been working on this program for ages, what it does is use analogue redstone signals to send messages, but the issue I have been having is that the print (at the end, print(string.char(timer))) isn't happening and I'm not sure why! It works by converting the ascii code for the letter into a redstone delay which will be read by another computer. Here is my code:

while(true) do
local timer=0
local event, param = os.pullEvent()
if event == 'key' then
  if param == keys.enter then
  local s=read()
  print(s)
  for a = 1, string.len(s) do
	print(string.sub(s,a,a))
	rs.setOutput("back",true)
	print(tostring(string.byte(string.sub(s,a,a))/10))
	sleep(string.byte(string.sub(s,a,a))/10)
	rs.setOutput("back",false)
	sleep(1)
  end
  if param ==keys.f4 then
	return
  end
  if rs.getInput("right")==true then
  while(rs.getInput("right"))==true do
  timer=timer+1
  sleep(0.1)
  end
  print(string.char(timer))
  timer=0
end
end
end
end
Edited on 21 December 2013 - 01:26 PM
Bomb Bloke #2
Posted 21 December 2013 - 06:14 PM
That print statement should be executed so long as the system is getting redstone input to the right at nearly the exact time it finishes transmitting a character out the rear. However, the odds of the timing actually being "accurate" enough to accomplish that is remote.

You might be able to get away with something like this:

while not rs.getInput("right") do os.pullEvent("redstone") end -- Wait until the redstone input on the right turns on.

while rs.getInput("right") do
  timer=timer+1
  sleep(0.1)
end
print(string.char(timer))
timer=0

… however, I still suspect that the timing of things won't be accurate enough to get reliable data, as server tick speeds can vary and so forth.

You could instead use bundled cables. These allow you to send 16 bits of data down the wire instead of the one that a regular redstone wire gives you, which is enough to send at least a couple of basic ASCII characters at a time.

Another option is the "Analog" functions in the redstone API, which allows 16 bits of data over a regular redstone wire.

Regardless as to how you want to try it, I strongly recommend the use of os.pullEvent() to detect when the redstone inputs are turning off or on.

Edit: Oh, and string.byte() has its own system for dealing with segments of a string. string.byte(s,a,a) is perfectly fine on its own.
Edited on 21 December 2013 - 05:16 PM
faissaloo #3
Posted 21 December 2013 - 06:28 PM
How would sending the message work then if I have the program wait for redstone from the right?