Jump to content




Redstone signal display

api help computer

12 replies to this topic

#1 nuceng

  • Members
  • 7 posts

Posted 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.

#2 TheRockettek

  • Members
  • 547 posts
  • LocationRem is best girl

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...i/Redstone_(API)

#3 nuceng

  • Members
  • 7 posts

Posted 27 November 2016 - 04:26 PM

View PostTheRockettek, on 27 November 2016 - 09:53 AM, said:

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)
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.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 27 November 2016 - 11:52 PM

View Postnuceng, on 27 November 2016 - 04:27 AM, said:

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.

#5 nuceng

  • Members
  • 7 posts

Posted 28 November 2016 - 06:58 PM

View PostBomb Bloke, on 27 November 2016 - 11:52 PM, said:

View Postnuceng, on 27 November 2016 - 04:27 AM, said:

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"

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#7 nuceng

  • Members
  • 7 posts

Posted 02 December 2016 - 12:30 AM

View PostBomb Bloke, on 29 November 2016 - 03:29 AM, said:

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 by nuceng, 02 December 2016 - 12:54 AM.


#8 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

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 by Hbomb_79, 02 December 2016 - 02:30 AM.


#9 nuceng

  • Members
  • 7 posts

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

#10 nuceng

  • Members
  • 7 posts

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"

#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 December 2016 - 04:00 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".

#12 nuceng

  • Members
  • 7 posts

Posted 02 December 2016 - 04:41 AM

View PostBomb Bloke, on 02 December 2016 - 04:00 AM, said:

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 by nuceng, 02 December 2016 - 04:41 AM.


#13 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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:

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users