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:
- adds 1 to second
- if 60 seconds have past it will add 1 to minutes and set seconds to 0
- if 60 minutes have past it will add 1 to uur(hours) and set minutes to 0
- prints the variables
- 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