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

Help me with this program?

Started by lewisholcombe, 08 March 2013 - 11:11 AM
lewisholcombe #1
Posted 08 March 2013 - 12:11 PM
Hey guys.
So basically what i want to do is have a computer and a monitor hooked up to some levers.
Read this program then i can explain

mon=peripheral.wrap("back")
mon.clear()
mon.setCursorPos(1,1)
mon.setTextScale(2)
print"Coal Generators: *"
print"Geothermal Generators: *"
print"Wind Mill: *"
print"Water Mill: *"
print"Solar Panel: *"
print"HV Solar Array: *"
print"MV Solar Array: *"
print"LV Solar Array: *"
print"Nuclear Reactor: *"
print"Water Strainer: *"
print"Oil Fabricator: *"
So im running tekkit and what i want is when i power my say coal generators by a lever it will send a redstone signal to the computer as-well and change the * to on/off depending if its on or off.
Does anyone have the correct program to do this?
I understand the program might have to be a,b,c,d ect instead of *.
Thanks.
oeed #2
Posted 08 March 2013 - 12:48 PM
First of all, you need to add brackets around the quotes for the print function.
So like this:

    print("Coal Generators: *")
ChunLing #3
Posted 08 March 2013 - 03:10 PM
Add an infinite loop that pulls a redstone event, then checks the state on the relevant side of the computer, and depending on what it is, changes the relevant line on the monitor.

Since you have more than five redstone states to check, you'll need to use bundled cable. The functions for checking redstone states are in the redstone API (also referenced as rs). Pull events using os.pullEvent() (in the os API).
theoriginalbit #4
Posted 08 March 2013 - 03:30 PM
First of all, you need to add brackets around the quotes for the print function.
You actually don't.

print"Hello World!"
That is completely valid.
oeed #5
Posted 08 March 2013 - 04:13 PM
First of all, you need to add brackets around the quotes for the print function.
You actually don't.

print"Hello World!"
That is completely valid.

Oh really, I never knew that. You learn something new everyday.
lewisholcombe #6
Posted 09 March 2013 - 04:37 AM
Add an infinite loop that pulls a redstone event, then checks the state on the relevant side of the computer, and depending on what it is, changes the relevant line on the monitor.

Since you have more than five redstone states to check, you'll need to use bundled cable. The functions for checking redstone states are in the redstone API (also referenced as rs). Pull events using os.pullEvent() (in the os API).
I don't get what you mean.
Would you be able to place it in the program?
ChunLing #7
Posted 09 March 2013 - 07:42 AM
I guess, though really the point is for you to understand these things well enough to write them yourself. Since you presumably want the loop to run indefinitely, we'll use a while true do loop. The first thing we do inside the loop is call os.pullEvent("redstone") , so that the program waits for a change in the redstone inputs. Then we check the redstone inputs using the redstone API functions. This part is somewhat dependent on your setup, for now I'll just pretend you have a bundled cable split from the connection between the switches and whatnot, and red is the Coal generators. We use the result of rs.getBundledInput("left") to decide whether to write "On" or "Off".
while true do
    mon.setCursorPos(1,1)
    os.pullEvent("redstone")
    local rsnpt = rs.getBundledInput("left")
    print("Coal Generators:"..(colors.test(rsnpt,colors.red) and " On" or " Off"))
    print("Geothermal Generators: "..(colors.test(rsnpt,colors.green) and " On" or " Off"))
    print("Wind Mill: "..(colors.test(rsnpt,colors.blue) and " On" or " Off"))
end
You can add additional print statements of the same form…okay, I'll just do a couple more examples above. See, all that changes is the name of the device and the wire color.

I guess I should explain. What I'm doing is concatenating either " On" or " Off" onto the end of the string to be printed. I decide which by setting up a logic operation that emulates something called a ternary operator (if you know what that is you don't need to read the explanation, though there are some differences between Lua logic and ternary). (X and Y) evaluates to X if it is false or nil, and Y otherwise. So something like colors.test(rsnpt,colors.red) and " On" will result in false if the red wire wasn't powered, and " On" otherwise. Similarly, (X or Y) evaluates to X if it is NOT false or nil, and Y otherwise. So if colors.test(rsnpt,colors.red) was true, and thus the and evaluated to " On", then the or will still evaluate to " On". If the test were false, and thus the previous and evaluated to false, the or will evaluate to " Off".

It should be noted that the and operation has precedence over the or operation, so where parenthesis are not used to specify a different order of evaluation, ands will be evaluated first.
latemail #8
Posted 20 March 2013 - 12:59 AM
Hi,

I did exakt the same thing but using computer and rednet-messages.

pro: no wiring, cheap, easy to change (positions)

con: you need to save the status of each "farm", otherwise you have to setup all after each reboot