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

[LUA] [Question] Bundled RS Detection & States

Started by Evis, 28 March 2012 - 09:17 AM
Evis #1
Posted 28 March 2012 - 11:17 AM
Alright so I've adapted two samples provided on these forums for an ice machine process used to cool down my IC reactors. I'm not looking for anyone to do any of it for me, but I'm wondering if I can get help on three things; (code below)

I've used the built in help, but when I try to detect, it breaks the script. I guess I need some help with the formatting and/or some direction.

1.

I have two files, the start-up runs through the various components, and prints reports. Right now, they are simply dummy text, but my goal is to display states based on true or false; e.g. Detect a color input on the bundled cable and print either text and/or a value.

So lets say I have 3 power storage units that output when full. On power-up, the test will check the bundled cable, red wire to see if a RS signal is true.

How could I pull that variable and include in print("Unit 1 is [state]" where state is FULL or CHARGING. I get if bundled cable red is true, then print(…) but all my efforts have failed.

is it;


if rs.getBundledInput(back,red) == true then
print("Red Unit FULL")
else
print("Red Unit Charging")

2.

Once it is detected, I'm completely lost on read.io; I wanted to give the option to start ice production if supply is low. So if my counter reads 64, it should throw the "Low Supply" and ask if you'd like to turn on the Snow Collector Array. (SCA)

Do you want to start ice collection now? y to start | n to continue...

The SCA is controlled by a second program (See below) called control, also adopted. I tried to set the output with;


if rs.getBundledInput(back,blue) == true then
rs.setBundledOutput(back,green) == true


If I got the above to work, I imagine the control program would reset the wire state.

Lastly, 3 ..

If I wanted to make some momentary switches, or choose to send a timed pulse to a specific wire, how would I format it?

I'm still learning timers and loops and I believe that's the way here.

Any help and direction is greatly appreciated!

Code

startup


--EvBios System Check .v03
--Adapted from original by unknown

--Ready the Terminal
local function clearScreen()
  term.clear()
  term.setCursorPos(1, 1)
end

--Start the Built In Test
print("Power On BIT Started:")
sleep(3)
term.setCursorPos(1, 5)
print("EvBIOS v.03")
sleep(3)
term.setCursorPos(1, 7)
print("Bundled Cable Detected")
sleep(2)
term.setCursorPos(1, 9)
print("Loading Management Profile...")
sleep(3)
term.setCursorPos(31, 9)
print("COMPLETE")
sleep(3)
clearScreen()

--Check the Asset System
print("Checking Asset System..|")
sleep(3)
term.setCursorPos(1, 3)
print("Ice System Reports Low Supply!")
sleep(1)
term.setCursorPos(1, 5)
print("Compressors ONLINE")
sleep(1)
term.setCursorPos(1, 7)
print("All Filters ONLINE")
sleep(1)
term.setCursorPos(1, 9)
print("Collectors ONLINE")
sleep(1)
term.setCursorPos(1, 11)
print("Transposers ONLINE")
sleep(6)
clearScreen()

--Check the Blutricity and Sorting System
print("Checking Sorting System../")
sleep(3)
term.setCursorPos(1, 3)
print("Blutricity Grid ONLINE")
sleep(2)
term.setCursorPos(1, 5)
print("Sorter ONLINE")
sleep(2)
term.setCursorPos(1, 7)
print("Sorter AI Reports:")
sleep(2)
term.setCursorPos(20, 7)
print("READY")
sleep(4)
clearScreen()

--Check the EU Power System
print("Checking Power Subsystem..--")
sleep(3)
term.setCursorPos(1, 3)
print("MV Power Grid Nominal")
sleep(3)
term.setCursorPos(1, 5)
print("Local MFSU Capacity 100%")
sleep(1)
term.setCursorPos(1, 7)
print("Local MFE Capacity 100%")
sleep(1)
term.setCursorPos(1, 9)
print("Local Unit Capacity 100%")
sleep(1)
term.setCursorPos(1, 11)
print("All Power Systems  READY")
sleep(4)
clearScreen()

--Check the other stuff
print("Checking System Integration..\\")  -- Here is the escaped backslash.
sleep(2)
term.setCursorPos(1, 3)
print("Talkback OK")
sleep(2)
term.setCursorPos(1, 5)
print("CPU READY")
sleep(2)
term.setCursorPos(1, 7)
print("Lag Detected: Downloading More RAM..")
sleep(4)
term.setCursorPos(1, 9)
print("RAM Download COMPLETE")
sleep(3)
clearScreen()

--Check redwire connections
print("Checking Redwire Network..|")
sleep(4)
term.setCursorPos(1, 3)
print("System Integrity 100%")
sleep(2)
term.setCursorPos(1, 5)
print("Removing Safeties")
sleep(2)
term.setCursorPos(1, 7)
print("Disabling Homicidal AI")
sleep(3)
clearScreen()

--BIT Success
print("All Reactor Support System READY")
sleep(1)
term.setCursorPos(1, 3)
print("Operators Must Wear Eye Protection")
sleep(2)
term.setCursorPos(1, 5)
print("Do Not Send Ice to Reactors; OFFLINE")
sleep(4)
term.setCursorPos(1, 8)
print("Loading Interface... Please wait...")
sleep(3)

--Load the control program
shell.run("control")

control


-- Reactor Coolant Control v.03 by Evis
-- Original by Shif

shell.run("clear");

-- Creates 3 arrays, s to store the constant of each color, o to store the toggles and sn to store the names of the toggles
s = {colors.white, colors.red, colors.lightBlue, colors.yellow, colors.blue, colors.black};
o = {"off", "off", "off", "off", "off", "off"};
sn = {"Extract Ice Blocks", "External Power Disconnect", "Supply Snow to Compressors", "Start Snow Collector Array", "Supply Ice to Reactors", "Local MFSU Disconnect"};

-- some placeholders for later
cs = 1;
co = "off";

-- master color variable, the sum of all the color constants of services that are on will be stored here
master = 0;
while true do
shell.run("clear");

-- reset master
master = 6;
print("/// Reactor Coolant Support Controller v.03 ///\n");

-- for loop to check if a toggle is on and if it is then it adds the color value to the master variable
for i=1, 6 do

-- the status variable will store "on" or "off", i use it to convert "on" to "on " so the text doesnt move
status = o[i];
if(o[i] == "on") then
status = status.." ";
master = master + s[i];
end

-- this is what turns each cable on, based on the value of the master variable it decides which cables to turn on
redstone.setBundledOutput("back", master);

-- this prints each of the services and their states
print(i.." - "..status.." - "..sn[i]);
end

-- here it asks for what service to toggle, if the number is not in the list it asks again
sValid = true;
while sValid do
write("\nSelect a service to toggle: ");
cs = read();

-- this took me some painful moments of troubleshooting, if you dont convert what you read() and then try to use it to point inside an array it will point to different data, example: not calling tonumber(cs) and then doing i[cs] would do i["3"] instead of i[3]
cs = tonumber(cs);
if((cs == 1) or (cs == 2) or (cs == 3) or (cs == 4) or (cs == 5) or (cs == 6)) then
sValid = false;
else
print("Invalid Service");
end
end

--here it toggles the service, if its on it turns it off and vice versa
co = o[cs];
if(co == "on") then
o[cs] = "off";
else
o[cs] = "on";
end
end

-- here is where we print detection events
--
Advert #2
Posted 28 March 2012 - 11:49 AM
1) You need to use colors.red instead of red. See help colors.
You also don't need to check if something is true in a if statement, it'll always pass if the value presented is not false or nil (a numeric value of 0 will pass).
You also didn't close your if statement with and end, but I think you know that :o/>/>.

