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

Simple counter and some thing you could do with this

Started by stan2012, 19 October 2012 - 08:12 AM
stan2012 #1
Posted 19 October 2012 - 10:12 AM
Hi all

here is a simple tutorial of how to make a simple counter:

first we have to declare the variables

count = 0

then we need a loop to add 1 to count with:
count = count + 1

count = 0
while true do
  count = count + 1 --add 1 to count
end

but now we have no output so we add the print() command;



count = 0  --sets count to zero
while true do  --main loop
  count = count +1  --add 1 to count
  print(count)  --prints count
end  --ends loop

but now the screen will be flooded with numbers.
so we add term.clear() to clear the screen for us


count = 0  --sets count to zero
while true do  --main loop
  term.clear()  --will clean the screen
  count = count +1  --add 1 to count
  print(count)  --prints count
end  --ends loop

but now the counter will add 1 to count every tick.
so we add sleep(1) to wait 1 second before adding 1 to count


count = 0  --sets count to zero
while true do  --main loop
  term.clear()  --will clean the screen
  count = count +1  --add 1 to count
  print(count)  --prints count
  sleep(1)  --waits 1 second
end  --ends loop

that's it.
now we have successfully made a counter( if i didn't mess up my code :P/>/> )

some advanced things you could do with that simple counter;

a timer with seconds, minutes, and hours for a 2x2 monitor


mon = peripheral.wrap("top")  --connects to the monitor on top of the computer
sec = 0  --declare variables
min = 0
uur = 0

mon.setTextScale(1.5)  --set the text scale of the monitor to a 1.5 magnification

term.redirect(mon)  --redirect the output ot the monitors
while true do  --main loop
  term.clear()  --clear the screen
  term.setCursorPos(1,1)  --set the cursor to the left upper corner

  if sec==60 then  --if 60 seconds have past add 1 to minutes and set seconds to 0
	min = min + 1  --adds 1 to minutes
	sec = 0
  end --ends if

  sec = sec + 1 --counts the seconds

  if min==60 then --if 60 minutes have past add 1 to uur(hours) and set minutes to 0
	uur = uur + 1  --adds 1 to uur(hours)
	min = 0
  end
  --prints everything
  print("time is: ")  
  print(sec.." seconds")
  print(min.." minutes")
  print(uur.." hours")
  sleep(1)  --sleeps for 1 second
end

what this basically does is:
  1. adds 1 to second
  2. if 60 seconds have past it will add 1 to minutes and set seconds to 0
  3. if 60 minutes have past it will add 1 to uur(hours) and set minutes to 0
  4. prints the variables
  5. sleeps 1 second before going back to step 1

measures the distance between 2 places

meter = 0

while not turtle.detect() do
  meter = meter + 1

  term.clear()  --clear the screen
  term.setCursorPos(1,1)  --set the cursor to the left upper corner

  print("traveled "..meter.." meters!")




  turtle.forward()

i think this one doesn't need explanation.


that's it for today(maybe)
if you have any questions post them below.
if you want my coding expertise feel free to contact me. I know allot more than i am showing here.
sorry for my bad English i am dutch
Jazza_Hat #2
Posted 28 October 2012 - 09:33 AM
Those are pretty cool but here's a tip for making a clock. But you will have to format it.



function clock()
	for i = 0,i+1,1 do
		if i > 60 then
			i = i+40
		end
		if i > 6000 then
			i = i + 4000
		end
		if i > 240000 then
			i = i + 760000
		end
		print(i)
	end
end
Skullblade #3
Posted 28 October 2012 - 06:28 PM
is the nerd joke the "Bit Binary"???
stan2012 #4
Posted 09 November 2012 - 08:48 AM
yeah
PixelToast #5
Posted 09 November 2012 - 08:55 AM
well i know a trit ternary
joeypunkrock #6
Posted 16 November 2012 - 01:05 AM
Hey, thanks for the tutorial but I am having a bit of trouble with my counter.

I am trying to make a counter that counts down from a number and displays it on a monitor. This is what I have so far:


count = 83
while true do
count = count -1
mon.setCursorPos(1,10)
mon.write(count)
sleep(1)
end

However this will count down infinity and I need it to end the loop when count == 0

so…

I tried to add


if count == 0 then
stop count
end

Which is probably totally wrong!

I am no master at this believe me and it is probably something really simple I am missing out.

However I should say that my counter is placed inside of an If statement already (and I don't believe you can have two if statements inside of each other, I have tried using elseif but still no luck.

I have a tendency to leave important information out but here is the If statement in which my counter is located:


if input == "mellohi" then
	rednet.broadcast("mellohi")
	mon.setCursorPos(1,2)
	mon.write("Now playing: Mellohi")
	mon.setCursorPos(1,6)
	mon.write("Please wait for current song to finish")
count = 83
while true do
	count = count -1
	mon.setCursorPos(1,10)
	mon.write(count)
	sleep(1)
end
sleep(83)
mon.setCursorPos(1,2)
mon.write("Please select a song")
mon.setCursorPos(1,6)
mon.clearLine()
end

My code is for a DJ Jukebox in which people can choose a song and it plays. It's all working great except I want a timer to count down for a certain amount of time until the song has finished so the player can choose another song.

If anyone might be able to help shed some light on what I may be missing out or doing wrong that would be great.

I apologize for the extremely LONG post but it's better than leaving information out.

Thanks!
Orwell #7
Posted 16 November 2012 - 03:39 AM

count = 83
while count > 0 do -- this_is_ the adjustment :P/>/>
count = count -1
mon.setCursorPos(1,10)
mon.write(count)
sleep(1)
end

Just let the loop run while count is greater than zero. If you want to include zero change 'while count > 0 do' to 'while count >= 0 do'.
joeypunkrock #8
Posted 16 November 2012 - 04:23 AM

count = 83
while count > 0 do -- this_is_ the adjustment :P/>/>
count = count -1
mon.setCursorPos(1,10)
mon.write(count)
sleep(1)
end

Just let the loop run while count is greater than zero. If you want to include zero change 'while count > 0 do' to 'while count >= 0 do'.

omg I literally face-palmed when I saw this I knew it was something little I should have done. Thank you very much!!!
I made sure I removed the sleep(83) after the loop because that makes it sleep for another 83 seconds haha.

Now I'm going to attempt to get the jukebox to automatically play songs when noone has inputted a song and also maybe try and queue songs. If it is possible at least.

Thank's a lot!
cheekycharlie101 #9
Posted 16 November 2012 - 08:16 AM
instead of print for the code wouldn't you need mon.write()? or do you not need that as you have redirected the terminal using term.redirect()?
Orwell #10
Posted 16 November 2012 - 09:40 AM
instead of print for the click wouldn't you need mon.write()? or do you not need that as you have redirected the terminal using term.redirect()?
I've been searching and searching across this page and I have no clue which bit you're talking about. :P/>/> What click?

Hmmm, after looking some more.. If you're talking about the code in the tutorial, yes the term is redirected, so everything you write using write() or print() would go to the monitor.
cheekycharlie101 #11
Posted 16 November 2012 - 09:53 AM
[xml][/xml]
gmdmb3 #12
Posted 16 November 2012 - 03:37 PM
Hello Everyone,

I'm looking for some help here. I tried the code above for the timer and its great but i'm looking to alter it a bit.
I'll start by explaining the project…

i have an automated tree farm that uses redstone timers to run the 4 different phases
Phase 1: Planting Phase. runs for 40 seconds
Phase 2: Reset Builder Phase. runs for 2 seconds
Phase 3: Growing Phase. runs for 20 minutes
Phase 4: Harvesting Phase. runs for 2 minutes and 40 seconds

now what i wanted to do was have a monitor display the phase and the countdown timer left in the phase
however my programming skills are less than Nil.

i would appreciate any help i can get
Orwell #13
Posted 16 November 2012 - 04:28 PM

local mon

local phase = {}
phase.planting = {['next']=phase.resetBuilder, ['duration']=40, ['name']="Planting Phase"}
phase.resetBuilder = {['next']=phase.growing, ['duration']=2, ['name']="Reset Builder Phase"}
phase.growing = {['next']=phase.harvesting, ['duration']=1200, ['name']="Growing Phase"}
phase.harvesting = {['next']=phase.planting, ['duration']=200, ['name']="Harvesting Phase"}

local currentPhase = phase.planting

local function printPhase(cPhase, timer)
  mon.clear()
  mon.setCursorPos(1,1)
  mon.write( string.format("%s: %d seconds left.", cPhase.name, cPhase.duration-timer) )
end

local function findMonitor()
  for _,side in pairs(rs.getSides()) do
	if peripheral.getType(side) == "monitor" then
	  return peripheral.wrap(side)
	end
  end
  error("No monitor found!")
end

mon = findMonitor()

while true do
  for i=1,currentPhase.duration do
	printPhase(currentPhase, i)
	sleep(1)
  end
  currentPhase = currentPhase.next
end

I can't believe I just did this.. Though, next time, you should ask in the 'Ask a Pro' subforum. If you have any question about the code, feel free to ask. :P/>/>

EDIT: corrected issue mentioned in the post below this one
gmdmb3 #14
Posted 16 November 2012 - 06:08 PM
Thank you so much for the fast response
i tried the code but its not working
i get treefarm:12: attempt to index ? (a nil value)
any clues?
also is there any reason you used {} instead of ()?
does it make a difference?
Orwell #15
Posted 17 November 2012 - 04:13 AM
That was a very stupid mistake I made. :)/>/> Edited in the previous post.
{} defines a table. It holds the different phases in it. Each phase is also a table holding a reference to the next table (phase), the duration to wait and the name. Look up on lua tables to understand this better. :D/>/>
Tiin57 #16
Posted 17 November 2012 - 10:31 AM
That was a very stupid mistake I made. :)/>/> Edited in the previous post.
{} defines a table. It holds the different phases in it. Each phase is also a table holding a reference to the next table (phase), the duration to wait and the name. Look up on lua tables to understand this better. :D/>/>
http://lua.org/pil
gmdmb3 #17
Posted 19 November 2012 - 10:09 PM
ok i finally got to try the code and the first phase runs with no problems but after that i get that error again but it says 29 instead of 12. what does that mean?