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

error printing 12 and exiting with no print/write 12 or a vairable that could contain 12

Started by tebbenjo, 28 November 2012 - 04:05 PM
tebbenjo #1
Posted 28 November 2012 - 05:05 PM
the goal / setup
I'm trying to make a program to time beer / rum and have persistent data, my setup is 14 booze barrels each with a button (attached to the first 14 colours of bundled cable, to the back of the computer) and a red stone lamp ( same but to the bottom) and a indicator lamp on each side of the computer. I want to time each barrel when the button is pressed to the amount of time the user types in, with persistent data be twine logins / chunks unloading.

the problem
my program prints 12 and exits even tough it shouldn't exit or print anything like 12 or any variable that could contain 12. to replicate the error; just run the program, no special circumstances required.

the code
warning this is a work in progress, load doesn't work yet and i'm just programming barrel number one first because the blocks relating to a specific barrel should tessellate with only minor adjustments. sorry for probably posting a stupid error and being noobie, but I spent all of my free time for the past weak on finding this error I.e. > 10 Hours but instead of rage quitting and probably being noobie forever i'll ask for help and hopefully learn something. so please help a noob in need.

Spoiler


term.clear()
term.setCursorPos(1,1)



 function addcol(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end
 function remcol(col, side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end



save = function()
print("save started")
while true do
sleep(0)
timerf = fs.open("timer", "w")
timerf.writeLine(tostring(tme))
timerf.write(tostring(qu))
timerf.close()

timef = fs.open("time", "w")
timef.writeLine(tostring(timeone))
timef.writeLine(tostring(timetwo))
timef.writeLine(tostring(timethree))
timef.writeLine(tostring(timefour))
timef.writeLine(tostring(timefive))
timef.writeLine(tostring(timesix))
timef.writeLine(tostring(timeseven))
timef.writeLine(tostring(timeeight))
timef.writeLine(tostring(timenine))
timef.writeLine(tostring(timeten))
timef.writeLine(tostring(timeelven))
timef.writeLine(tostring(timetwlve))
timef.writeLine(tostring(timetherteen))
timef.write(tostring(timefourteen))
timef.close()

timel = fs.open("time left", "w")
timel.writeLine(tostring(timeleftOne))
timel.writeLine(tostring(timeleftTwo))
timel.writeLine(tostring(timeleftThree))
timel.writeLine(tostring(timeleftFour))
timel.writeLine(tostring(timeleftFive))
timel.writeLine(tostring(timeleftSix))
timel.writeLine(tostring(timeleftSeven))
timel.writeLine(tostring(timeleftEight))
timel.writeLine(tostring(timeleftNine))
timel.writeLine(tostring(timeleftTen))
timel.writeLine(tostring(timeleftElven))
timel.writeLine(tostring(timeleftTwlve))
timel.writeLine(tostring(timeleftTherteen))
timel.writeLine(tostring(timeleftFourteen))
timel.close()

sleep(0.2)
end
end



timer = function()
print("timer started")
while true do
sleep(0)
for qu = 0, 60 do
sleep(1)
end
tme = tme + 1
end
end



main = function()
print("main started")
while true do
sleep(0)
if rs.testBundledInput ("back", 1) == true then
rs.setOutput("right", true)
rs.setOutput("left", true)
addcol(1, "bottom")
timeone = tme
term.clear()
term.setCursorPos(1,1)
print("you just put something in number One")
print("|-------------------------------------------|")
print("|                                           |")
print("|      what would you like to make?         |")
print("| 1) Brew--------------------------0 minuts |")
print("| 2) Youngster-------------------120 minuts |")
print("| 3) Beer------------------------240 minuts |")
print("| 4) Ale-------------------------720 minuts |")
print("| 5) Dragonblood----------------1440 minuts |")
print("| 6) rum--------------------------60 minuts |")
print("|                                           |")
print("|-------------------------------------------|")
timeleftOne = tonumber(read())
term.clear()
term.setCursorPos(1,1)
print("Still waiting for number One")
 rs.setOutput("right", false)
 rs.setOutput("left", false)
sleep(0)
end

One = tme - timeone
Two = tme - timetwo
Three = tme - timethree
Four = tme - timefour
Five = tme - timefive
Six = tme - timesix
Seven = tme - timeseven
Eight = tme - timeeight
Nine = tme - timenine
Ten = tme - timeten
Elven = tme - timeelven
Twlve = tme - timetwlve
Therteen = tme - timetherteen
Fourteen = tme - timefourteen

term.setCursorPos(1,1)
if One > tonumber(timeleftOne) or One == tonumber(timeleftOne) then
timeone = 0
print("One finshed or empty")
remcol(1, "bottom")
else print("Still waiting "..tostring(One).." minuts for one")
end

sleep(0.25)
term.clear()
end

end



parallel.waitForAll (timer, main, save)
Edited by
tebbenjo #2
Posted 28 November 2012 - 05:08 PM
sorry forgot to mention, copy it into notepad++, it's much easier on the eyes, with syntax highlighting and actual line numbers, so it doesn't slur together like the black and white on this website.
tebbenjo #3
Posted 29 November 2012 - 05:35 AM
someone, help.
bjornir90 #4
Posted 29 November 2012 - 05:57 AM
You can use elseif instead of start a new if in the top of your program, in your timer function you can use the for loop instead of the while … do loop this will make your code cleaner ;)/>. And please indent and comment it, it's very hard to read it. I didn't see where you put a function that only print a variable… it really only print 12 ?
tebbenjo #5
Posted 29 November 2012 - 09:19 AM
You can use elseif instead of start a new if in the top of your program, in your timer function you can use the for loop instead of the while … do loop this will make your code cleaner ;)/>. And please indent and comment it, it's very hard to read it. I didn't see where you put a function that only print a variable… it really only print 12 ?

