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

Need help making a program for my Coke Ovens.

Started by 18tyweslow, 09 May 2015 - 04:23 PM
18tyweslow #1
Posted 09 May 2015 - 06:23 PM
Hey everyone!

I was wondering if anyone knew a way I could receive feedback from my coke ovens using computercraft monitors.

I'm using light detecting fixtures to detect when the oven is on, and that sends a redstone signal to a computer. What would I have to do to make that computer recognize that the redstone is active, write "oven active" on the monitor and when it's not getting a signal, write "oven inactive"? I'll have 6 of these in total on a monitor. I'd like to use advanced monitors too, so the text could have a green box around it when active, and red when inactive. I'd be using a 3x3 space for the monitors, by the way.

I was thinking about using bundled cables, so I can have all the ovens on one cable. I've been trying to make a program that does this, but I can't figure it out. I need it to update when an oven turns on/off also.

Can someone help me out?
Bomb Bloke #2
Posted 09 May 2015 - 11:07 PM
Start with the basics first; hop into the Lua console and play around with rs.getInput(side) and rs.testBundledInput(side, colour). You should be able to get these to return true/false based on what your ovens are doing.

Once you're familiar with that, rig up a loop that checks the state of each oven, and outputs your data to your monitor. You can make the loop pause until there's a redstone state change by calling os.pullEvent("redstone").

Note that not all types of bundled cable will work with your computer, and the ones which do vary depending on the mod versions in use.
Edited on 09 May 2015 - 09:08 PM
18tyweslow #3
Posted 10 May 2015 - 12:21 AM
Start with the basics first; hop into the Lua console and play around with rs.getInput(side) and rs.testBundledInput(side, colour). You should be able to get these to return true/false based on what your ovens are doing.

Once you're familiar with that, rig up a loop that checks the state of each oven, and outputs your data to your monitor. You can make the loop pause until there's a redstone state change by calling os.pullEvent("redstone").

Note that not all types of bundled cable will work with your computer, and the ones which do vary depending on the mod versions in use.

Alright, sweet. So far, I have this

while true do
local event = os.pullEvent("redstone")
local Red=rs.testBundledInput("back",colors.red)
local Yellow=rs.testBundledInput("back",colors.yellow)
local Orange=rs.testBundledInput("back",colors.orange)
local Blue=rs.testBundledInput("back",colors.blue)
if Red then
print "Red"
elseif Yellow then
print "Yellow"
elseif Orange then
print "Orange"
elseif Blue then
shell.run"clear"
end
end

How do I get the computer to display a message based on if the signal is false or true?

I got the computer to see if the signal is on, but it only happens once. I'm unsure how to make the program keep checking for a signal. I think I can achieve that through the use of boolean? I'm not familiar with how to use it though. D; I'm sorry if this sounds stupid. I haven't used ComputerCraft since 1.2.5. :P/>
Edited on 10 May 2015 - 02:48 AM
Bomb Bloke #4
Posted 10 May 2015 - 12:13 PM
The code you've posted will currently repeat the loop once for every time the redstone input changes (assuming you've got a compatible cable hooked up, though if not, it won't work at all) - you wouldn't want it to happen more often than that, so that aspect appears fine as-is.

Likewise, you've already rigged it to display messages based on the incoming signal; though one thing you might want to change there is your use of "elseif" - currently your script will either print Red or Yellow or Orange (or clear the display), but it'll never do more than one of these things per iteration.
18tyweslow #5
Posted 10 May 2015 - 02:54 PM
The code you've posted will currently repeat the loop once for every time the redstone input changes (assuming you've got a compatible cable hooked up, though if not, it won't work at all) - you wouldn't want it to happen more often than that, so that aspect appears fine as-is.

Likewise, you've already rigged it to display messages based on the incoming signal; though one thing you might want to change there is your use of "elseif" - currently your script will either print Red or Yellow or Orange (or clear the display), but it'll never do more than one of these things per iteration.

Great, I'm glad to see I'm on the right track. I've got a few more questions. How do I get the computer to display a message when the specific cable loses a redstone signal? As of now, when it gets a red signal, it displays "Red". But when I deactivate red, it displays nothing. How would I get it to display "Red off" etc?
HPWebcamAble #6
Posted 10 May 2015 - 09:56 PM
How do I get the computer to display a message when the specific cable loses a redstone signal? As of now, when it gets a red signal, it displays "Red". But when I deactivate red, it displays nothing. How would I get it to display "Red off" etc?

Well first, you can use code tags on the forums: [.code] (Without the .)

The redstone event doesn't tell you WHAT changed, you just know something did. Your code needs to compare the current values to the old values, like this:


--# Get the initial states
local Red=rs.testBundledInput("back",colors.red)
local Yellow=rs.testBundledInput("back",colors.yellow)
local Orange=rs.testBundledInput("back",colors.orange)
local Blue=rs.testBundledInput("back",colors.blue)

while true do
  os.pullEvent("redstone") --# You don't need to store the return value of this, it doesn't actually return anything for redstone events

  if rs.testBundledInput("back",colors.red) ~= Red then -- # Did red change?
    print("Red is now "..rs.testBundledInput("back",colors.red)) --# Print the new value
    Red = rs.testBundledInput("back",colors.red) --# Update the old one
  end

  if rs.testBundledInput("back",colors.yellow) ~= Yellow then --# Did yellow change?
    print("Yellow is now "..rs.testBundledInput("back",colors.yellow))
    rs.testBundledInput("back",colors.yellow)
  end

  --# Also check Orange
  --# Also check Blue

end

You can make this code much shorter with tables, here is the reference manual if you are interested in looking into them:
http://lua-users.org/wiki/TablesTutorial
18tyweslow #7
Posted 10 May 2015 - 10:48 PM
How do I get the computer to display a message when the specific cable loses a redstone signal? As of now, when it gets a red signal, it displays "Red". But when I deactivate red, it displays nothing. How would I get it to display "Red off" etc?

Well first, you can use code tags on the forums: [.code] (Without the .)

The redstone event doesn't tell you WHAT changed, you just know something did. Your code needs to compare the current values to the old values, like this:


--# Get the initial states
local Red=rs.testBundledInput("back",colors.red)
local Yellow=rs.testBundledInput("back",colors.yellow)
local Orange=rs.testBundledInput("back",colors.orange)
local Blue=rs.testBundledInput("back",colors.blue)

while true do
  os.pullEvent("redstone") --# You don't need to store the return value of this, it doesn't actually return anything for redstone events

  if rs.testBundledInput("back",colors.red) ~= Red then -- # Did red change?
	print("Red is now "..rs.testBundledInput("back",colors.red)) --# Print the new value
	Red = rs.testBundledInput("back",colors.red) --# Update the old one
  end

  if rs.testBundledInput("back",colors.yellow) ~= Yellow then --# Did yellow change?
	print("Yellow is now "..rs.testBundledInput("back",colors.yellow))
	rs.testBundledInput("back",colors.yellow)
  end

  --# Also check Orange
  --# Also check Blue

end

You can make this code much shorter with tables, here is the reference manual if you are interested in looking into them:
http://lua-users.org.../TablesTutorial

I tried using your code, but whenever I activated the red or yellow wires, it throws out "ovens:16: attempt to concatenate string and boolean"

What does that mean?
Lupus590 #8
Posted 10 May 2015 - 11:14 PM
"ovens:16: attempt to concatenate string and boolean"

What does that mean?

"ovens" is the name of the script that caused the error, 16 in the line number of the error.
concatenate is combining strings ("test ".."string" –> "test string")
the error is that you are trying to combine a string with a boolean

try this:

--#do not use the var names in lua, they may overwrite global tables
--#which can cause strange errors
string = "example"
bool = true
newString = string..tostring(bool)

--# if the above errors with "attempt to concatenate string and nil" then try this

--#ignore the /> i have no idea what keeps adding them
local function boolToString(B)/>
  if type(B)/> ~= "boolean" then
	return ""
  end

  if b then
	return "true"
  else
	return "false"
  end


end

Edited on 10 May 2015 - 09:18 PM
18tyweslow #9
Posted 10 May 2015 - 11:22 PM
"ovens:16: attempt to concatenate string and boolean"

What does that mean?

"ovens" is the name of the script that caused the error, 16 in the line number of the error.
concatenate is combining strings ("test ".."string" –> "test string")
the error is that you are trying to combine a string with a boolean

try this:

--#do not use the var names in lua, they may overwrite global tables
--#which can cause strange errors
string = "example"
bool = true
newString = string..tostring(bool)

--# if the above errors with "attempt to concatenate string and nil" then try this

--#ignore the /> i have no idea what keeps adding them
local function boolToString(B)/>/>/>
  if type(B)/>/>/> ~= "boolean" then
	return ""
  end

  if b then
	return "true"
  else
	return "false"
  end


end



This is line 16 of the code:

print("Yellow is now "..rs.testBundledInput("back",colors.yellow))

How do I fix that to not cause the error? I'm sorry for being so bad at this. I'm trying to learn. D;
Edited on 10 May 2015 - 09:23 PM
Lupus590 #10
Posted 10 May 2015 - 11:31 PM
This is line 16 of the code:

