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

[LUA] computer controlled pistons

Started by Minithra, 11 March 2013 - 08:56 PM
Minithra #1
Posted 11 March 2013 - 09:56 PM
I want to use a computer to control three different sets of pistons for a blaze farm, and to start a cycle at the push of a button.

I can't get it to work. I am not good at any sort of programming.

This is what I have so far, cobbled together from the net… and probably bad:


function gate()
rs.setBundledOutput("left", colors.yellow)
end
function wall()
rs.setBundledOutput("left", colors.black)
end

function crushers()
rs.setBundledOutput("left", colors.red)
sleep(14)
end
function listener()
   while true do
		  if rs.getInput("back") then
			    gate()
                sleep(0.2)
                wall()
                sleep(0.2)
                crushers()
		  end
		  sleep(0)
   end
end
theoriginalbit #2
Posted 11 March 2013 - 10:02 PM
I can't get it to work. I am not good at any sort of programming.
What exactly can't you get to work? Is there an error? If so what does it say? Is it not behaving how you would expect? If so what is it doing and what is it meant to do?

EDIT: Looked at the code again and saw it right away, idk how I missed it the first time… its because you have all your program in functions, and you never call that function listener to start the program

Here is some code I put together that accomplishes the task in less code and with less lag on your Minecraft world, please read the comments, and if you have any questions just ask:
Spoiler

local function output(col) -- a function is a good way to collect together common tasks
  rs.setBundledOutput('left', col) -- output the colour
  sleep(0.2) -- wait
  rs.setBundledOutput('left', 0) -- clear the output, i suspect this may have been causing one of the 'unexpected behaviours'
end

while true do
  os.pullEvent('redstone') -- wait here until the computer gets a redstone signal
  if rs.getInput('back') then -- if the redstone signal was the one we wanted
	output(colors.yellow) -- gate
	output(colors.black) -- wall
	output(colors.red) -- crushers
  end
end
Edited on 11 March 2013 - 09:10 PM
Minithra #3
Posted 11 March 2013 - 10:14 PM
It wasn't doing anything at all :D/>

Thank you very much. I can understand what it does :)/>

One slight issue - I need slight different timings on the pistons. It's why I had them to different colors.

Where can I introduce delays? Ideally, I'd have gate a half-second before wall, and the crusher a half second after the wall, with the crushers on for a few seconds. Not sure how many yet. Between the outputs? Would that be okay?


    output(colors.yellow) -- gate
    sleep(0.5)
    output(colors.black) -- wall
    sleep(0.5)
    output(colors.red) -- crushers
    sleep(10)

Like that?
theoriginalbit #4
Posted 11 March 2013 - 10:18 PM
It wasn't doing anything at all :D/>
Yeh ok so it would be because you did not call the listener function

you could add another parameter to the output function and make changes like so

local function output(col, delay)
  rs.setBundledOutput('left', col)
  sleep(delay) -- wait
  rs.setBundledOutput('left', 0)
end
then you would use it like so

output(colors.yellow, 0.5)
output(colors.black, 0.5)
output(colors.red, 10)
Minithra #5
Posted 11 March 2013 - 10:27 PM
Thank you. That looks good.

But it doesn't work. Still doesn't do anything.

I tried a red alloy wire to its back, connected to a lever or button, and I tried a lever right on the back of the computer. Nothing.

I have the bundled wires out of its left side, splitting into yellow/black/red a few blocks later, ending in play red alloy wire on the pistons. It should work…
Kryptanyte #6
Posted 11 March 2013 - 10:29 PM
Is it still throwing an error, and also whats your updated code?
Minithra #7
Posted 11 March 2013 - 10:33 PM
The code is what TheOriginalBIT posted, including the second post, with the delays.

I was probably doing something really dumb. I changed it to output to front, and it's working now. Left side of the computer is the side to to the left if I'm standing on it, facing the same direction the monitor is, right?

Just need to rework the timings, because now the gate and wall retracts instantly, which isn't what I wanted :)/>

Thank you very much for your help!
immibis #8
Posted 11 March 2013 - 10:33 PM
Don't use sleep(0) and constantly check for something, unless you want to cause server lag.
Use something like this instead of sleep(0):

while not rs.getInput('back') do
os.pullEvent("redstone")
end
theoriginalbit #9
Posted 11 March 2013 - 10:33 PM
I have the bundled wires out of its left side, splitting into yellow/black/red a few blocks later, ending in play red alloy wire on the pistons. It should work…
Are you SURE you have it on the left?

EDIT: just saw your reply. the sides are when you are looking at the front of the computer, and its YOUR left is its left.
Minithra #10
Posted 11 March 2013 - 10:40 PM
Yeah, dumb mistake :D/>
Minithra #11
Posted 11 March 2013 - 11:09 PM
Hmm… problem.

How do I keep everything on for as long as I want to? At the moment, each different set of pistons is only on for their set time, then they stop. What I need is for everything to stop at the same time, after the last set is done.

I tried different times, nothing seems to be doing what I want.
theoriginalbit #12
Posted 11 March 2013 - 11:17 PM
You can use rs.getBundledInput and colors.combine to do what is needed


local function output(col, delay)
  local curr = rs.getBundledInput('left') -- get the current output
  rs.setBundledOutput('left', colors.combine(curr, col)) -- combine the current with the new one
  sleep(delay) -- wait
  -- we dont want to clear it here
end

while true do
  os.pullEvent('redstone') -- wait here until the computer gets a redstone signal
  if rs.getInput('back') then -- if the redstone signal was the one we wanted
	output(colors.yellow, 0.5)
	output(colors.black, 0.5)
	output(colors.red, 10)
	rs.setBundledOutput('left', 0) -- now lets clear it
  end
end
Minithra #13
Posted 11 March 2013 - 11:36 PM
That does the same thing. When the next set of pistons is called, the previous one resets.

I think I'll just remove the output function and have outputs on three sides in the if clause. Maybe it doesn't like having to keep multiple colors going at the same time :/
theoriginalbit #14
Posted 11 March 2013 - 11:40 PM
uhh did you copy paste it? or type it out? if you copy pasted that idk why it wouldn't work, it should only reset after it has finished each loop.
Minithra #15
Posted 12 March 2013 - 12:24 AM
Copied and pasted it.

This is what I'm using now, and it does what I want it to:

http://pastebin.com/bApjC7FB
immibis #16
Posted 12 March 2013 - 01:23 AM
When you call rs.setBundledOutput you set all the colours at once. To use multiple colours add them together (with +, they're just numbers)