thank you, I made the changes you suggested, and removed the "work-in-progress" function load, as it isn't called anywhere yet, so was just taking up space and making things confusing.

and no not a function to print a variable; that's the problem, it shouldn't be printing any variables and there's no "print("12")"'s in there anywhere so why does 12 come up?

and yes the output is:

save started
timer started
main started
12

then it returns to the prompt.
tebbenjo #6
Posted 29 November 2012 - 10:59 AM
found it!!!!!!!!; miss copy to the computercraft computer, that interface is hard to use. I had "sleep(o)" instead of "sleep(0)" , so now i feel stupid. ohwhell I'll post the fished code hear when I do finish it then lock the post.
Doyle3694 #7
Posted 29 November 2012 - 11:11 AM
Good job! ;)/> And the threads usually just die by themselfs, so no need to lock :P/>
tebbenjo #8
Posted 30 November 2012 - 11:54 AM
thank you, everybody. as I said I would Here's the fished code:

Spoiler


--/lua
load = function()

timef = fs.open("time", "r")
timeone = tonumber(timef.readLine(1))
timetwo = tonumber(timef.readLine(2))
timethree = tonumber(timef.readLine(3))
timefour = tonumber(timef.readLine(4))
timefive = tonumber(timef.readLine(5))
timesix = tonumber(timef.readLine(6))
timeseven = tonumber(timef.readLine(7))
timeeight = tonumber(timef.readLine(8))
timenine = tonumber(timef.readLine(9))
timeten = tonumber(timef.readLine(10))
timeelven = tonumber(timef.readLine(11))
timetwlve = tonumber(timef.readLine(12))
timetherteen = tonumber(timef.readLine(14))
timefourteen = tonumber(timef.readLine(15))
timef.close()

timel = fs.open("time left", "r")
timeleftOne = tonumber(timel.readLine(1))
timeleftTwo = tonumber(timel.readLine(2))
timeleftThree = tonumber(timel.readLine(3))
timeleftFour = tonumber(timel.readLine(4))
timeleftFive = tonumber(timel.readLine(5))
timeleftSix = tonumber(timel.readLine(6))
timeleftSeven = tonumber(timel.readLine(7))
timeleftEight = tonumber(timel.readLine(8))
timeleftNine = tonumber(timel.readLine(9))
timeleftTen = tonumber(timel.readLine(10))
timeleftElven = tonumber(timel.readLine(11))
timeleftTwlve = tonumber(timel.readLine(12))
timeleftTherteen = tonumber(timel.readLine(13))
timeleftFourteen = tonumber(timel.readLine(14))
timel.close()

