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

[Question] How can I make this clock go right

Started by Lakur545, 05 April 2012 - 04:08 PM
Lakur545 #1
Posted 05 April 2012 - 06:08 PM
Hey im new on this site.
Only a few days ago i began playing with Computercraft.
I then wanted to make a clock to show the time, and found the pastebin to a clock
I fiddled a little with it and made it able to set the hour, minute, second value for the clock, but it doesn't reset my second value to 0 after the first run.
i would like to get a little guidance.
the code is below in the spoiler
Spoiler

hours = 0
minutes = 0
seconds = 0
term.clear()
term.setCursorPos(1,1)
print("hours: ")
hours = io.read()
term.clear()
term.setCursorPos(1,1)
print("minutes: ")
minutes = io.read()
term.clear()
term.setCursorPos(1,1)
print("seconds: ")
seconds = io.read()
term.clear()
term.setCursorPos(1,1)

function clock()
for seconds = seconds, 60, 1 do
  shell.run("clear")
  print (hours, ":",minutes,":", seconds)
  print ("hours:minutes:seconds")
   if seconds == (60) then
	minutes = minutes + 1
	clock()
   end
   if minutes == (60) then
	hours = hours + 1
	minutes = 0
   end
   if hours == (24) then
   hours = 0
   end
   sleep (1)
  end
seconds = 0
end
clock()
OmegaVest #2
Posted 05 April 2012 - 06:28 PM
Well, here's a few things I see.

Firstly, to answer your question: the reason it does not reset is at line 2 of the function. Namely 'seconds = seconds'. The program is looking at its global variable for a value, not making a new one every time. You need to add a 'seconds = 0' to the if statement before the recursive loop.

Onto that, however. It may be my lack of experience with programming, but a recursive loop inside a for loop is. . .rather awkward to my eyes. I would suggest a single while loop that increments the necessary values when needed, like so:
Spoiler

print ("Hours: ")
hours = io.read()
term.clear()
term.setCursorPos(1,1)
print ("Minutes: ")
minutes = io.read()
term.clear()
term.setCursorPos(1,1)
print ("Seconds: ")
seconds = io.read()
term.clear()
term.setCursorPos(1,1)

while true do
   shell.run("clear")
   print (hours, ":", minutes, ":", seconds)
   print (hours;minutes;seconds)
   seconds = seconds+1
   if seconds == 60 then
	  seconds = 0
	  minutes = minutes+1
	  if minutes == 60 then
		 minutes = 0
		 hours = hours+1
		 if hours == 24 then
			hours = 0
		 end
	  end
   end
sleep(1)
end

Bear in mind, this will only stop if you use CRTL-T. And, I'm certain there are better ways to do this, even recursively, but this will do what you want it to, I think.
Lakur545 #3
Posted 05 April 2012 - 06:54 PM
Thxs alot for the quick reply, and thxs for the info, as im rather new, im glad being thaught new stuff.


*edit
while true do
shell.run("clear")
print (hours, ":", minutes, ":", seconds)
print ("hours;minutes;seconds") <—- found out "" was missing :)/>/>
seconds = seconds+1
if seconds == 60 then
seconds = 0
minutes = minutes+1
if minutes == 60 then
minutes = 0
hours = hours+1
if hours == 24 then
hours = 0
end
end
end
sleep(1)
end
Edited on 05 April 2012 - 05:13 PM
OmegaVest #4
Posted 05 April 2012 - 07:05 PM
One thing you might do, if you feel even my code is a bit complex, take a look at the tutorial section of these forums. A lot of this is actually in there.

Also, for a cleaner interface, you might think about using term.setCursor(x,y) and term.write(text) in place of prints. More work, but much more worth it. Was actually my first program, making the default shell look nice while I used it.
Lakur545 #5
Posted 05 April 2012 - 07:21 PM
Btw is there a way to have a pullEvent that would make u able to add 1 hour, minute or second, while the clock is running ?
OmegaVest #6
Posted 05 April 2012 - 07:39 PM
Mmm. Yes, I think so.

Inside the while loop, either at the beginning or end, place the following code snippet. Again, this is not terribly elegant, but it should work.
Spoiler

os.startTimer(1.0)
events, attrib = os.pullEvents()
if events == "char" then -- could also use "key" but I forgot the values for the keys
   if attrib == tolower("h") then -- "h" can be replaced with anything
	  hours = hours+1
   elseif attrib  == tolower("m") then
	  minutes = minutes+1
   elseif attrib == tolower("s") then
	  seconds = seconds +1
   end
elseif( events == "timer" then
   seconds = second +1
end

Then remove the sleep() and seconds incrementor and it should work out perfectly.
Lakur545 #7
Posted 05 April 2012 - 07:52 PM
thxs for the fast answer once again