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

Combustion Engine Control

Started by Mauroq, 15 November 2012 - 01:25 PM
Mauroq #1
Posted 15 November 2012 - 02:25 PM
Hello,

i just started with computercraft, and LUA in general, and thought i could use some help on a project I'm working on.
For the record i use computercraft in conjunction with other mods from Tekkit.

Since Combustion Engines have a tendency of blowing up i thought it a smart idea to build a system for it.
I want computercraft to make a bundled wire, but at the end a red wire, turn on if the engine is not "Red"
for this I use ccSensor and a sensor close to the Combustion Engine.

this is my code so far, I don't get any errors but it doesn't show anything on my screen so i would
like you guys to maybe take a look into it.


Mauroq

os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")

ctrl=sensors.getController()
_sensor="Sensor"

function getTemp()

target=sensors.getAvailableTargetsforProbe(ctrl,_sensor,"CombustionEngine")
data=sensors.getSensorReadingAsTable(ctrl,_sensor,target[1],"CombustionEngine")
return data[14]
end

function enableWire(color)
local currentSet = rs.getBundledOutput("back")
if not colors.test(currentSet, color) then
currentSet = colors.combine(currentSet, color)
rs.setBundledOutput("back", currentSet)
end
end

function disableWire(color)
local currentSet = rs.getBundledOutput("back")
if colors.test(currentSet, color) then
currentSet = colors.subtract(currentSet, color)
rs.setBundledOutput("back", currentSet)
end
end

while true do
if getTemp()=="Red" then
enableWire(colors.red)
sleep(30)
else
disableWire(colors.red)
end
end

While true do
local evt, arg = os.pullEvent()
local mon = peripheral.wrap("top")
mon.setCursorPos(1,3)

if getTemp()=="Blue" then
mon.write("Engine 1: Blue")

elseif getTemp()=="Green" then
mon.write("Engine 1: Green")

elseif getTemp()=="Yellow" then
mon.write("Engine 1: Yellow")
end
end


remiX #2
Posted 16 November 2012 - 12:01 AM
It doesn't show any text because you have two while true do loops, and the first while true do loop is NEVER broken therefor your second one will never begin.

PS: The second one you typed 'While', it must be 'while'
Mauroq #3
Posted 16 November 2012 - 05:55 AM
I now have this,
while true do
local evt, arg = os.pullEvent()
local mon = peripheral.wrap("top")
mon.setCursorPos(1,3)
if getTemp()=="Red" then
enableWire(colors.red)
sleep(30)
else
disableWire(colors.red)
sleep(1)
end
break
elseif getTemp()=="Blue" then
mon.write("Engine 1: Blue")
elseif getTemp()=="Green" then
mon.write("Engine 1: Green")
elseif getTemp()=="Yellow" then
mon.write("Engine 1: Yellow")
end

but i get an end expected error to close the while line
remiX #4
Posted 16 November 2012 - 06:20 AM
Ok well there are quite a few problems in that code:

1. You have os.pullEvent() - But what is it waiting for? Therefore: This code will never go passed that line.
2. The if statement must go like this:

if getTemp() == "Red" then
    -- Code if red
elseif getTemp() == "Blue" then
    -- Code if blue
elseif getTemp() == "Green" then
    -- Code if green
elseif getTemp() == "Yellow" then
    -- Code if yellow
else    -- Else must always be at the end
    -- Code if it is anything else
end    -- You end it after all elseif's and else, not after the else

So your while true do loop should be something like this:

while true do
    local evt, arg = os.pullEvent()   -- Not sure what event you want though?
    local mon = peripheral.wrap("top")
    mon.setCursorPos(1,3)
    if getTemp()=="Red" then
        enableWire(colors.red)
        sleep(30)
    elseif getTemp()=="Blue" then
        mon.write("Engine 1: Blue")
    elseif getTemp()=="Green" then
        mon.write("Engine 1: Green")
    elseif getTemp()=="Yellow" then
        mon.write("Engine 1: Yellow")
    else
        disableWire(colors.red)
        sleep(10)
	    break  -- This will stop the loop
    end
end
MaHuJa #5
Posted 17 November 2012 - 03:34 AM
1. You have os.pullEvent() - But what is it waiting for? Therefore: This code will never go passed that line.

Since there's no filter, a redstone clock would do it :)/>/>

I suspect this has to do with the whole "program gets shut down after 10 seconds without a pullevent" thing. One thing that's not explained well there is the many options you have for fulfilling that condition. A sleep(5) would go a very long way here.

What does the red wire do, anyway? Is there a not gate from the red wire to the engine?
I would get rid of the not gate and do the direct control: red wire on, engine on. If the computer gets unloaded because there's no players near and there are no chunkloaders, that means the engine won't be running while there's nothing to check on it and keep it safe.

So your while true do loop should be something like this:
disableWire needs to be called from all the other conditions. The only reason I can see for entering the last else block would be the engine, or possibly sensor, being broken. (Grieeper goes boom?)


Also, I would suggest reading the value once, storing it in a variable, and checking that variable in each if, rather than calling the function several times in a row.