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
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