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

Computercraft

Started by skatercooper, 16 September 2014 - 11:05 PM
skatercooper #1
Posted 17 September 2014 - 01:05 AM
Hello all its my first time here but i have a problem that i cannot solve.
I need to make a program that runs all the time, and it needs to first check if its getting a redstone signal, then it needs to choose a random integer between 1-3 and output a redstone signal in one of three ways. Sorry for the bad description and grammar, tell me if you need me to get a picture of what i am trying to do….
Bomb Bloke #2
Posted 17 September 2014 - 02:47 AM
local inputSide = "back"                                            -- Or whatever.
local outputSide = {"front","right","left"}                         -- Or whatever.
local outputPulseDuration = 1                                       -- Or whatever.

while true do                                                       -- Start a loop that repeats indefinitely.
	if rs.getInput(inputSide) then                              -- If we're getting input,
		local currentOutput = math.random(#outputSide)      -- ... choose a random number between one and the number of entries in outputSide...
		rs.setOutput(outputSide[currentOutput],true)        -- ... enable the output which corresponds to that number in the outputSide table...
		sleep(outputPulseDuration)                          -- ... wait a while...
		rs.setOutput(outputSide[currentOutput],false)       -- ... then turn it off again.
	
		while rs.getInput(inputSide) do                     -- Then, so long as we're STILL getting input,
			os.pullEvent("redstone")                    -- ... wait for changes in redstone input.
		end
	end

	os.pullEvent("redstone")                                    -- Wait for any change in input (eg the signal turning back on).
end                                                                 -- Go back up and repeat the loop.
Edited on 17 September 2014 - 12:50 AM
blipman17 #3
Posted 17 September 2014 - 10:31 PM
name the program startup, so it would automatically start if you open the computer and stay on until minecraft restart.