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

Two pulse timer

Started by steve789, 27 October 2013 - 06:12 AM
steve789 #1
Posted 27 October 2013 - 07:12 AM
Title: Two pulse timer

Hi im trying to create a timer that sends two pulses every 5 minutes or so, but i want the second pulse to be 10 seconds behind the first. I figured a computer would be the best option for this but i have no idea how to write a program for it.

Thanks in advance for any help.
CCJJSax #2
Posted 28 October 2013 - 01:16 AM
This is pretty simple actually. Look into Sleep() and the Redstone API. I'll code this for you, but most people probably would point you in the right direction (as I did with the Sleep() and the RS API)

when you see "–" (no quotes) the computer will look over it and pretend it's not even there. these are called comments and are for the coder, not the computer. it's helpful for explanation.



function pulse(side) -- function to pulse the designated side of the computer.  Remember to put your side into quotes, to make it a string.

  rs.setOutput(side, true) -- part of the redstone API.
	sleep(1) -- makes the computer wait 1 second.  Note that the number inside is not in quotes as it is a number, not a string.  It is in seconds.
  rs.setOutput(side, false)

end

while true do -- while is a loop, paired with the true boolean, it will indefinitely loop. AKA it is essentially infinite.
  pulse("top") -- top is in quotes making it a string.  use top, bottom, left, right, front, back in quotes for the respective side of the computer.
  sleep(10) -- waits for 10 seconds
  pulse("top") -- pulses again
  sleep(300) -- waits for 5 minutes.
end


if you want an easy way to have the computer sleep for x amount of minutes then you can do something like this


minutes = 5 * 60 -- so 5 minutes.
sleep(minutes) -- will sleep for the defined number of minutes.
gwatson #3
Posted 19 November 2013 - 06:38 PM
Scripter,

Was hoping you could help. I am a COMPLETE noob at this. But your little script above almost does what I'm looking for. I have played with it for hours and still can't get the result I'm looking for. And I know it's something REALLY simple. Basically I'm trying to turn on the redstone for a set time say 30 seconds then turn it off for a set time say 60 seconds. Please tell me the simple line or two that have eluded me for hours..


Thanks
Lyqyd #4
Posted 19 November 2013 - 08:25 PM
So, you just want this?


while true do
  rs.setOutput("back", true)
  sleep(30)
  rs.setOutput("back", false)
  sleep(60)
end
CCJJSax #5
Posted 19 November 2013 - 11:29 PM
Scripter,


Was hoping you could help. I am a COMPLETE noob at this. But your little script above almost does what I'm looking for. I have played with it for hours and still can't get the result I'm looking for. And I know it's something REALLY simple. Basically I'm trying to turn on the redstone for a set time say 30 seconds then turn it off for a set time say 60 seconds. Please tell me the simple line or two that have eluded me for hours..


Thanks


I think I see what you're saying. Lyqyd is right, and if that is all your program is doing, then the way he said would be better, however if it's going to do more (including if you want to change timings) then you should put it in a function.

This is basically the exact same code as I did before, so my notes are still valid.


function pulse(side, onTime) -- The second parameter (onTime) needs a number input.  e.g. number without quotes

  rs.setOutput(side, true) -- part of the redstone API. 'side' is a string, then boolean (true/false)
        sleep(onTime) -- makes the computer wait number onTime seconds. onTime is defined when called
  rs.setOutput(side, false)

end

while true do
  pulse("top", 30) -- Put a number in after a comma, like this
  sleep(60) -- waits for 60 seconds.
end