2) You can get input from the user by using read():


print("Do you want ice cream?")
local userinput = read()
if userinput == "y" then
 print("Too bad.")
else
 print("OK, no delicious ice cream for you, then.")
end


3) Here's an example: http://pastie.org/3684718

You could also use parallel.waitForAny() so you can use sleep() and read(); you'll juts have to turn the loop into a function, and remove tick timer, then run your function inside parallel.waitForAny():

parallel.waitForAny(loopFunction, doStuff)

I've commented it, but if you have any questions, feel free to ask.

Some thoughts: You could turn all those checks into functions, so the code doesn't get messy when you actually impliment them:


local checks = {}
checks[1] = function() -- First check to be run; or you could do local function checkLazors() if you wanted to, but then you can't loop them in order at the end, unless you add it to the table :o/>/>
 -- checks lazors here
 -- Return if successful first
 return true, "Lazors online" -- return the text since we don't actually check the stuff
end

local function checkPower()
 -- checks power here
 return true, "Power Online"
end
checks[2] = checkPower -- showing you how we'd do it this way, if you wanted to use checkPower somewhere else, too.

function checks.randomCheckThatDoesntNeedToBeInOrder()
 -- check stuff here, this check doesn't need to be in order :)/>/>
 return false, "Misc check failed! <somemoredetail>"
