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

Toggle-able redstone using button api

Started by Bob3104558, 22 October 2013 - 02:07 PM
Bob3104558 #1
Posted 22 October 2013 - 04:07 PM
Title: Toggle-able redstone using button api

I am a newb when it comes to computers, basically I am using dw20's button api and his demo program. I want to be able to push a button which will toggle my t5 soul cage spawner hooked to another computer.

I already have the receiving computer set so when it gets the text "on" it will not output redstone to the cage and when it gets the word "off" it will output redstone thus turning off the spawner. Basically I want a function so that when i click the button it will toggle the redstone on the other computer, so sending the words "on" or "off" which sounds like a bad way to do it. I haven't been able to find any other programs that would do this. Any help would be greatly appreciated.

I would even be fine with a program that has 2 buttons 1 for off and 1 for on as long as its easy to tell whether the spawner is on or not
ATMunn #2
Posted 22 October 2013 - 08:51 PM
You can do something like this:

local state = 0
function toggleOutput()
local sendID, msg, distance = rednet.receive()
if msg == "toggle" and state == 0 then
rs.setOutput("right", true)
state = 1
end
if msg == "toggle" and state == 1 then
rs.setOutput("right", false)
state = 0
end
end


This should make it so that when the computer is sent the message "toggle" it will toggle the redstone signal to the right (you can change that to any side you want). Make sure that "local state = 0" goes BEFORE "function toggleOutput", or else it will always be on and never toggle.

Hope this helped!
campicus #3
Posted 22 October 2013 - 09:25 PM
*false not flase
ATMunn #4
Posted 23 October 2013 - 01:16 PM
*false not flase
Oops, typo!
jay5476 #5
Posted 24 October 2013 - 12:12 AM
well dw20's button API is useable, but its kinda frowned upon so i would like to bring your attention to this
http://www.computerc...__fromsearch__1
it is Lyqyd's button API and is very flexible
ATMunn #6
Posted 24 October 2013 - 03:43 PM
well dw20's button API is useable, but its kinda frowned upon so i would like to bring your attention to this
http://www.computerc...__fromsearch__1
it is Lyqyd's button API and is very flexible
I was telling you how to make the rs output toggle I wasn't talking anything about the button API you were using. Also, did you have one computer by the spawner and one by the monitor? If you did, I can give you a new example.
TheOddByte #7
Posted 24 October 2013 - 04:16 PM
Would something like this be useful?

Rednet Receiveing Program


local hID = 0 --# The id you want to receive from( I added this so you don't get messages from other computers that can change the output )


--# Opening all modems sides so you can use rednet
	for _,side in pairs(rs.getSides()) do[/size]
		if peripheral.getType(side) == "modem" then
			rednet.open(side)
		end
	end

local function changeOutput()

	local id, msg
	repeat
		id, msg = rednet.receive()
	until id == hID

	if msg == "turn_off" then
		rs.setOutput("right", true)

	elseif msg == "turn_on" then
		rs.setOutput("right", false)
	end
end


--# Running the function in a loop as an example
while true do
	changeOutput()
end

If you want to know how to use rednet or something else then just visit the ComputerCraft wiki, There's lots of stuff you can learn from there! ;)/>
And if you are new to Lua then visit this link to start learning! :D/>