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

Test Fail

Started by Jman, 06 April 2013 - 06:20 PM
Jman #1
Posted 06 April 2013 - 08:20 PM
Okay so I have been revamping my previous version of code for one of my mine cart roller coasters on my lan server, however I cant get the count down timer to stop counting. The function wont let me break the loop. Please Help I'm already at (0:0-1385).

Here's the code.
on = boot do
term.clear()
term.setCursorPos(14.5,9)
textutils.slowPrint ("Launch Terminal V.2.2")
term.setCursorPos(14.5,10)
textutils.slowPrint ("SPIRE® & JmanSites™\©")
sleep(2)
end
-- BREAK LINE -- CONTENT ABOVE NOT INCLUDED IN 'REFRESH' --
--[Setup]--
term.clear()
term.setCursorPos(1,1)
a1="1"; a2="2"; a3="3"; b1="Start"; b2="Info"; b3="Update"
print "1] Start Ride  2] Info  3] Update"
write "Input: "
ip = read()
--[Functions]--
local function check()
if s == 0 then
  term.setCursorPos(15,10)
  textutils.slowPrint ("	Ride Stoping	")
  os.pullEvent("redstone")
  rs.setOutput("back", false)
  sleep(.2)
  rs.setOutput("back", false)
  term.clear()
  term.setCursorPos(14.5,10)
  textutils.slowPrint ("Ride Stopped!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint ("Reseting Launch Terminal")
  sleep(0.5)
  shell.run("Refresh")
end
end
--[CODE]--
--(start)--
if ip == a1 or b1 then
term.clear()
term.setCursorPos(13,9)
textutils.slowPrint ("Ride Duration - 1 minute")
term.setCursorPos(15,10)
print ("Remaining: 1:00")
redstone.setOutput("back", true)
s=59	   -- Miliseconds Coming Soon
while true do
  term.clear()
  term.setCursorPos(13,9)
  print ("Ride Duration - 1 minute")
  term.setCursorPos(15,10)
  if s >= 10 then   -- Time Read Start
   print ("Remaining: 0:"..s)
  elseif s <= 10 then
   print ("Remaining: 0:0"..s)
  end	 -- Time Read End
  sleep(0.95)
  s = s-1
  check()
end
--(info\update)--
elseif ip == a2 or b2 then
term.clear()
print("Info Not Yet Available")
shell.run("Refresh")
elseif ip == a3 or b3 then
term.clear()
term.setCursorPos(1,1)
print("[SERVER] Updating Software")
sleep(3)
print("[SERVER] Auto-Updater may be Corrupt or Disabled")
sleep(1)
print("[SERVER] Requesting Service Engine")
sleep(.75)
print("[ASE] Would You Like to...")
sleep(.5)
print("[ASE] ERROR!")
print("[ASE] Automated Service Engine Incomplete. Executing Process")
sleep(1)
shell.run("MOC")
--(hidden shutdown)--
elseif ip == "Shut Down" then
term.clear()
term.setCursorPos(0,1)
print ("[SYSTEM]")
sleep(0.6); term.setCursorPos(9,1)
textutils.slowPrint("DISABLING PRESSURE BLOCK")
os.shutdown()
else
print "Unknown Command"
shell.run("Refresh")
end

EDIT: Wonderful, I just discovered that my options don't work, no matter what I type out of the choices that are presented ("1,2,3,Start,Info,Update,Shut Down") They all just start the timer.
Smiley43210 #2
Posted 06 April 2013 - 10:14 PM
Ok, looked at your code, found some issues.

1. Fixing the options (the menu)
Here's something to remember: No shortcuts in conditional statements. Things like
local test = "tulip"
if test == "tulip" or "three" then
	return true
end
wont work (in your case). The code doesn't error because it's still valid. Conditionals in Lua work like so: If the value is nil or false, it evaluates to false. Otherwise, true. So, you must do
local test = "tulip"
if test == "tulip" or test == "three" then
	return true
end
For all of the ifs and elseifs checking the read(), you have to add in that second part.

2. Fixing the negative timer
Calling a program from itself is a bad practice (well, at least, I think it is) and, if you do it too much, will eventually error (49 calls).
Instead, use a main while loop. So stick everything below the break line EXCEPT for the check() function in a while loop. (Basically add "while true do" above "–[Setup]–" on line 10, add another end at the bottom, and move the function check() above the while declaration.
Next, we'll fix the timer itself. You can do one of two things.

a. Ditch the check() function
Instead of calling check() in the while loop after "if ip == a1 or ip == b1 then", move everything in check() to the bottom of the loop, and then add break to the bottom of the if conditional
Spoiler
while true do
  term.clear()
  term.setCursorPos(13,9)
  print ("Ride Duration - 1 minute")
  term.setCursorPos(15,10)
  if s >= 10 then   -- Time Read Start
   print ("Remaining: 0:"..s)
  elseif s <= 10 then
   print ("Remaining: 0:0"..s)
  end	-- Time Read End
  sleep(0.95)
  s = s-1
  if s == 0 then
   term.setCursorPos(15,10)
   textutils.slowPrint ("		Ride Stopping	")
   os.pullEvent("redstone")
   rs.setOutput("back", false)
   sleep(.2)
   rs.setOutput("back", false)
   term.clear()
   term.setCursorPos(14.5,10)
   textutils.slowPrint ("Ride Stopped!")
   sleep(2)
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint ("Reseting Launch Terminal")
   sleep(0.5)
   break
end
end

b. Change the check() function
In the function, add a return true at the bottom, right before the 2 end statements. Then add a return false in between the end statements (the if and the function ends).
Next, in the while loop that handles the timer, replace "check()" with

if check() then
break
end

3. One non-critical thing
if s >= 10 then   -- Time Read Start
print ("Remaining: 0:"..s)
elseif s <= 10 then
print ("Remaining: 0:0"..s)
end	-- Time Read End
This does work, but if you reversed the order, at 10 seconds remaining, it would print "0:010". You can remove the = in the elseif part.


SpoilerFixed code using option b for step 2
http://pastebin.com/755CmASL
VADemon #3
Posted 06 April 2013 - 10:57 PM

local function check()
if s == 0 then
  term.setCursorPos(15,10)
  textutils.slowPrint ("	Ride Stoping	")
  os.pullEvent("redstone")
  rs.setOutput("back", false)
  sleep(.2)
  rs.setOutput("back", false)
  term.clear()
  term.setCursorPos(14.5,10)
  textutils.slowPrint ("Ride Stopped!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint ("Reseting Launch Terminal")
  sleep(0.5)
  shell.run("Refresh")
end
end
-- bla bla bla
s=59	   -- Miliseconds Coming Soon
while true do
  term.clear()
  term.setCursorPos(13,9)
  print ("Ride Duration - 1 minute")
  term.setCursorPos(15,10)
  if s >= 10 then   -- Time Read Start
   print ("Remaining: 0:"..s)
  elseif s <= 10 then
   print ("Remaining: 0:0"..s)
  end	 -- Time Read End
  sleep(0.95)
  s = s-1
  check()
end

First of all, I recommend you to give the timer seconds directly to the function check(). With that you won't need the global variable s

local function check(timerValue)
--bla bla bla do something with "timerValue" inside the function
end

check(s) -- call from you countdown function
Moreover, to stop the while-loop, you have to insert a break inside that loop (of course with a condition, so you don't instantly stop the whole loop).

But like always, there's a better solution ;)/>
Use a for-loop. Here's my ready code that (hopefully) works. I think I explained enough in comments to the code

--please start reading from your countdown code
local function check(timerSecond) --ofc we need to "accept" the transfered variable here
if timerSecond == 0 then
  term.setCursorPos(15,10)
  textutils.slowPrint ("		Ride Stoping	")
  os.pullEvent("redstone")
  rs.setOutput("back", false)
  sleep(.2)
  rs.setOutput("back", false)
  term.clear()
  term.setCursorPos(14.5,10)
  textutils.slowPrint ("Ride Stopped!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint ("Reseting Launch Terminal")
  sleep(0.5)
  shell.run("Refresh")
end
end
--[CODE]--
--(start)--
if ip == a1 or b1 then
term.clear()
term.setCursorPos(13,9)
textutils.slowPrint ("Ride Duration - 1 minute")
term.setCursorPos(15,10)
print ("Remaining: 1:00")
redstone.setOutput("back", true)
s=59	   -- Miliseconds Coming Soon
for i = s, 0, -1 do -- iterate from 59 to 0. -1 is necessary for a countdown
  term.clear()
  term.setCursorPos(13,9)
  print ("Ride Duration - 1 minute")
  term.setCursorPos(15,10)
  if i >= 10 then   -- Time Read Start -- NOW inside that loop we will use "i" as variable,
  -- because it goes down after every iteration
   print ("Remaining: 0:"..i)
  elseif i < 10 then -- it's actually i<10 and not i<=10. The highest number there is 9.
   print ("Remaining: 0:0"..i)
  end	-- Time Read End
  sleep(0.95)
  -- s = s-1 we simply dont need this anymore, for-loop does the trick
  check(i) --now we call check() function with the current number
end
And yeah, the for-loop will stop when it'll reach zero :)/>