timerf = fs.open("timer", "r")
tme = tonumber(timerf.readLine(1))
qu = tonumber(timerf.readLine(2))
timerf.close()

if timeone == nil then timeone = 0 end
if timetwo == nil then timetwo = 0 end
if timethree == nil then timethree = 0 end
if timefour == nil then timefour = 0 end
if timefive == nil then timefive = 0 end
if timesix == nil then timesix = 0 end
if timeseven == nil then timeseven = 0 end
if timeeight == nil then timeeight = 0 end
if timenine == nil then timenine = 0 end
if timeten == nil then timeten = 0 end
if timeelven == nil then timeelven = 0 end
if timetwlve == nil then timetwlve = 0 end
if timetherteen == nil then timetherteen = 0 end
if timefourteen == nil then timefourteen = 0 end

if qu == nil then qu = 0 end
if tme == nil then tme = 0 end

if timeleftOne == nil then timeleftOne = 0 end
if timeleftTwo == nil then timeleftTwo = 0 end
if timeleftThree == nil then timeleftThree = 0 end
if timeleftFour == nil then timeleftFour = 0 end
if timeleftFive == nil then timeleftFive = 0 end
if timeleftSix == nil then timeleftSix = 0 end
if timeleftSeven == nil then timeleftSeven = 0 end
if timeleftEight == nil then timeleftEight = 0 end
if timeleftNine == nil then timeleftNine = 0 end
if timeleftTen == nil then timeleftTen = 0 end
if timeleftElven == nil then timeleftElven = 0 end
if timeleftTwlve == nil then timeleftTwlve = 0 end
if timeleftTherteen == nil then timeleftTherteen = 0 end
if timeleftFourteen == nil then timeleftFourteen = 0 end

term.clear()
term.setCursorPos(1,1)
end
load()

