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

redstone signal won't stop

Started by mikesaa309, 16 June 2013 - 10:07 AM
mikesaa309 #1
Posted 16 June 2013 - 12:07 PM
So I have a program which will detect a minecart on a detector track and send a message to another computer over rednet to say train is at (station name). This works perfectly however it doesn't stop looping. I want it to say train is at station name then thats it but instead it keeps printing it and when the redstone signal is false it's suppose to say train has left station name which it does but again it keeps going so on the receiving computer it looks like this:

train is at station
train is at station
train is at station

But i want it to do:

train is at station

and not repeat the line of text even if the redstone signal is still true and the same for when it leaves the station or when the redstone signal is false. Here is the code so far:


–Variables
local stationName = ""
local id = 0
local modemSide = someSide
local frequence = someFrequence
local rsSide = someSide

–Initialisation
rednet.open("left")

–getting the station name
term.write("station name: ")
stationName = read()

–getting the id
term.write("Enter computer ID: ")
id = read()
while not tonumber(id) do
print("Not a number")
id = read()
end
id = tonumber(id)

–Functions
local function changeName()
term.write("Enter station name: ")
stationName = read()
end

local function changeId()
term.write("enter computer ID: ")
id = read()
while not tonumber(id) do
print("Not a number")
id = read()
end
id = tonumber(id)
end

local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" then
if sKey == keys.i then
changeId()
elseif sKey == keys.n then
changeName()
end
end
end
end

local function waitForRs()
os.pullEvent("redstone")
while true do
if rs.getInput("back") then
rednet.send(id,"train is at "..stationName)
else rednet.send(id, "train has left "..stationName)
end
sleep(1)
end
end




–Main function
local function main()
parallel.waitForAny(waitForKeypress, waitForRs)
end

–BSoD
local _,err = pcall(main)
if err then
print("Some bad error has occured D:\n\n")
print(" " .. tostring(err) .. " \n\n")
print("press any key to exit…")
while true do
local evt = os.pullEvent()
if evt == "key" then
break
end
end
end
diegodan1893 #2
Posted 16 June 2013 - 12:46 PM
Please use code tags to post code in the forums, it makes the code more readable.

The problem is that you put the os.pullEvent() outside the while loop, so when the first event occur, the program will keep looping forever as it doesn't listen for more events. However, your program can be more efficient if you don't use parallel API, there is no reason for using it, you only need an event listener:


local function eventListener()
   while true do
	  local event, p1, p2, p3 = os.pullEvent()
	  if event == "key" then
		 if p1 == keys.i then
			changeId()
		 elseif p1 == keys.n then
			changeName()
		 end
	
	  elseif event == "redstone" then
		 if rs.getInput("back") then
			rednet.send(id, "train is at "..stationName)
		 else
			rednet.send(id, "train has left "..stationName)
		 end
      end
   end
end

You can call this function in the main funcition or use it directly as the main function.
mikesaa309 #3
Posted 16 June 2013 - 03:12 PM
So i've made a program which detects a cart on a detector track and sends a message over rednet to another computer saying train is at station. This works fine. I want it to display on a monitor and by using the cursor position and term.write i've managed to get it to work perfectly. The following code asks the user for the side the monitor is on then when ever the computer receives a rednet message to display it on the monitor. this works correctly and I can specify what side the monitor is on without editing the code. However I want to be able to change the monitors side without restarting the program so in the code i attempted to set a key n to be pressed which will ask th user again to specify the monitors side but nothing happens when i press n on the keyboard. What is wrong with this code?


local side = ""
print("side of monitor: ")
side = read()
rednet.open("right")
mon = peripheral.wrap(side)
term.clear()
term.setCursorPos(1, 1)

local function changeSide()
print("Enter monitor side: ")
side = read()
end

local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" then
if Skey == keys.n then
changeSide()
end
end
end
end

local pos = 0

while true do
local id, message, distance = rednet.receive()
mon.setCursorPos(1, pos)
pos = pos + 1
mon.write(message)
end

local function main()
parallel.waitForAny(waitForKeypress)
end
mibac138 #4
Posted 16 June 2013 - 03:24 PM
Change

local function changeSide()
print("Enter monitor side: ")
side = read()
end

to:

local function changeSide()
print("Enter monitor side: ")
mon=peripheral.wrap(read())
end


@Edit this isnt the couse but it will "repair" some of your code :)/>
your code is horrible ;/

@down ninja'd
diegodan1893 #5
Posted 16 June 2013 - 03:27 PM
It will be more efficient if you read this: http://www.computerc...725#entry126725

About you question here, it doesn't work because
if Skey == keys.n then
Should be
if sKey == keys.n then
Remember that Lua is case sensitive.


But that will still not work because you need to peripheral.wrap() the new side again. So your changeSide function should be:

local function changeSide()
   print("Enter monitor side: ")
   side = read()
   peripheral.wrap(side)
end


Edit: ninja'd.
Edit 2: how can I remove the on the code?
Edit 3: done (I think)
Lyqyd #6
Posted 16 June 2013 - 03:47 PM
Threads merged. Please stick to one topic for questions about a single piece of code.