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

Help with making a stone button run a program

Started by Kegulf, 06 August 2013 - 12:43 AM
Kegulf #1
Posted 06 August 2013 - 02:43 AM
Ok, so I started playing around with CC today in Tekkit Classic, I understand some of it, but not much (keep this in mind if you want to reply :P/>)
Running CraftOS 1.3.

I've made a redpower frame bridge that extends over a moat, I have a computer on one side, and I want to execute the program with a button from the other side.
I have connected a red alloy wire to the left side of the computer and I have taken it underneath the moat and over to the other side and connected it to a stone button (yes it gets the signal from the button to the computer).

I have my program "Extend" which is built up like this:

Spoiler

-- Text and apperances
term.clear()
term.setCursorPos(1, 1)
print("Extending the Bridge!")
sleep "1.5"
term.clear()
term.setCursorPos(1, 1)

--Extending bridge 6 blocks using White as the extender wire

redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)

-- Some more text
print("The Bridge has been Extended!")

-- 6 seconds to get over
sleep "6"

term.clear()
term.setCursorPos(1, 1)

-- Pulling the bridge back
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"

Yes, I know there is probably a better way to do this, but I'm a noob, and this works xD

So! Onto my question, is there any way to write a easy script to make the computer run this program when a stone button is pressed?

Thanks in advance,

Kegulf :)/>
jay5476 #2
Posted 07 August 2013 - 02:02 AM
im guessing it is just a normal redstone signal so you could use
 if rs.getInput(side) then shell.run("extend") end 
assuming your using extend to make the bridge
Dave-ee Jones #3
Posted 07 August 2013 - 04:59 AM
im guessing it is just a normal redstone signal so you could use
 if rs.getInput(side) then shell.run("extend") end 
assuming your using extend to make the bridge

What he said.
The side would, obviously, be the side the button is on.
immibis #4
Posted 07 August 2013 - 05:12 AM

while true do -- repeat forever

  -- as long as the redstone signal is off, wait for a redstone signal to change.
  -- os.pullEvent doesn't return which signal changed, that's why we need to use rs.getInput to see if the
  -- signal is on yet - we don't want to run the program if an unrelated redstone signal changed.
  -- by waiting for a redstone signal change with os.pullEvent, instead of using sleep, we reduce server load.
  while not rs.getInput("input side") do
    os.pullEvent("redstone")
  end

  -- run the other program
  shell.run("Extend")
end
civilwargeeky #5
Posted 07 August 2013 - 01:45 PM
Hi. I added a few things to your code to make it better (hopefully). First off, instead of saying the same thing over and over again, you can use a "for" loop. You can see how I used them to shorten and simplify the code substantially. You can find examples here: http://www.computerc...info/wiki/Loops. The "while true do" at the top makes it run as long as the computer is turned on. The repeat loop I used (which is what actually solves your problem) will do whatever is inside first (wait for a change in redstone), and then evaluate the condition at the end. If the condition is true, then the loop will end, and go on to the rest of your code. Hope that helps :)/>

--Make it run forever
while true do

repeat
  os.pullEvent("redstone")
until rs.getInput("side") --Wait until redstone input is true

-- Text and apperances
term.clear()
term.setCursorPos(1, 1)
print("Extending the Bridge!")
sleep "1.5"
term.clear()
term.setCursorPos(1, 1)

--Extending bridge 6 blocks using White as the extender wire
for i=1, 6 do
redstone.setBundledOutput("back", 1)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
end

-- Some more text
print("The Bridge has been Extended!")

-- 6 seconds to get over
sleep "6"

term.clear()
term.setCursorPos(1, 1)

-- Pulling the bridge back
for i=1, 6 do
redstone.setBundledOutput("back", 2)
sleep "0.6"
redstone.setBundledOutput("back", 0)
sleep "0.6"
end

end --While end
Kingdaro #6
Posted 07 August 2013 - 03:16 PM
A part of me wonders why nobody pointed out that the OP is using strings for sleep instead of numbers.

sleep "0.6"   --# wrong
sleep(0.6)    --# right
civilwargeeky #7
Posted 07 August 2013 - 04:45 PM
A part of me wonders why nobody pointed out that the OP is using strings for sleep instead of numbers.

sleep "0.6"   --# wrong
sleep(0.6)	--# right
Right. I was wondering about that too. I don't know if it would even work. The proper usage would indeed be "sleep(0.6)"
Kegulf #8
Posted 07 August 2013 - 10:22 PM
im guessing it is just a normal redstone signal so you could use
 if rs.getInput(side) then shell.run("extend") end 
assuming your using extend to make the bridge

And to make it work I would just have to make a program called button, and c&p this? :P/> I tried that, did not work, might be CraftOS 1.3s fault?