addcol = function(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end
remcol = function(col, side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end

save = function()
print("save started")
while true do
sleep(0)
timerf = fs.open("timer", "w")
timerf.writeLine(tostring(tme))
timerf.write(tostring(qu))
timerf.close()

timef = fs.open("time", "w")
timef.writeLine(tostring(timeone))
timef.writeLine(tostring(timetwo))
timef.writeLine(tostring(timethree))
timef.writeLine(tostring(timefour))
timef.writeLine(tostring(timefive))
timef.writeLine(tostring(timesix))
timef.writeLine(tostring(timeseven))
timef.writeLine(tostring(timeeight))
timef.writeLine(tostring(timenine))
timef.writeLine(tostring(timeten))
timef.writeLine(tostring(timeelven))
timef.writeLine(tostring(timetwlve))
timef.writeLine(tostring(timetherteen))
timef.write(tostring(timefourteen))
timef.close()

timel = fs.open("time left", "w")
timel.writeLine(tostring(timeleftOne))
timel.writeLine(tostring(timeleftTwo))
timel.writeLine(tostring(timeleftThree))
timel.writeLine(tostring(timeleftFour))
timel.writeLine(tostring(timeleftFive))
timel.writeLine(tostring(timeleftSix))
timel.writeLine(tostring(timeleftSeven))
timel.writeLine(tostring(timeleftEight))
timel.writeLine(tostring(timeleftNine))
timel.writeLine(tostring(timeleftTen))
timel.writeLine(tostring(timeleftElven))
timel.writeLine(tostring(timeleftTwlve))
timel.writeLine(tostring(timeleftTherteen))
timel.write(tostring(timeleftFourteen))
timel.close()

sleep(0.1)
end
end

timer = function()
print("timer started")
while true do
sleep(0)
while qu < 59 do
sleep(1)
qu = qu + 1
end
qu = 0
tme = tme + 1
end
end

type1 = function(camefrom)
rs.setOutput("right", true)
rs.setOutput("left", true)
term.clear()
term.setCursorPos(1,1)
print("you just put something in number "..camefrom.."...")
print("|-------------------------------------------|")
print("|										   |")
print("|	  what would you like to make?		 |")
print("| a) Brew--------------------------0 minuts |")
print("| B)/> Youngster-------------------120 minuts |")
print("| c) Beer------------------------240 minuts |")
print("| d) Ale-------------------------720 minuts |")
print("| e) Dragonblood----------------1440 minuts |")
print("| f) rum--------------------------60 minuts |")
print("|										   |")
print("|-------------------------------------------|")
local toreturn = read()
if toreturn == "a" or toreturn == "A" then
toreturn = 0
elseif toreturn == "b" or toreturn == "B" then
toreturn = 120
elseif toreturn == "c" or toreturn == "C" then
toreturn = 240
elseif toreturn == "d" or toreturn == "D" then
toreturn = 720
elseif toreturn == "e" or toreturn == "E" then
toreturn = 1440
elseif toreturn == "f" or toreturn == "F" then
toreturn = 60
else
toreturn = tonumber(toreturn)
end
rs.setOutput("right", false)
rs.setOutput("left", false)
term.clear()
term.setCursorPos(1,1)
return toreturn
end

main = function()
print("main started")
while true do
sleep(0)
if rs.testBundledInput ("back", 1) == true then
timeone = tme
addcol(1, "bottom")
timeleftOne = type1("one")
elseif rs.testBundledInput ("back", 2) == true then
timetwo = tme
addcol(2, "bottom")
timeleftTwo = type1("two")
elseif rs.testBundledInput ("back", 4) == true then
timethree = tme
addcol(4, "bottom")
timeleftThree = type1("three")
elseif rs.testBundledInput ("back", 8) == true then
timefour = tme
addcol(8, "bottom")
timeleftFour = type1("four")
elseif rs.testBundledInput ("back", 16) == true then
timefive = tme
addcol(16, "bottom")
timeleftFive = type1("five")
elseif rs.testBundledInput ("back", 32) == true then
timesix = tme
addcol(32, "bottom")
timeleftSix = type1("six")
elseif rs.testBundledInput ("back", 64) == true then
timeseven = tme
addcol(64, "bottom")
timeleftSeven = type1("seven")
elseif rs.testBundledInput ("back", 128) == true then
timeeight = tme
addcol(128, "bottom")
timeleftEight = type1("eight")
elseif rs.testBundledInput ("back", 256) == true then
timenine = tme
addcol(256, "bottom")
timeleftNine = type1("nine")
elseif rs.testBundledInput ("back", 512) == true then
timeten = tme
addcol(512, "bottom")
timeleftTen = type1("ten")
elseif rs.testBundledInput ("back", 1024) == true then
timeelven = tme
addcol(1024, "bottom")
timeleftElven = type1("elven")
elseif rs.testBundledInput ("back", 2048) == true then
timetwlve = tme
addcol(2048, "bottom")
timeleftTwlve = type1("twlve")
elseif rs.testBundledInput ("back", 4096) == true then
timetherteen = tme
addcol(4096, "bottom")
timeleftTherteen = type1("therteen")
elseif rs.testBundledInput ("back", 8192) == true then
timefourteen = tme
addcol(8192, "bottom")
timeleftFourteen = type1("fourteen")
end

One = tme - timeone
Two = tme - timetwo
Three = tme - timethree
Four = tme - timefour
Five = tme - timefive
Six = tme - timesix
Seven = tme - timeseven
Eight = tme - timeeight
Nine = tme - timenine
Ten = tme - timeten
Elven = tme - timeelven
Twlve = tme - timetwlve
Therteen = tme - timetherteen
Fourteen = tme - timefourteen

term.setCursorPos(1,1)
if One > tonumber(timeleftOne) or One == tonumber(timeleftOne) then
timeone = 0
print("One finshed or empty")
remcol(1, "bottom")
else
print("Still waiting "..tostring(timeleftOne - One).." minuts for one")
addcol(1, "bottom")
end
if Two > tonumber(timeleftTwo) or Two == tonumber(timeleftTwo) then
timetwo = 0
print("Two finshed or empty")
remcol(2, "bottom")
else
print("Still waiting "..tostring(timeleftTwo - Two).." minuts for Two")
addcol(2, "bottom")
end
if Three > tonumber(timeleftThree) or Three == tonumber(timeleftThree) then
timethree = 0
print("Three finshed or empty")
remcol(4, "bottom")
else
print("Still waiting "..tostring(timeleftThree - Three).." minuts for Three")
addcol(4, "bottom")
end
if Four > tonumber(timeleftFour) or Four == tonumber(timeleftFour) then
timefour = 0
print("Four finshed or empty")
remcol(8, "bottom")
else
print("Still waiting "..tostring(timeleftFour - Four).." minuts for Four")
addcol(8, "bottom")
end
if Five > tonumber(timeleftFive) or Five == tonumber(timeleftFive) then
timefive = 0
print("Five finshed or empty")
remcol(16, "bottom")
else
print("Still waiting "..tostring(timeleftFive - Five).." minuts for Five")
addcol(16, "bottom")
end
if Six > tonumber(timeleftSix) or Six == tonumber(timeleftSix) then
timesix = 0
print("Six finshed or empty")
remcol(32, "bottom")
else
print("Still waiting "..tostring(timeleftSix - Six).." minuts for Six")
addcol(32, "bottom")
end
if Seven > tonumber(timeleftSeven) or Seven == tonumber(timeleftSeven) then
timeseven = 0
print("Seven finshed or empty")
remcol(64, "bottom")
else
print("Still waiting "..tostring(timeleftSeven - Seven).." minuts for Seven")
addcol(64, "bottom")
end
if Eight > tonumber(timeleftEight) or Eight == tonumber(timeleftEight) then
timeeight = 0
print("Eight finshed or empty")
remcol(128, "bottom")
else
print("Still waiting "..tostring(timeleftEight - Eight).." minuts for Eight")
addcol(128, "bottom")
end
if Nine > tonumber(timeleftNine) or Nine == tonumber(timeleftNine) then
timenine = 0
print("Nine finshed or empty")
remcol(256, "bottom")
else
print("Still waiting "..tostring(timeleftNine - Nine).." minuts for Nine")
addcol(256, "bottom")
end
if Ten > tonumber(timeleftTen) or Ten == tonumber(timeleftTen) then
timeten = 0
print("Ten finshed or empty")
remcol(512, "bottom")
else
print("Still waiting "..tostring(timeleftTen - Ten).." minuts for Ten")
addcol(512, "bottom")
end
if Elven > tonumber(timeleftElven) or Elven == tonumber(timeleftElven) then
timeelven = 0
print("Elven finshed or empty")
remcol(1024, "bottom")
else
print("Still waiting "..tostring(timeleftElven - Elven).." minuts for Elven")
addcol(1024, "bottom")
end
if Twlve > tonumber(timeleftTwlve) or Twlve == tonumber(timeleftTwlve) then
timetwlve = 0
print("Twlve finshed or empty")
remcol(2048, "bottom")
else
print("Still waiting "..tostring(timeleftTwlve - Twlve).." minuts for Twlve")
addcol(2048, "bottom")
end
if Therteen > tonumber(timeleftTherteen) or Therteen == tonumber(timeleftTherteen) then
timetherteen = 0
print("Therteen finshed or empty")
remcol(4096, "bottom")
else
print("Still waiting "..tostring(timeleftTherteen - Therteen).." minuts for Therteen")
addcol(4096, "bottom")
end
if Fourteen > tonumber(timeleftFourteen) or Fourteen == tonumber(timeleftFourteen) then
timefourteen = 0
print("Fourteen finshed or empty")
remcol(8192, "bottom")
else
print("Still waiting "..tostring(timeleftFourteen - Fourteen).." minuts for Fourteen")
addcol(8192, "bottom")
end

sleep(0.2)
term.clear()
end

end

parallel.waitForAll (timer, main, save)

and some photos.
Spoiler