end

for k, f in pairs(checks) do
  print("Running check: " .. (type(k) == "number" and "#" .. k or k)) -- Print #1 or randomCheck...
  local success, text = f() -- run the check function
  if success then
   print("Success: " .. text)
  else -- check failed, abort!
   print("Failed: " .. text)
   print("Aborting!")
   error("Safety checks failed") -- exit using error()
  end
end

-- do stuff, safety checks passed below this point
Edited by
Evis #3
Posted 28 March 2012 - 12:39 PM
Thanks a very much for the help and examples. I've made some progress on #1 and #2 (below)

I'm running into a problem when I have two states to check. It won't detect them after adding the second state, which I've commented out. I suppose it needs to check them every so often.

Going to read for #3, and experiment with the functions which seems much better.

Thanks again!

Control

--EvBios System Check .v06
--Adapted from original by Shif
--Ready the Terminal
local function clearScreen()
  term.clear()
  term.setCursorPos(1, 1)
end
term.clear()
--Start the Built In Test
term.setCursorPos(1, 1)
print("Power On BIT Started:")
sleep(1)
-- Check for detection of control signal
sleep(3)
if rs.getBundledInput("back") == colors.cyan then
term.setCursorPos(2, 3)
print("Signal DETECTED")
else
print("Signal Not Detected")
sleep(2)
end
--Continue BIT
term.setCursorPos(1, 5)
print("EvBIOS v.04")
sleep(1)
term.setCursorPos(1, 7)
print("Bundled Cable Detected")
sleep(2)
term.setCursorPos(1, 9)
print("Loading Management Profile...")
sleep(2)
term.setCursorPos(31, 9)
print("COMPLETE")
sleep(2)
term.clear()

--Check the Asset System
term.setCursorPos(1, 1)
print("Checking Asset System..|")
sleep(1)
term.setCursorPos(1, 3)
print("Ice System Reports Low Supply!")
sleep(1)
--Prompt an input to start ice
print("Do you want to start ice collection? Y or N")
print("Press X to Cancel")
local userinput = read()
if userinput == "y" then
print("STARTED")
rs.setBundledOutput("back", colors.lightBlue)
sleep(2)
term.clear()
elseif
userinput == "x" then
print("Terminal Shutting Down")
sleep(2)
os.shutdown()
else
print("SKIPPED")
sleep(2)
term.clear()
end
term.setCursorPos(1, 1)
print("Checking Asset System..|")
sleep(1)
term.setCursorPos(1, 5)
print("Compressors ONLINE")
sleep(2)
term.setCursorPos(1, 7)
print("All Filters ONLINE")
sleep(2)
term.setCursorPos(1, 9)
print("Collectors ONLINE")
sleep(2)
term.setCursorPos(1, 11)
print("Transposers ONLINE")
sleep(1)
term.setCursorPos(1, 13)
print("Asset System ONLINE")
sleep(3)
clearScreen()
--Check the Blutricity and Sorting System
print("Checking Sorting System&#46;&#46;/")
sleep(2)
term.setCursorPos(1, 3)
print("Blutricity Grid ONLINE")
sleep(2)
term.setCursorPos(1, 5)
print("Sorter ONLINE")
sleep(2)
term.setCursorPos(1, 7)
print("Sorter AI Reports:")
sleep(2)
term.setCursorPos(20, 7)
print("READY")
sleep(4)
clearScreen()
--Check the EU Power System
print("Checking Power Subsystem..--")
sleep(2)
term.setCursorPos(1, 3)
print("MV Power Grid Nominal")
sleep(2)
term.setCursorPos(1, 5)
print("Local MFSU Status: ")
-- Check for a full mfsu
--sleep(3)
--if rs.getBundledInput("back") == colors.magenta then
-- term.setCursorPos(22, 5)
-- print("FULL")
--else
-- term.setCursorPos(22, 5)
-- print("CHARGING")
-- sleep(3)
--end

