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

[lua][question]

Started by spardha, 02 May 2012 - 08:03 AM
spardha #1
Posted 02 May 2012 - 10:03 AM
I'm very new to LUA and coding in general, my aim was to have a simple program turn off the mass fabricator in the day so my MFE can recharge and then turn back on at night with a simple redstone signal.

I'm currently using Technic ssp, which is on 1.1 otherwise i think i would have tried the CC sensor add-on.

if os.time() < 17 then
redstone.setOutput("right", true )
elseif os.time() > 17 then
redstone.setOutput("right", false )
end

Also im just testing with a light at the moment so the output might not be the right way around. :)/>/> I did think that this would have to run continuously and tried to add


when true do
if os.time() < 17 then
redstone.setOutput("right", true )
elseif os.time() > 17 then
redstone.setOutput("right", false )
end
end

But that didn't work, but i don't have the error i think it was waiting to long, I'll have to update with the error when i'm home

Thanks

EDIT: Forgot to put title "derp"
Edited on 02 May 2012 - 08:10 AM
Luanub #2
Posted 02 May 2012 - 10:57 AM
Its probably a yield for to long without getting reply? If so then its fairly simple to fix, you just need to add an sleep. The length doesn't matter infact you can do sleep(0).

I personally however would use an alarm for this as you will not need a constant running loop, the program will halt and wait for the timer to go off. Here's an example..

while true do
local sTime = os.time()  -- this records the current time
if sTime < 17 then
   os.setAlarm( 17 )  -- this will set an alarm that will trigger an event at 17 minecraft time
   os.pullEvent("alarm") -- this halts and waits for an "alarm" event, there are other types of events you can use this for
   rs.setOutput("right", true)  -- turn on after 17
elseif sTime > 17 then
   os.setAlarm ( 6 ) -- i think the sun is up by then, i changed to 6 sun is up by then
   os.pullEvent("alarm")
   rs.setOutput("right", false) -- turn off at 7
end
end

With this code you will probably want to check the time at startup and set the rs state the the appropriate state for the time of day but should be cleaner over all.

btw I have not tested the script but it should work.
Edited on 02 May 2012 - 09:12 AM
spardha #3
Posted 02 May 2012 - 11:02 AM
Thanks for the reply and the code luanub.

Its gone straight over my head but i will test and let you know how it goes.
Luanub #4
Posted 02 May 2012 - 11:12 AM
I edited the code and put in some comments explaining, Hope it helps.
spardha #5
Posted 03 May 2012 - 08:58 AM
It didn't work for some reason… didn't get an error but the light that i was using for testing didnt come on when it was dark?
Luanub #6
Posted 03 May 2012 - 09:39 AM
The if logic needed to be a little more robust, this should work.


while true do
sTime = os.time()
if sTime < 17 and sTime > 6 then
os.setAlarm( 17 )
os.pullEvent("alarm")
rs.setOutput("right", true)
elseif sTime > 17 or sTime < 6 then
os.setAlarm( 6 )
os.pullEvent("alarm")
rs.setOutput("right", false )
end
end


And to make it so it sets the correct state at the start of the program add this to the beginning of the code

local sTime = os.time()
if sTime < 17 and sTime > 6 then
rs.setOutput("right", false )
else
rs.setOutput("right", true)
end
Edited on 03 May 2012 - 07:42 AM