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

Program to run redstone devices during desired time

Started by azumi, 07 June 2013 - 02:06 PM
azumi #1
Posted 07 June 2013 - 04:06 PM
HI
I made my first lua program that i would like to share with you.
It is very simple but extremly usefull, you can modyfy it to keep running your rotary macerator or other mashines in specified time (ie: only during your time spend on server) or use it as it is, to light redstone lamps during night.
I found loop with exit possibility by Lettuce, but i dont know why it just runs the program once and then waits for my input.
If anyone could point the error here i would be very greatefull :)/>


function run()
	local time = os.time()
	local signal = false
	if time>17 or time<6 then
		signal = true
	end
	rs.setOutput("left", signal)
end
function quit()
while true  do
input = io.read()
if input == "stop" then
rs.setOutput("left", false)
break
end
end
end
print ("type 'stop' to quit program")
parallel.waitForAll(quit, run)

oryginal loop code
Spoilerfunction run()
–your whole looping code
end
function quit()
while true do
input = io.read()
if input == "stop" then
break
end
end
end
parallel.waitForAll(quit, run)

my oryginal code without exit possibilty (works!)
Spoilerwhile true do
local time = os.time()
local signal = false
if time>17 or time<6 then
signal = true
end
rs.setOutput("left", signal)
os.queueEvent("randomEvent")
os.pullEvent()
end
Lyqyd #2
Posted 07 June 2013 - 04:30 PM
Split into new topic.
Engineer #3
Posted 07 June 2013 - 04:47 PM
probably you are missing signal = false at the very top. Because the signal variable is defined when time > 17 or time < 6.
And to answer your question, you are only checking run function once. If you put this in an infinite loop, I think it will do what you desire. (An example of an infinite loop:

while true do --# start loop, true is always true so it is infinite
    -- content
end --# end loop

An optional optimized code. Please ask if you dont know how this works, I dont kow if you had any programming background.

local function run()
   while true do
     local time = os.clock()
     if time > 17 or time < 6 then
       redstone.setOutput("left", true )
     end
   end
end

local function quit()
   while true do
      local input = read() --# CC has an own read function
      if input == "stop" then
         break
      end
   end
end

print("Type 'stop' to quit")
parrallel.waitForAny( quit, run )
Edited on 07 June 2013 - 02:50 PM