sleep(1)
term.setCursorPos(1, 7)
print("Local MFE Capacity 100%")
sleep(2)
term.setCursorPos(1, 9)
print("Local Unit Capacity 100%")
sleep(1)
term.setCursorPos(1, 11)
print("All Power Systems  READY")
sleep(4)
clearScreen()
--Check the other stuff
print("Checking System Integration..")  -- Here is the escaped backslash.
sleep(2)
term.setCursorPos(1, 3)
print("Talkback OK")
sleep(2)
term.setCursorPos(1, 5)
print("CPU READY")
sleep(2)
term.setCursorPos(1, 7)
print("Lag Detected: Downloading More RAM..")
sleep(4)
term.setCursorPos(1, 9)
print("RAM Download COMPLETE")
sleep(3)
clearScreen()
--Check redwire connections
print("Checking Redwire Network..|")
sleep(4)
term.setCursorPos(1, 3)
print("System Integrity 100%")
sleep(2)
term.setCursorPos(1, 5)
print("Removing Safeties")
sleep(2)
term.setCursorPos(1, 7)
print("Disabling Homicidal AI")
sleep(3)
clearScreen()
--BIT Success
print("All Reactor Support System READY")
sleep(1)
term.setCursorPos(1, 3)
print("Operators Must Wear Eye Protection")
sleep(2)
term.setCursorPos(1, 5)
print("Do Not Send Ice to Reactors; OFFLINE")
sleep(2)
term.setCursorPos(1, 8)
print("Loading Interface... Please wait...")
sleep(4)
--Load the control program
shell.run("control")
Evis #4
Posted 29 March 2012 - 09:44 AM
Just wanted to say thanks to you I have a much better understanding of LUA!

I'm still having one minor issue where; if two dissimilar color bundled cables are true; the script won't detect either of them.

Do I need a pullEvent? or loop?
Advert #5
Posted 29 March 2012 - 10:05 AM
Just wanted to say thanks to you I have a much better understanding of LUA!

I'm still having one minor issue where; if two dissimilar color bundled cables are true; the script won't detect either of them.

Do I need a pullEvent? or loop?

I'm not quite sure what you mean; do you mean two different colored bundled cables?

If so, as long as they are both connected to the computer, and you're checking the right sides, it should work.
Evis #6
Posted 29 March 2012 - 10:22 AM
Just wanted to say thanks to you I have a much better understanding of LUA!

I'm still having one minor issue where; if two dissimilar color bundled cables are true; the script won't detect either of them.

Do I need a pullEvent? or loop?

I'm not quite sure what you mean; do you mean two different colored bundled cables?

If so, as long as they are both connected to the computer, and you're checking the right sides, it should work.

I mean I had magenta and cyan both true, and the code didn't detect. It did before sporatically, when there was only one wire connected and true.

I rewrote it as;


if rs.getBundledInput("back") == colors.white then
term.setCursorPos(36, 3)
print("RUN ")
else
term.setCursorPos(36, 3)
print("STBY")
sleep(0.5)
end

This is the only 'check' for that input and it doesn't see the white wire as true.

Strange behavior, or am I missing something?

I found this snip in another thread;


os.pullEvent('redstone')
If rs.getBundledInput(side) == color then
Evis #7
Posted 29 March 2012 - 10:30 AM
Ah, was this patched?

http://www.computercraft.info/forums2/index.php?/topic/416-bundles-cables-do-not-generate-events/
Luanub #8
Posted 29 March 2012 - 11:11 AM
Try replacing


if rs.getBundledInput("back") == colors.white then


with this

if redstone.testBundledInput("back", colors.white ) == true then

I use something very similar in one of my programs and I have not had any issues with it.
Evis #9
Posted 30 March 2012 - 06:03 AM
Try replacing


if rs.getBundledInput("back") == colors.white then


with this

if redstone.testBundledInput("back", colors.white ) == true then

I use something very similar in one of my programs and I have not had any issues with it.

That did the trick! Thanks!