I have been trying to figure this out for at least 2 days but i think i lack the knowledge to program such a task can you please help me. Thank you in advance. I have figured out on how to send messages using the modem but i just don't know how to do the timer.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[lua][help]Making a timer (more detail in the description)
Started by ThrowTheCheeze, 11 January 2013 - 09:52 AMPosted 11 January 2013 - 10:52 AM
How to make a timer that will count down if it has receive a specific message from another computer using the modem and that it will keep waiting for the same message and count down every time it received the same message. It will also be nice if the number was displayed on a monitor screen. Also if the number hits zero it will output a redstone signal.
I have been trying to figure this out for at least 2 days but i think i lack the knowledge to program such a task can you please help me. Thank you in advance. I have figured out on how to send messages using the modem but i just don't know how to do the timer.
I have been trying to figure this out for at least 2 days but i think i lack the knowledge to program such a task can you please help me. Thank you in advance. I have figured out on how to send messages using the modem but i just don't know how to do the timer.
Posted 11 January 2013 - 10:59 AM
Can you post what you have. You're probably on the right track and then we can help you from there :)/>
Posted 11 January 2013 - 11:17 AM
Do you mean something like this?
If the computer receives a message like countdown it will begin a countdown
If the computer receives a message like countdown it will begin a countdown
rednet.open('right')
while true do -- infinite loop
senderID, message = rednet.receive()
if message == "countdown" then -- if it receives a messag econtaining 'countdown'
for i = 10, 1, -1 do -- counts down from 10 to 1, -1 is the step
term.setCursorPos(1, 1)
term.clearLine()
print(i .. (i == 1 and " second" or " seconds")) -- basically an if statement within the print function
sleep(1)
end
end
end
Posted 11 January 2013 - 11:18 AM
I am using three computers. One of them is a turtle and the other two are computers.
turtle (id 14)
computer #1 (id 7) The main purpose for this computer is to control the turtle
computer #2 or the timer (id 9)
I have no clue on how to start this one. Sorry for my noobish programming or if it's really hard to understand.
turtle (id 14)
function down()
rednet.open("right")
if turtle.down() == true then
rednet.send(7, "down")
shell.run("new") -- rerun the program
else
rednet.send(7, "no")
shell.run("new")
end
end
rednet.open("right")
id, message = rednet.receive()
if message == "down" then
down()
end
computer #1 (id 7) The main purpose for this computer is to control the turtle
rednet.open("right")
rednet.send(14, "down")
id, message = rednet.receive()
if message == "down" then
print("turtle has successfully go down")
rednet.send(9, "down")
elseif message == "no" then
print("turtle cannot move down")
end
computer #2 or the timer (id 9)
I have no clue on how to start this one. Sorry for my noobish programming or if it's really hard to understand.
Posted 11 January 2013 - 11:19 AM
I want it to subtract a number every time it receives the message.
Posted 11 January 2013 - 11:28 AM
Oh is that what you're trying to do… mm well each time it succesfully moves down, you can
use simple math to decrease a variable by one:
Also, for the turtle program, you do not need to shell.run() the program over and over,
instead use a infinite loop
computer #2 or the timer (id 9)
use simple math to decrease a variable by one:
times = times - 1
Also, for the turtle program, you do not need to shell.run() the program over and over,
instead use a infinite loop
-- turtle code (id 14)
rednet.open("right") -- only need to open it once.
function down()
if turtle.down() then -- with booleans, you do not need to compare them with false or true, you can simply do this and if not turtle.down() then (false)
return "yes"
else
return "no"
end
--[[
A simplier way of doing this can look something like this:
return turtle.down() and "yes" or "no" -- should work.
--]]
end
while true do -- infinite loop
id, message = rednet.receive()
if message == "down" then
work = down()
rednet.send(7, work) -- will send yes or no to computer id 7
end
end
-- computer #1 (id 7)
rednet.open("right")
-- Not sure if you want an infinite loop here
rednet.send(14, "down")
id, message = rednet.receive()
if message == "yes" then
print("Turtle successfully moved down.")
rednet.send(9, "down")
elseif message == "no" then
print("Turtle failed to move down."
end
computer #2 or the timer (id 9)
-- Not sure what the code is here but when it receives 'down' then decremt a value:
times = times - 1
Posted 11 January 2013 - 11:53 AM
so for computer #2 the code will be
but how will it keep counting down the changed value everytime it receives the same message do i have to retype it or is there a better way? I tried using a loop but it keeps printing the number and an error appears.
bios:141: Too long without yielding
This is the coding that i used that keeps making that error
Also thank you for teaching me some tricks or tips to make my coding a lot easier =D
rednet.open("right")
local times = 4 -- example
id, message = rednet.receive()
if message == "down" then
times = times - 1
print(times)
end
but how will it keep counting down the changed value everytime it receives the same message do i have to retype it or is there a better way? I tried using a loop but it keeps printing the number and an error appears.
bios:141: Too long without yielding
This is the coding that i used that keeps making that error
rednet.open("right")
function down()
local count = 4
if message == "down" then
count = count - 1
print(count)
end
end
id, message = rednet.receive()
while true do
down()
end
Also thank you for teaching me some tricks or tips to make my coding a lot easier =D
Posted 11 January 2013 - 02:15 PM
so for computer #2 the code will berednet.open("right") local times = 4 -- example id, message = rednet.receive() if message == "down" then times = times - 1 print(times) end
but how will it keep counting down the changed value everytime it receives the same message do i have to retype it or is there a better way? I tried using a loop but it keeps printing the number and an error appears.
bios:141: Too long without yielding
To make it keep doing the code so it will never stop, use a while loop:
rednet.open("right")
local times = 4 -- example
while true do
term.setCursorPos(1, 1) term.clearLine()
print(times)
id, message = rednet.receive()
if message == "down" then
times = times - 1
end
end
This is the coding that i used that keeps making that errorrednet.open("right") function down() local count = 4 if message == "down" then count = count - 1 print(count) end end id, message = rednet.receive() while true do down() end
Also thank you for teaching me some tricks or tips to make my coding a lot easier =D
It gives this error if you do not let the program yield (pause) within 10 seconds or so.
But you should rather change your code to this:
rednet.open("right")
count = 4
while true do
term.setCursorPos(1, 1) term.clearLine()
print(count)
id, message = rednet.receive() -- put the rednet.receive() within the infinite loop so it will keep listening.
-- In this case, a function is not needed and you can simply do this
if message == "down" then
count = count - 1
end
end
Posted 11 January 2013 - 03:46 PM
Thank you for your service. I really appreciate it now all i have to do is learn how to display it to monitor screen and you don't have to show it to me i wanna learn by myself but thank you for your tips it helps me a lot. :lol:/>
Posted 12 January 2013 - 12:50 AM
Posted 12 January 2013 - 12:42 PM
So this will be my code for the timer
Thank you! It is finally working i tested the program so many times by now :lol:/> and it finally worked thank you for your help! I also put a script on my turtle to dig if there is a block and maybe make it more advance by asking for fuel if needed and now I will try to learn on how to make a menu on my command computer (The one that controls the turtle) Wish me luck :D/>
rednet.open("back")
monitor = peripheral.wrap("top")
term.clear() term.setCursorPos(1,1)
local ylevelt = 0
local fuel = 0
term.write("Enter the fuel level of your turtle:")
fuel = read()
term.setCursorPos(1,2)
term.write("Enter the ylevel of your turtle:")
ylevelt = read()
function ylevel()
monitor.setCursorPos(1,2)
monitor.clearLine()
monitor.setTextColor(colors.yellow)
monitor.write(ylevelt)
end
function tfuel()
monitor.setCursorPos(1,4)
monitor.clearLine()
monitor.setTextColor(colors.yellow)
monitor.write(fuel)
end
function counts()
ylevel() --to display on screen
sleep(.5)
tfuel()
while true do
id, message = rednet.receive()
if message == "down" then
ylevelt = ylevelt-1
ylevel()
elseif message == "fdown" then
fuel = fuel-1
tfuel()
end
end
end
---------Main Program
monitor.clear()
monitor.setTextScale(1)
monitor.setCursorPos(1,1)
monitor.setTextColor(colors.blue)
monitor.write("Current estimated y level:")
monitor.setCursorPos(1,3)
monitor.setTextColor(colors.blue)
monitor.clearLine()
monitor.write("Current fuel level:")
counts()
Thank you! It is finally working i tested the program so many times by now :lol:/> and it finally worked thank you for your help! I also put a script on my turtle to dig if there is a block and maybe make it more advance by asking for fuel if needed and now I will try to learn on how to make a menu on my command computer (The one that controls the turtle) Wish me luck :D/>
Posted 12 January 2013 - 02:29 PM
Good to know it's going good :P/>
With regards to a menu, I'd suggest for you to use tables with the options, even if there is only 2 options. It's much easier to work with (imo).
Goodluck and don't be shy to come back for more help :P/>
With regards to a menu, I'd suggest for you to use tables with the options, even if there is only 2 options. It's much easier to work with (imo).
Goodluck and don't be shy to come back for more help :P/>