print("Yellow is now "..rs.testBundledInput("back",colors.yellow))

How do I fix that to not cause the error? I'm sorry for being so bad at this. I'm trying to learn. D;

old code for reference

print("Yellow is now "..rs.testBundledInput("back",colors.yellow))

fixed, I think

--#tostring take any value and attempts to convert it to a string (a value that the print function can use)
print("Yellow is now "..tostring(rs.testBundledInput("back",colors.yellow)))


overly engineered fix (use this if the above doesn

--# put this somewhere near the top of your code, at least above the erroring print function
--#once again, ignore the />
local function boolToString(B)/>/>/>/>/>/>/>
--#this function will convert the variable passed to it into a string representation of the value of the boolean
--#if the variable is not a boolean, it returns an empty string
  if type(B)/>/>/>/>/>/>/> ~= "boolean" then
	    return "" --#b is not a boolean, we could return "not a boolean" but that may  look messy
  end
  if b then
	    return "true" --#considering this is redstone, you may want to change this to "on"
  else
	    return "false" --#and this to "off"
  end
end
--#your repaired line
print("Yellow is now "..boolToSting(rs.testBundledInput("back",colors.yellow)))
Edited on 10 May 2015 - 09:41 PM
18tyweslow #11
Posted 10 May 2015 - 11:38 PM
-snip-

Awesome! It works!
Now I need it to keep looping the code and checking when it turns on/off. Currently, I run the code and it'll say either true or false, depending on when I run the program and the state of redstone(on/off). I need it to update without having to reboot the program. Is that possible?
Edited on 10 May 2015 - 09:49 PM
Lupus590 #12
Posted 10 May 2015 - 11:47 PM
-snip-

Awesome! It works!
Now I need it to keep looping the code and checking when it turns on/off. Currently, I run the code and it'll say either true or false, depending on when I run the program and the state of redstone(on/off). I need it to update without having to reboot the program. Is that possible?


--#one time setup code goes here

while true do --#this will loop forever
  os.pullevent("redstone") --#if nothing happens then I may have this event filer wrong

  --#your looping code goes here


end--#end the loop
--#code the end will never run
Edited on 10 May 2015 - 09:47 PM
18tyweslow #13
Posted 10 May 2015 - 11:49 PM
-snip-

Awesome! It works!
Now I need it to keep looping the code and checking when it turns on/off. Currently, I run the code and it'll say either true or false, depending on when I run the program and the state of redstone(on/off). I need it to update without having to reboot the program. Is that possible?


--#one time setup code goes here

while true do --#this will loop forever
  os.pullevent("redstone") --#if nothing happens then I may have this event filer wrong

  --#your looping code goes here


end--#end the loop
--#code the end will never run

http://imgur.com/ooRBuZ5 That's what I want my 3x3 Advanced Monitor setup to look like when it's done.

I want it to display all 6 ovens, and their current state, either processing or not, and their box's background either green or red depending on their state. Any ideas how I could accomplish that? That's what I'm trying to do with this current code, but the more I think about it, the more I don't think it'll work.

Any ideas? D;
Edited on 10 May 2015 - 09:50 PM
Lupus590 #14
Posted 10 May 2015 - 11:59 PM
I really need to go to bed, I'll leave you with this:
http://computercraft...indow_%28API%29
make your individual status boxes as windows

you will want to redraw them at the end of your loop

you may also want to store your data about your ovens in tables http://lua-users.org/wiki/TablesTutorial
you can then loop through the table to call redraw on each window (google will be your friend here)


--#one time setup code goes here

while true do --#this will loop forever
  os.pullevent("redstone") --#if nothing happens then I may have this event filer wrong

  --#your looping code goes here

  --#redraw the windows here

end--#end the loop
--#code the end will never run


best of luck
Edited on 10 May 2015 - 10:01 PM
18tyweslow #15
Posted 11 May 2015 - 12:01 AM
I really need to go to bed, I'll leave you with this:
http://computercraft...indow_%28API%29
make your individual status boxes as windows

you will want to redraw them at the end of your loop

you may also want to store your data about your ovens in tables http://lua-users.org.../TablesTutorial
you can then loop through the table to call redraw on each window (google will be your friend here)


--#one time setup code goes here

while true do --#this will loop forever
  os.pullevent("redstone") --#if nothing happens then I may have this event filer wrong

  --#your looping code goes here

  --#redraw the windows here

end--#end the loop
--#code the end will never run


best of luck

Sounds good! Thank you!