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

IC2 Reactor Control Help (OpenPeripherals)

Started by couchpatatochip1, 31 March 2018 - 04:08 PM
couchpatatochip1 #1
Posted 31 March 2018 - 06:08 PM
Hi. I've been working on this bare-bones reactor control that shuts off the reactor when the heat value exceeds 7000. I'm very new to programming and I am only familiar with the basics and I can't seem to get my code to work. My setup is an advanced computer with a wired modem in the back connecting it to a nuclear reactor with a wired modem.
nuke = peripheral.wrap("back")
heat = nuke.callRemote("nuclear_reactor_0","getHeat")
while true do
    while heat < 7000 do
	   rs.setOutput("right",true)
    sleep(0)
	 end
end

The sleep(0) was added to remove a "too long without yielding" error; however, even though there isn't an error the code does not seem to function properly. For example, when I begin the program and the reactor heat is 0 and then run it until the heat is at 7000, the computer will still output a redstone signal. Similarly, if I start the program when the reactor heat is above 7000 and I cool the reactor to a heat level below 7000, then the redstone signal will not change and there will be no output. I believe this has something to do with the "too long without yielding" error, but I do not know how to fix the error without using the sleep command. Thank you for your patience!
SquidDev #2
Posted 31 March 2018 - 09:40 PM
The primary issue here is that the heat is fetched outside the loop, meaning you're only ever checking that the reactor was more than 7000 when the program started. It's fairly trivial to fix this though: one just needs to move that inside the loop:


local nuke = peripheral.wrap("nuclear_reactor_0")
while true do
    while nuke.getHeat() < 7000 do
        rs.setOutput("right",true)
        sleep(0)
    end
end

Note that this probably isn't quite what you want: this'll set the redstone output to true whilst the heat is less than 7000. You probably want something like:

local nuke = peripheral.wrap("nuclear_reactor_0")
while true do
    --# Could technically do sleep(0), but this is more server friendly
    while nuke.getHeat() < 7000 do sleep(0.1) end
    rs.setOutput("right",true)

    --# TODO: Handle cooling and turning redstone off
end
couchpatatochip1 #3
Posted 31 March 2018 - 09:48 PM
Thank you! I had a feeling that it was only checking the heat once but I had no idea how to go about handling it.
theoriginalbit #4
Posted 31 March 2018 - 09:49 PM
In addition to what SquidDev said, changing a Redstone signal is also persistent change; so once you've turned it on it will stay on until you turn it off. This is also why you're not seeing it change when the heat does. For this you can use the condition you've used to check if the heat is ok to update the Redstone state, which would look like this:


local nuke = peripheral.wrap("nuclear_reactor_0")
while true do
    local tooHot = nuke.getHeat() >= 7000 --# we've inverted the condition here so that when it is above 7000 the value is 'true' resulting in the Redstone signal turning on
    rs.setOutput("right", tooHot)
    sleep(0.1)
end