A part of me wonders why nobody pointed out that the OP is using strings for sleep instead of numbers.
 sleep "0.6" --# wrong sleep(0.6) --# right 
:P/>

It works :)/>
Kegulf #9
Posted 07 August 2013 - 10:23 PM
Hi. I added a few things to your code to make it better (hopefully). First off, instead of saying the same thing over and over again, you can use a "for" loop. You can see how I used them to shorten and simplify the code substantially. You can find examples here: http://www.computerc...info/wiki/Loops. The "while true do" at the top makes it run as long as the computer is turned on. The repeat loop I used (which is what actually solves your problem) will do whatever is inside first (wait for a change in redstone), and then evaluate the condition at the end. If the condition is true, then the loop will end, and go on to the rest of your code. Hope that helps :)/>/>/>
 --Make it run forever while true do repeat os.pullEvent("redstone") until rs.getInput("side") --Wait until redstone input is true -- Text and apperances term.clear() term.setCursorPos(1, 1) print("Extending the Bridge!") sleep "1.5" term.clear() term.setCursorPos(1, 1) --Extending bridge 6 blocks using White as the extender wire for i=1, 6 do redstone.setBundledOutput("back", 1) sleep "0.6" redstone.setBundledOutput("back", 0) sleep "0.6" end -- Some more text print("The Bridge has been Extended!") -- 6 seconds to get over sleep "6" term.clear() term.setCursorPos(1, 1) -- Pulling the bridge back for i=1, 6 do redstone.setBundledOutput("back", 2) sleep "0.6" redstone.setBundledOutput("back", 0) sleep "0.6" end end --While end 

Haha Yeah, as I said, I just started in CC :P/> Thanks, looks awesome :D/>
One problem :o/> Doesn't work, wrote it as you have done here.
Tried to run it, got:
bios:206:[string "extend"]:6: 'end' expected (to close 'while' at line 2)
tried to put in end after the first section, did not work :P/>
civilwargeeky #10
Posted 08 August 2013 - 02:48 AM
Hi. I added a few things to your code to make it better (hopefully). First off, instead of saying the same thing over and over again, you can use a "for" loop. You can see how I used them to shorten and simplify the code substantially. You can find examples here: http://www.computerc...info/wiki/Loops. The "while true do" at the top makes it run as long as the computer is turned on. The repeat loop I used (which is what actually solves your problem) will do whatever is inside first (wait for a change in redstone), and then evaluate the condition at the end. If the condition is true, then the loop will end, and go on to the rest of your code. Hope that helps :)/>/>/>
 --Make it run forever while true do repeat os.pullEvent("redstone") until rs.getInput("side") --Wait until redstone input is true -- Text and apperances term.clear() term.setCursorPos(1, 1) print("Extending the Bridge!") sleep "1.5" term.clear() term.setCursorPos(1, 1) --Extending bridge 6 blocks using White as the extender wire for i=1, 6 do redstone.setBundledOutput("back", 1) sleep "0.6" redstone.setBundledOutput("back", 0) sleep "0.6" end -- Some more text print("The Bridge has been Extended!") -- 6 seconds to get over sleep "6" term.clear() term.setCursorPos(1, 1) -- Pulling the bridge back for i=1, 6 do redstone.setBundledOutput("back", 2) sleep "0.6" redstone.setBundledOutput("back", 0) sleep "0.6" end end --While end 

Haha Yeah, as I said, I just started in CC :P/> Thanks, looks awesome :D/>
One problem :o/> Doesn't work, wrote it as you have done here.
Tried to run it, got:
bios:206:[string "extend"]:6: 'end' expected (to close 'while' at line 2)
tried to put in end after the first section, did not work :P/>
Hmm. That's odd. Notepad++ says all the blocks are properly closed. Visual inspection confirms… Are you sure you put an "end" after both the "for" blocks? Did you add any if statements that might need "end"s? I'm pretty sure the problem is not on my end.
Kegulf #11
Posted 08 August 2013 - 12:05 PM
Haha Yeah, as I said, I just started in CC :P/> Thanks, looks awesome :D/>
One problem :o/> Doesn't work, wrote it as you have done here.
Tried to run it, got:
bios:206:[string "extend"]:6: 'end' expected (to close 'while' at line 2)
tried to put in end after the first section, did not work :P/>

Hmm. That's odd. Notepad++ says all the blocks are properly closed. Visual inspection confirms… Are you sure you put an "end" after both the "for" blocks? Did you add any if statements that might need "end"s? I'm pretty sure the problem is not on my end.

As I said, I wrote it just as you did, checked it 4 times xD Is there any way I can use NP++ to write my code into the game? That would be AWESOME!
The whole writing in the CC computer is annoying and unpractical :P/>
(funny coincidence, my name is Odd, Norwegian name :P/>)