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

[lua][error] using os.time for an automatic door

Started by migas, 10 January 2013 - 04:39 AM
migas #1
Posted 10 January 2013 - 05:39 AM
so i wanted to make an automatic door based on the time, i wanted it to open during the day and close it at night time

here is a script "money" made me in the CC IRC

http://pastebin.com/vTfiS9wH

the script returns me "bad argument, doube expected, got nil" in the second line

then another person (oddstr13) told me to do this: http://pastebin.com/QzvnFYY3 but the computer stays on sleep forever :C

what i wanted the script to do:
at 6 am minecraft time turn on the redstone power on,
leave it on until 6 pm,
at 6 pm turn it off
make a loop so it opens and closes the redstone everyday

also if possible i wanted the program not to print any thing (or clear the screen) to type "automatic door is on"
remiX #2
Posted 10 January 2013 - 08:26 AM
It happens because the number doesn't go up in one minute each second, sometimes it goes up two minutes in a tick and it will skip then check of it.
And you needed to check if it's "8:00" not "08:00"

I changed it a bit to get the first time and then check :

term.clear()
while true do -- infinite loop
local time = os.time()
time = textutils.formatTime(time, true)
term.setCursorPos(1,1)
term.clearLine()
print(time)
hour = tonumber(string.sub(time, 1, string.find(time, ":") - 1)) -- finds the number before the colon (:)/>
if hour >= 8 and hour < 20 then -- if the hour is between 8 AM and 8 PM
print("On")
redstone.setOutput("back", true) -- if the redstone is at the back of computer etc
elseif hour >= 20 and hour < 8 then -- if the hour is between 8 PM and 8 AM
print("Off")
redstone.setOutput("back", false) -- if the redstone is at the back of computer etc
end
sleep(1)
end

edit: Sigh, indentation won't stay!
migas #3
Posted 11 January 2013 - 04:51 AM
thank you very much, its really great, it not only stays on during the day, but i don't need to wait until the day starts

there is just that problem it will not turn off at night.., i will test more later, i just tested from the day to the night (wanted to test a full cycle but i don't have time now

if any one can change the code so it stops outputting the redstone at night it will be perfect

once more, thank you very much for trying and get half way there
Lyqyd #4
Posted 11 January 2013 - 04:53 AM
The line:

elseif hour >= 20 and hour < 8 then

Change and to or.
migas #5
Posted 11 January 2013 - 10:09 AM
thank you lyqyd, it seems it works perfectly with just a small bug

when it changes from off state to on, the screen shows "onf"

can this be easly fixed? the code seems perfectly, it prints the time, and the state wich is really good,

now i can put it on my ssp house to don't worry about the door or presure plates (which i hate the noise from them)

before this i had used a solar painel from IC2 but with a computer is more fun :D/>

really, thank all of you who helped <3
TheArchitect #6
Posted 11 January 2013 - 10:17 AM
It's just displaying the last "f" from "off", so if instead of writing "on" you write "on " (appending a space to the end) should clear that f out of the way.
remiX #7
Posted 11 January 2013 - 10:35 AM
Change
print("On")
to
print("On ")
with the space
migas #8
Posted 11 January 2013 - 10:35 AM
It's just displaying the last "f" from "off", so if instead of writing "on" you write "on " (appending a space to the end) should clear that f out of the way.

yeah it's the easy solution and i am gonna use it, i was searching in google how to make a code to clear a specific line ( so i could had it before the "on")
Lyqyd #9
Posted 11 January 2013 - 10:53 AM
Use the ComputerCraft wiki to look up ComputerCraft-specific questions. The function you were looking for was term.clearLine()
migas #10
Posted 11 January 2013 - 12:05 PM
Use the ComputerCraft wiki to look up ComputerCraft-specific questions. The function you were looking for was term.clearLine()

thank you, i really appreciate all the code people did to me, can i post the final code on the programs section and give credit to all the people who worked on the code?