Counting Trains

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
I'm trying to set up a computer that counts the number of loops my train makes. I have a locomotive detector set to put a redstone pulse into the bottom of the computer. I want the computer upon getting that signal to increment a number up one, and display it to a monitor above.

I'm playing Space Astronomy which means 1.7.10, though I doubt that makes a difference.
 

RenzosNips

New Member
Jul 29, 2019
199
-1
0
Are you looking for an actual program?

If you are just looking for the type of code, this should be similar. (Pseudo code, of course. I haven't used Lua in a long while)

Code:
monitor = peripheral("side")
trains = 0
tickrate = 20
tickcount = 0
posX = 5
posY = 5

def updateCount()
  if rs.getInput("bottom") == true
    trains = trains + 1
end

def updateScreen()
  monitor.clear()
  monitor.draw(trains, positionX, positionY)
end

while true do
  if tickcount == tickrate
    tickcount = 0
    updateScreen()
  updateCount()
end
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
Yeah, that's the kind of thing I'm looking for. I'm probably the worst programmer ever, so I need to see what I'm trying to do, in order to make it work. Thanks a lot.
 

Inaeo

New Member
Jul 29, 2019
2,158
-3
0
I'm curious as to why your looking for this. Is it just to see how many circuits the train makes before it gets lost/jammed somewhere, or something else? I suppose if you set up a few of these around your rail line, you could use the mismatched counts to determine train position along the line.
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
The train is running on a four thousand block track. Trying to keep watch on it would be a full-time job, and so counting how many circuits it makes is both less hassle and if I see it hasn't changed in a while, I can go and track it down and fix it.

Multiple stations would probably be a good idea, but I'd have to send the information back somehow. Otherwise it's just as fast to follow the train along the track.
 

Inaeo

New Member
Jul 29, 2019
2,158
-3
0
You could use wireless redstone at regular intervals, then use some method of controlling signal strength to differentiate up to 15 checkpoints. I know it's possible, but it would take me a while to wrap my head around LUA.
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
If the pack I'm playing had Wireless redstone that would be fine. I'd have to add it in. Or I could use a computer craft repeater system to update them, but then I'd have to chunkload the repeaters.
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
Okay, so I finally got back to the pack to try this out and ended up with this program:

Code:
monitor = peripheral.wrap("top")
trains = 0
tickrate = 20
tickcount = 0
posX = 5
posY = 5

def = updateCount()
 if rs.getAnalogueInput("bottom") == true
  then trains =  trains + 1
end

def = updateScreen()
  monitor.clear()
  monitor.draw(trains, posX, posY)

while true do
 if tickcount == tickrate
  then tickcount = 0
  updateScreen()
  updateCount()
 end
end

But it tells me I'm trying to call nil. I'm confused.
 

Vaeliorin

New Member
Jul 29, 2019
288
0
0
While I'm not very fluent in LUA, it appears at the very least your missing an end for updateScreen. You're trying to call it within itself, which is (probably) the reason it's saying your trying to call nil.
 

Pyure

Not Totally Useless
Aug 14, 2013
8,334
7,191
383
Waterloo, Ontario
I'm also not fluent in Lua, but I see a couple things that may cause you problems.

1) Is "def" really a lua keyword? I thought functions were declared with "function"
2) You're not terminating all your IF clauses (and/or functions)
3) Its probably good practice to return from void functions. I'm guessing.

Code:
monitor = peripheral("side")
trains = 0
tickrate = 20
tickcount = 0
posX = 5
posY = 5

function updateCount()
  if rs.getInput("bottom") == true
    trains = trains + 1
  end
  return
end

function updateScreen()
  monitor.clear()
  monitor.draw(trains, positionX, positionY)
  return
end

while true do
  if tickcount == tickrate
    tickcount = 0
    updateScreen()
    updateCount()
 end
end
 

Pyure

Not Totally Useless
Aug 14, 2013
8,334
7,191
383
Waterloo, Ontario
I also see you added another version (didn't notice it due to spoiler).

Again, just triple-check if def is a legit lua keyword, and ensure you're terminating your IFs and Functions.
 

KingTriaxx

Forum Addict
Jul 27, 2013
4,266
1,333
184
Michigan
I'm probably the worst programmer ever.

Above statement is still true. You're correct. When I transferred the example, I completely forgot to change those.

This works, except it immediately terminates, instead of hanging around waiting for a redstone signal. I tried adding a short sleep delay, but it sleeps and then terminates, which is less than useful.
Code:
monitor = peripheral.wrap("top")
trains = 0
tickrate = 20
tickcount = 0
posX = 5
posY = 5

function updateCount()
 if rs.getInput("bottom") == true
  then trains =  trains + 1
 end

function updateScreen()
  monitor.clear()
  monitor.draw(trains, posX, posY)
  end

while true do
 if tickcount == tickrate
  then tickcount = 0
  updateScreen()
  updateCount()
 end
end
end
 

Pyure

Not Totally Useless
Aug 14, 2013
8,334
7,191
383
Waterloo, Ontario
Oh now I see the problem, you arent closing the first function. So its looking for an End, but you had it in the wrong place.

Code:
monitor = peripheral.wrap("top")
trains = 0
tickrate = 20
tickcount = 0
posX = 5
posY = 5

function updateCount() -- START OF FUNCTION
  if rs.getInput("bottom") == true then -- START OF IF
      trains = trains + 1
  end -- END OF IF
end -- END OF FUNCTION

function updateScreen() -- START OF FUNCTION
  monitor.clear()
  monitor.draw(trains, posX, posY)
end  -- END OF FUNCTION

-- MAIN PROGRAM STARTS HERE.  
while true do -- START OF LOOP
  if tickcount == tickrate then -- START OF IF
    tickcount = 0
    updateScreen()
    updateCount()
  end  -- END OF IF
end -- END OF LOOP (ITERATE)
 

QuantumZari

New Member
Jul 29, 2019
10
0
0
It is looping but it never makes it into the tickcount if clause as tickcount is never incremented.

More importantly though attempting to use a loop like this will cause the program to be forcibly halted as it never yields. One way to work around this might be to use a sleep statement of some sort but the correct solution would be to use the Event API.

Code:
monitor = peripheral.wrap("top")
trains = 0

--clear screen and print count
function updateScreen()
  monitor.clear()
  monitor.setCursorPos(1,1)
  --calling tostring() removes the .0 at the end
  monitor.write(tostring(trains))
end

--print the count of 0 when starting
updateScreen()

while true do --loop forever
  os.pullEvent("redstone") --yield until a redstone event
  if rs.getInput("bottom") == true then --make sure it went high and not low
    --got a signal, update the count
    trains = trains + 1
    updateScreen()
  end
end