This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Redstone signal display
Started by nuceng, 27 November 2016 - 03:27 AMPosted 27 November 2016 - 04:27 AM
My problem, other then i cant program to save my life, is that i want to have a single computer to monitor multiple redstone signals through a bundle cable. I've been building a powerplant/ factory for the last several years on and off. I have 200+ wireless signals and various other parameters. I want to monitor from a central location but i cant seem to get a starting point on a program that will work. All i want it to do is detect a redstone signal either true or false and print a unique phrase based on what the signal is. I have a program (that doesn't work currently) that might be down the right track but most likely I'm way off. I'm sure someone has a simple way to do this. It seems super basic compared to what you guys usually program. Any questions just let me know. Looking for tips here, maybe an example. I don't expect someone to just code for me.
Posted 27 November 2016 - 09:53 AM
try looking through the redstone api as this will lead to some functions that will assist you. Computercraft cant just only detect redstone signals, it can send them too. It also works with bundled cables!
http://computercraft.info/wiki/Redstone_(API)
http://computercraft.info/wiki/Redstone_(API)
Posted 27 November 2016 - 04:26 PM
I've already looked through the redstone API and I use it in a few programs already. I need something more advanced for what I'm trying to do. I tried setting up a matrix to get states of a bundled cable input, compare them to the previous state but i have trouble going from that to the program being able to pick out what changes and spitting out text that says specifically what happened.try looking through the redstone api as this will lead to some functions that will assist you. Computercraft cant just only detect redstone signals, it can send them too. It also works with bundled cables!
http://computercraft...i/Redstone_(API)
Posted 27 November 2016 - 11:52 PM
I have a program (that doesn't work currently) that might be down the right track but most likely I'm way off.
Hard to comment on how to improve it without seeing it.
Posted 28 November 2016 - 06:58 PM
I have a program (that doesn't work currently) that might be down the right track but most likely I'm way off.
Hard to comment on how to improve it without seeing it.
This code causes a not enough arguments fault on line 36 but there are only 35 lines to the code. Also haven't been able to test it so I'm not sure its what i want even.
function getStates()
curColors = rs.getBundledInput("left")
states =
{
[1] = colors.test(curColors, colors.red),
[2] = colors.test(curColors, colors.grey),
}
return state
end
print "online"
pstate = getStates()
function rsEvent()
cstate = getStates()
if (pstates[1] == false and cstate[1] == true) then
print("red on")
elseif (pstate[1] == true and cstate[1] == false) then
print("red off")
elseif (pstate[2] == false and cstate[2] == true) then
print("grey on")
end
pstate = cstate;
end
repeat
local event,p1 = os.pullEvent()
if event == "redstone" then
rsEvent()
end
until event == "char" and p1 == "x"
Posted 29 November 2016 - 03:29 AM
Testing it myself, I see the error is triggered on line 36 of the colours API - long story short, colors.grey is nil. You meant either colors.gray or colours.grey.
Posted 02 December 2016 - 12:30 AM
Testing it myself, I see the error is triggered on line 36 of the colours API - long story short, colors.grey is nil. You meant either colors.gray or colours.grey.
awesome. Thank you. Dam spelling gets me every time.
Still has errors though. Now i'm getting a nil value in the function rsEvents() i can make the fault go away by taking out the "[" around the numbers in the if, then statements but then the program does nothing. That means that nothing is assigned to [1] from what i can tell but i did assign a value to it in the table at the top. Probably just a small mistake somewhere but I'm not seeing it yet.
Edited on 01 December 2016 - 11:54 PM
Posted 02 December 2016 - 02:24 AM
pstate = getStates()
function rsEvent()
cstate = getStates()
if (pstates[1] == false and cstate[1] == true) then
print("red on")
elseif (pstate[1] == true and cstate[1] == false) then
print("red off")
elseif (pstate[2] == false and cstate[2] == true) then
print("grey on")
end
pstate = cstate;
end
In the first 'if' statement, you use pstates, instead of pstate, causing the attempt to index a nil value error I suspect you are getting.
Edit: Also, the parentheses '()' are not required for if statements, and only serve to group separate terms in the statement, eg:
if ( x and y ) or ( a and b ) then
--# Either both or one of the conditions grouped in parentheses was true.
end
if x and y then
--# x and y are truthy
end
if x == true and y == false then
--# x is true, y is false
end
--# so on
Your if statements don't do that, as only one operator is being used. This is just a design choice, but I prefer to avoid parentheses when I can.
Same applied to the print calls, if only a string is being printed, you can remove the parentheses, eg:
print "Hello World" --# Okay
print "Hello".." world" --# No
print aVariable --# No
Edited on 02 December 2016 - 01:30 AM
Posted 02 December 2016 - 03:44 AM
I forgot to mention i did catch the pstates error and i fixed it but that didnt change the issue. Still getting the nil value Good tip on the parentheses though i appreciate that one
Posted 02 December 2016 - 03:50 AM
here this is the program now. I get run:18 attempt to index ? (a nil value)
function getStates()
curColors = rs.getBundledInput("left")
states =
{
[1] = colors.test(curColors, colors.red),
[2] = colors.test(curColors, colors.gray),
}
return state
end
print "online"
pstate = getStates()
function rsEvent()
cstate = getStates()
if pstate[1] == false and cstate[1] == true then
print "red on"
elseif pstate1 == true and cstate1 == false then
print "red off"
elseif pstate2 == false and cstate2 == true then
print "gray on"
end
pstate = cstate;
end
repeat
local event,p1 = os.pullEvent()
if event == "redstone" then
rsEvent()
end
until event == "char" and p1 == "x"
function getStates()
curColors = rs.getBundledInput("left")
states =
{
[1] = colors.test(curColors, colors.red),
[2] = colors.test(curColors, colors.gray),
}
return state
end
print "online"
pstate = getStates()
function rsEvent()
cstate = getStates()
if pstate[1] == false and cstate[1] == true then
print "red on"
elseif pstate1 == true and cstate1 == false then
print "red off"
elseif pstate2 == false and cstate2 == true then
print "gray on"
end
pstate = cstate;
end
repeat
local event,p1 = os.pullEvent()
if event == "redstone" then
rsEvent()
end
until event == "char" and p1 == "x"
Posted 02 December 2016 - 04:00 AM
Line 18:
Here you're attempting to index into pstate and cstate. These are set by the getStates() function, which defines a table "states" and returns the nil value "state".
if pstate[1] == false and cstate[1] == true then
Here you're attempting to index into pstate and cstate. These are set by the getStates() function, which defines a table "states" and returns the nil value "state".
Posted 02 December 2016 - 04:41 AM
Line 18:if pstate[1] == false and cstate[1] == true then
Here you're attempting to index into pstate and cstate. These are set by the getStates() function, which defines a table "states" and returns the nil value "state".
awesome that fixed the errors.
Program works now, however it doesn't do what I want it to. Did some testing on it and the program will display "red on" when it is supposed to but will not display "red off" or "gray on"
Ultimately I want this program to display the status of 16 or more redstone signals when they change and specifically what those signals mean. The program as written is just for testing. Once I get it to do what I want it Wont say "red on/off" it will say " Reactor 1 On/off" for example. The key is to make each signal completely independent from the others for example if I have 16 total signals and 7 are true and 9 are false and one of them goes from true to false that needs to be displayed without re-displaying any previous signals.
I mean am I even on the right track here or do i have the completely wrong format for what I'm trying to accomplish here?
Edited on 02 December 2016 - 03:41 AM
Posted 04 December 2016 - 03:33 AM
Your coding is rather convoluted, but you're on the right track. Your remaining problem seems to boil down to yet more typos. When debugging your code, you need to learn to read back what you've written, as opposed to what you meant to write!
You refer to variables "pstate1", "pstate2", etc where you meant to index into the pstate table (pstate[1], pstate[2], etc). Ditto for cstate.
Just for kicks, here's how I'd write the script, taking advantage of the fact that each colour is represented by a power of two:
You refer to variables "pstate1", "pstate2", etc where you meant to index into the pstate table (pstate[1], pstate[2], etc). Ditto for cstate.
Just for kicks, here's how I'd write the script, taking advantage of the fact that each colour is represented by a power of two:
local oldState = rs.getBundledInput("left")
local colName = {"White", "Orange", "Magenta", "Light Blue", "Yellow", "Lime", "Pink", "Grey", "Light Grey", "Cyan", "Purple", "Blue", "Brown", "Green", "Red", "Black"}
print("Online")
repeat
local event, p1 = os.pullEvent()
if event == "redstone" then
local curState, checkCol = rs.getBundledInput("left"), 1
for i = 1, 16 do
local curOn, oldOn = colours.test(curState, checkCol), colours.test(oldState, checkCol),
if curOn and not oldOn then
print(colName[i] .. " turned on.")
elseif not curOn and oldOn then
print(colName[i] .. " turned off.")
end
checkCol = checkCol * 2
end
oldState = curState
end
until event == "key" and p1 == keys.x