5 posts
Posted 18 April 2012 - 05:46 AM
Hey guys,
This LUA is killing me (new to it).
What I'm trying to do:toggle between outputting a signal left and right for 5min. unles it's getting a redstone input from somewhere (preferabally front/back).
So, no input -> switching left and right every 5 min
when it gets a signal it should stop outputting.
How I'm (failing) to do it:
while true do
local sEvent, param1 = os.pullEvent()
if sEvent == "redstone" and rs.getInput("back") ~= true then
rs.setOuput("left", true)
sleep(300)
rs.setOutput("left", false)
rs.setOutput("right", true)
sleep(300)
rs.setOutput("right", false)
end
end
Now, I scrapped this together from various parts of the forum not fully understanding what I'm doing. So, there could be parts missing I should have copied with or should have renamed.
Any help would be appreciated.
Thanks.
1111 posts
Location
Portland OR
Posted 18 April 2012 - 05:59 AM
So the os.pullEvent() will cause the program to sleep until the event happens. In order to get it to work as written I would do the following.
while true do
if rs.getInput("back") ~= true then
rs.setOuput("left", true)
sleep(300)
rs.setOutput("left", false)
rs.setOutput("right", true)
sleep(300)
rs.setOutput("right", false)
end
sleep(0) -- will prevent a failure to yield error
end
This will not run the redstone commands if there is a redstone signal on the back, but will continue to run the loop checking for it. Once there is no signal it will then start to run the rs commands.
5 posts
Posted 18 April 2012 - 06:05 AM
Works like a charm.
Plus I figured out I forgot to put a piece of alloy wire on the back of the computer (there was one behind it, just not on)
TYVM.
5 posts
Posted 18 April 2012 - 06:38 AM
actually. I think I wasn't as clear with what I wanted.
Is there a way to stop all output the moment it gets the redstone power from the back?
also, killing the proccess and stopping all output when someone presses any key.
1111 posts
Location
Portland OR
Posted 18 April 2012 - 07:36 AM
So you want the redstone to "pause" the operation, and to have an option for the user to press any key to stop/exit the program?
I'm currently at work but will try to put something together for you when I get home.
5 posts
Posted 18 April 2012 - 07:47 AM
yes, to maybe give you a better picture of what i'm doing.
I have 2 combustion engine generators running on just fuel (no cooling). I'm using wireless redstone to power each engine in a generator.
I'm going to use the script to toggle between the generators renerating power (5min power, 5min cooling down (takes 9:30 to blow up)).
though, in case of emergency or maintaince or anything. i want to be able to 'not power' both sets at once, and not having to wait a cycle (since that could be too late :)/>/>)).
before i was trying to do it with just the redpower2 time and toggle switch. but, apart from actually coding it (which i hope I'll get the hang of the more i do it), using a computer would be much easier.
1111 posts
Location
Portland OR
Posted 18 April 2012 - 11:31 AM
Okay here is something to play with. Ended up being easier then I thought, just changed the sleep's to a timer and tweaked the logic some to eliminate some possible bugs. Let me know if you have any questions or need any help.
if rs.getInput("back") ~= true then
while true do
term.clear()
term.setCursorPos(1,3)
print ("Press any key or flip the switch to turn the engines off")
if rs.getOutput("left") then
rs.setOutput("left", false)
rs.setOutput("right", true)
elseif rs.getOutput("right") then
rs.setOutput("right", false)
rs.setOutput("left", true)
else
rs.setOutput("left", true)
end
local timer = os.startTimer(300) -- start the 5 min wait timer
repeat
local evt, p1 = os.pullEvent()
if evt == "key" and p1 ~= 1 or evt == "redstone" then -- check to see if event was something other then the timer
rs.setOutput("left", false)
rs.setOutput("right", false)
p1 = timer
while true do
term.clear()
term.setCursorPos(1,3)
print ("Press any key or flip the switch to turn the engines on")
local evt, p1 = os.pullEvent()
if evt == "key" and p1 ~= 1 or evt == "redstone" then
break
end
end
end
until p1 == timer
end
end
5 posts
Posted 18 April 2012 - 07:33 PM
You have once again made it work. thank you. :)/>/>
7 posts
Posted 04 February 2013 - 03:49 PM
Okay here is something to play with. Ended up being easier then I thought, just changed the sleep's to a timer and tweaked the logic some to eliminate some possible bugs. Let me know if you have any questions or need any help.
if rs.getInput("back") ~= true then
while true do
term.clear()
term.setCursorPos(1,3)
print ("Press any key or flip the switch to turn the engines off")
if rs.getOutput("left") then
rs.setOutput("left", false)
rs.setOutput("right", true)
elseif rs.getOutput("right") then
rs.setOutput("right", false)
rs.setOutput("left", true)
else
rs.setOutput("left", true)
end
local timer = os.startTimer(300) -- start the 5 min wait timer
repeat
local evt, p1 = os.pullEvent()
if evt == "key" and p1 ~= 1 or evt == "redstone" then -- check to see if event was something other then the timer
rs.setOutput("left", false)
rs.setOutput("right", false)
p1 = timer
while true do
term.clear()
term.setCursorPos(1,3)
print ("Press any key or flip the switch to turn the engines on")
local evt, p1 = os.pullEvent()
if evt == "key" and p1 ~= 1 or evt == "redstone" then
break
end
end
end
until p1 == timer
end
end
How could I implement the "press any key" feature into other programs?
2088 posts
Location
South Africa
Posted 04 February 2013 - 05:30 PM
It's good that you used the search feature, but you really shouldn't revive 10 month old threads.
You should rather post a thread for help
497 posts
Location
The Big Apple, NY
Posted 05 February 2013 - 01:20 AM
It's good that you used the search feature, but you really shouldn't revive 10 month old threads.
You should rather post a thread for help
there is the problem do we tell them to search or to not revive…..hopefully both…
1688 posts
Location
'MURICA
Posted 05 February 2013 - 02:25 AM
How could I implement the "press any key" feature into other programs?
If it's a separate program, you could just run a parallel.
parallel.waitForAny(
function() shell.run('someprogram') end,
function()
repeat
local _, k = os.pullEvent('key')
until k == keys.backspace
end
)
To end "someprogram" when you press the backspace key.
4 posts
Posted 05 February 2013 - 08:33 AM
ok so i would like a program so if computer detect a rs input from bottom it will print text on monitor is this posible?
1688 posts
Location
'MURICA
Posted 05 February 2013 - 09:33 AM
Yes. However it's good to actually try to make it before asking us to do it for you. Plenty of information on the wiki.
7 posts
Posted 05 February 2013 - 12:00 PM
It's good that you used the search feature, but you really shouldn't revive 10 month old threads.
You should rather post a thread for help
Oh, shoot. Didn't realize this was so old.
7 posts
Posted 05 February 2013 - 12:21 PM
How could I implement the "press any key" feature into other programs?
If it's a separate program, you could just run a parallel.
parallel.waitForAny(
function() shell.run('someprogram') end,
function()
repeat
local _, k = os.pullEvent('key')
until k == keys.backspace
end
)
To end "someprogram" when you press the backspace key.
Not to continue the revival, but could you help with this?
I'm unable to start a new thread for some unknown reason, and I've been modifying SethBling's "Flintify" program to my liking, but I can't figure out how to end it when enter is pressed. This is the modified code so far:
local args = { ... }
print("Beginning to flintify. This may take a while...")
print("Press ENTER to stop the flintification process.")
if #args >= 1 then
local slot = tonumber(args[1])
if slot and slot >= 1 and slot <= 16 then
turtle.select(slot)
end
end
while true do
if not turtle.placeUp() then
if not turtle.digUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
print("The flintification process is complete.")
break
end
end
end
end
end
end
parallel.waitForAny(
function() shell.run("flintify") end,
function()
repeat
local event, key = os.pullEvent("key")
until key == keys.enter
end
)
end
if not turtle.digUp() then
if not turtle.placeUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
if not turtle.attackUp() then
print("The flintification process is complete.")
break
end
end
end
end
end
end
parallel.waitForAny(
function() shell.run("flintify") end,
function()
repeat
local event, key = os.pullEvent("key")
until key == keys.enter
end
)
end
end
It runs without error, but it doesn't break the place -> mine loop when I press enter, like it should. Could you explain why?
4 posts
Posted 08 February 2013 - 04:26 AM
ok so anyone know how to do this:
while true do
rs.input("bottom", true) then
priint (" whatever i want to print here)
sleep(5)
mon.clear
—-please make it have big writting too!
7508 posts
Location
Australia
Posted 08 February 2013 - 04:42 AM
-snip-
Please do not post on an old topic with a new question. Well actually don't post on an old dead topic all together. I know you are new and can't post, we hear that reason all the time, but if you read the bold text in
this stickie, which is forum guidelines then you would have been lead to
this stickie, which is in this very "Ask a Pro" section which you posted!
4 posts
Posted 09 February 2013 - 04:31 AM
-snip-
Please do not post on an old topic with a new question. Well actually don't post on an old dead topic all together. I know you are new and can't post, we hear that reason all the time, but if you read the bold text in
this stickie, which is forum guidelines then you would have been lead to
this stickie, which is in this very "Ask a Pro" section which you posted!
yes but i cant post a new topic :L
7508 posts
Location
Australia
Posted 09 February 2013 - 05:06 AM
yes but i cant post a new topic :L
Thats why
this exists!!! Again you need to read. that link its the second link I posted previously! It is there for new members who cant make a topic.