Skip to content

Code Examples

Philipp edited this page Feb 4, 2022 · 10 revisions

Introduction

If you want to run the signals via some kind of signal box, it is a good idea to write your own programme. You can find more information here. You can find our OpenComputers tutorial here. To adjust a signal, you must enter the ID for the type and state. And below you will find some examples. Note that these are only simple examples and not fully functional signal boxes.

Examples

The simplest programme is to adjust the signal only.

--Variable
component = require("component")
types = component.signalcontroller.getSupportedSignalTypes()

--Part to change the signal
component.signalcontroller.changeSignal(types.stopsignal,1)

If you have multiple signals, you have to work with the address of the controller. The following code can be used for this.

--Variable
component = require("component")

Sig1 = component.proxy(component.get("bee9d1c1-7298-4adb-a79f-8ba8ac5dc115")
Sig2 = component.proxy(component.get("bb3") --The short form is also possible
types1 = Sig1.getSupportedSignalTypes()
types2 = Sig2.getSupportedSignalTypes()

--Part to change the signal
Sig1.changeSignal(types1.stopsignal,2)
Sig2.changeSignal(types2.stopsignal,3)

With this code you can adjust defined signals by pushing a lever. In addition, the programme remains in a loop until the programme is terminated by pressing CTRL+C. You also need a Redstone I/O block for the setup.

--Variable
component = require("component")
event = require("event")

Sig1 = component.proxy(component.get("bee9d1c1-7298-4adb-a79f-8ba8ac5dc115"))
Sig2 = component.proxy(component.get("bb3")) --The short form is also possible
Sig3 = component.proxy(component.get("17e"))
types1 = Sig1.getSupportedSignalTypes()
types2 = Sig2.getSupportedSignalTypes()
types3 = Sig3.getSupportedSignalTypes()

while true do --loop
    local event_name = event.pullMultiple("redstone_changed", "interrupted")
    if event_name == "redstone_changed" then --change signals
        Sig1.changeSignal(types1.stopsignal,2)
        Sig1.changeSignal(types1.zs3,3)
        Sig2.changeSignal(types2.stopsignal,1)
        Sig2.changeSignal(types2.zs1,1)
        Sig3.changeSignal(types3.zs3,12)
    elseif event_name == "interrupted" then --close programme
        break
    end
end

With the following programme you can adjust different signal aspects from the screen and end the programme. All you have to do is click on the corresponding spot on the screen.

--Variable
component = require("component")
event = require("event")
term = require("term")

Sig1 = component.proxy(component.get("bee9d1c1-7298-4adb-a79f-8ba8ac5dc115"))
types1 = Sig1.getSupportedSignalTypes()

--screen layout
term.clear()
term.setCursor(1,1)
term.write("x HP0")
term.setCursor(1,2)
term.write("x HP1")
term.setCursor(1,3)
term.write("x HP2")
term.setCursor(1,5)
term.write("x Close programme")

while true do --loop
    local event_name, _, x, y = event.pullMultiple("touch", "interrupted")
    if event_name == "touch" and x == 1 and y == 1 then --Change signal to HP0
        Sig1.changeSignal(types1.stopsignal,1)
    elseif event_name == "touch" and x == 1 and y == 2 then --Change signal to HP1
        Sig1.changeSignal(types1.stopsignal,2)
    elseif event_name == "touch" and x == 1 and y == 3 then --Change signal to HP2
        Sig1.changeSignal(types1.stopsignal,3)
    elseif event_name == "touch" and x == 1 and y == 5 then  --close programme
        term.clear()
        break
    elseif event_name == "interrupted" then  --close programme with CTRL+C
        term.clear()
        break
    end
end

More coming (maybe) soon :)

Clone this wiki locally