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

[Rednet] [Monitor] Please help! :(

Started by mibac138, 15 January 2013 - 10:01 AM
mibac138 #1
Posted 15 January 2013 - 11:01 AM
What is wrong in this code? (Problem is with Drzwi(door) it don't works)


local charSelected = ">"
local charNotSelected = " "
local optionLength = 42
local configPath = "Database/config"
local logoFile = "Config/test.nfp"
local optionOnTextColor = colors.lime
local optionOffTextColor = colors.red
local keyUp = 200
local keyDown = 208
local keyEnter = 28
local keyQuit = 16
local options = { -- list of options
[1] = {name = "a", title = "Drzwi", value = "ON", ["type"] = "toggle", valueOn = "ON", valueOff = "OFF"},
[2] = {name = "b", title = "Tarcza", value = "ON", ["type"] = "toggle", valueOn = "ON", valueOff = "OFF"},
--[3] = {name = "c", title = "System", value = "ON", ["type"] = "togglesleep", valueOn = "ON", valueOff = "Restartowanie"},
}
local selected = 1 -- default option
local running = true

-- HELPER FUNCTIONS
local function redrawMenu()
  local termWidth, termHeight = term.getSize()
  local startY = 2
  term.setBackgroundColor(colors.white)
  for i=1, #options, 1 do
	term.setTextColor(colors.yellow)
	term.setCursorPos( 2, startY + i - 1 )
if selected == i then write(charSelected) term.setTextColor(colors.orange)
else write(charNotSelected) term.setTextColor(colors.cyan) end
	write( options[i].title )
write( string.rep( " ", optionLength - string.len( options[i].title ) ) )
if options[i]["type"] == "toggle" then
   if options[i].value == options[i].valueOn then
	 term.setTextColor(optionOnTextColor)
   else
	 term.setTextColor(optionOffTextColor)
   end
end
write(  tostring(options[i].value) )
write( string.rep( " ", termWidth - optionLength - string.len(options[i].value) - 3 ) )
  end
  term.setCursorPos(2,18)
  write( string.rep( " ", termWidth - 2 ) )
end
-- program
term.clear()
  local canMove = true

while running do
  rednet.open("bottom")
  redrawMenu()
  canMove = options[selected]["type"] == "toggle" or
	( (options[selected]["type"] == "key") and (options[selected].value ~= "_") )
	if options[1].value == "ON" then rednet.send(4, "ON") mon.clear() mon.setCursorPos(1,1) mon.setTextColor(colors.lime) mon.setTextScale(2) mon.write("Drzwi: On") elseif options[1].value == "OFF" then rednet.send(4, "OFF") mon.clear() mon.setCursorPos(1,1) mon.setTextColor(colors.red) mon.setTextScale(2) mon.write("Drzwi: Off") end
	if options[2].value == "ON" then rednet.send(6, "ON") mon.clear() mon.setCursorPos(1,2) mon.setTextColor(colors.lime) mon.setTextScale(2) mon.write("Tarcza: On") elseif options[2].value == "OFF" then rednet.send(6, "OFF") mon.clear() mon.setCursorPos(1,2) mon.setTextColor(colors.red) mon.setTextScale(2) mon.write("Tarcza: Off") end

  event, p1 = os.pullEvent()
  if event == "key" then
	if p1 == keyDown and canMove then
	  if selected == #options then selected = 1
   else selected = selected + 1 end
	elseif p1 == keyUp and canMove then
	  if selected == 1 then selected = #options
	 else selected = selected - 1
   end
	elseif p1 == keyEnter and canMove then
   if options[selected]["type"] == "toggle" then
		if options[selected].value == options[selected].valueOn then
		  options[selected].value = options[selected].valueOff
  else
		 options[selected].value = options[selected].valueOn
		end
elseif p1 == 16 then
   running = false
end
  end
end -- while running

It's mostly in Polish if you wont to know ;)/>

@Edit

Computer controling drzwi(door) code


while true do
term.setCursorPos(1,1)
term.clearLine()
printC("MibacMFFSOs", colors.yellow)
rednet.open("bottom")
e, p1, p2 = os.pullEvent("rednet_message")
if p1 == 5 and p2 == "ON" then
rs.setOutput("top", true)
term.clear()
printCentered("Force: On", 2, colors.lime)
elseif p1 == 5 and p2 == "OFF" then
rs.setOutput("top", false)
term.clear()
printCentered("Force: Off", 2, colors.red)
elseif p1 == 5 and p2 == "Reboot" then os.reboot()
end
sleep(0)
end

Computer controling Tarzca(shiled) code


while true do
term.setCursorPos(1,1)
term.clearLine()
printC("MibacMFFSOs", colors.yellow)
rednet.open("bottom")
e, p1, p2 = os.pullEvent("rednet_message")
if p1 == 5 and p2 == "ON" then
rs.setOutput("top", true)
term.clear()
printCentered("Force: On", 2, colors.lime)
elseif p1 == 5 and p2 == "OFF" then
rs.setOutput("top", false)
term.clear()
printCentered("Force: Off", 2, colors.red)
elseif p1 == 5 and p2 == "Reboot" then os.reboot()
end
sleep(0)
end
ChunLing #2
Posted 15 January 2013 - 06:11 PM
You need to describe exactly what you want the code to do, and exactly what happens instead.

In the case of the main code, Drzwi, it looks to be missing an end somewhere, which should have produced an error message (you should show us that sort of thing). But saying where it should go requires understanding what you're trying to do.
mibac138 #3
Posted 16 January 2013 - 04:43 AM
You need to describe exactly what you want the code to do, and exactly what happens instead.

In the case of the main code, Drzwi, it looks to be missing an end somewhere, which should have produced an error message (you should show us that sort of thing). But saying where it should go requires understanding what you're trying to do.

I want to do system what turns on my defence .

It not printing any error :huh:/>

And the problem is when Tarcza is on even if drzwi is on it shutdown
ChunLing #4
Posted 16 January 2013 - 05:37 AM
We don't know how your defenses are set up. Please explain, in terms of actual inputs and outputs, what the program is supposed to do. And, explain exactly what it is doing instead, inputs and outputs.
remiX #5
Posted 16 January 2013 - 06:53 AM
When I copy paste your first program I get an error:

77: end expected to close 'while' at line 49

edit:

end missing here:


			if options[selected]["type"] == "toggle" then
				if options[selected].value == options[selected].valueOn then
					options[selected].value = options[selected].valueOff
				else
					options[selected].value = options[selected].valueOn
				end
			end -- this end

Also, what exactly is wrong?

What do you mean by:
"And the problem is when Tarcza is on even if drzwi is on it shutdown "

Does it shutdown when both are on?
mibac138 #6
Posted 20 January 2013 - 04:06 AM
The problem is when tarcza is on drzwi are off when tarcza is on and drzwi is on :(/>