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

Touch Screen Door Control

Started by TechedZombie, 13 February 2015 - 03:05 PM
TechedZombie #1
Posted 13 February 2015 - 04:05 PM
Ok I know this will probably be something that most of you can do but I am new to programming and I am still in MS but I have a made a simple program (I mean really simple it's under 100 lines) that uses a 1x1 monitor and has a "Lock" and an "Open" button which activates redstone. Yes it is simple but it is visually appealing I am proud of it because it is my first time using Touch Screens so I thought others might like it too…

How it Works:
Infinite Loop Testing for someone to touch the monitor
Detects if they hit Open or Lock
Toggles Redstone Accordingly
———————————————————————


mon = peripheral.wrap("monitor_1")
rednet.open("bottom")
while true do
  mon.setBackgroundColor(colors.black)
  mon.clear()
  mon.setTextColor(colors.black)
  mon.setTextScale(2)
  mon.setBackgroundColor(colors.lime)
  mon.setCursorPos(1,2)
  mon.write("Open")
  mon.setBackgroundColor(colors.red)
  mon.setCursorPos(1,1)
  mon.write("Lock")

event, monid, x, y = os.pullEvent("monitor_touch")

	  redstone.setOutput("right", false)
	  redstone.setOutput("left", false)
	print (y)

   if event == "monitor_touch" and y == 1 then
	redstone.setOutput("right", false)
	redstone.setOutput("left", false)
rednet.broadcast("locking")
  end

  if y == 2 then
	redstone.setOutput("right", true)
	redstone.setOutput("left", true)
  end
end

SetUp:
run "pastebin get XyiqV0Wh startup

with wired modems and Networking Cable Hook up a monitor (1x1)

Be sure to change the monitor name to whatever your using.(This Should be on the first line of code)

then on the left and right side of the computer redstone will turn on when the "open" button is pished so wore the redstone to your door. (This will work with Piston Doors of most kinds)

Hope you like it!
Geforce Fan #2
Posted 16 February 2015 - 04:13 AM
You should localize your variables
Here's a tutorial on it: http://www.computerc...c/15688-locals/
Also, your indentations are weird. Here's how they work:

variable=anotherVariable
function myFunction()
  --we indent here
  variable=anotherVariable
  if variable == anotherVariable then
	variable=anotherVariable
  else
	variable=anotherVariable
  end
  while true do
	variable=anotherVariable
	break
  end
  for i=1,1 do
	variable=anotherVariable
  end
  repeat
	 variable=anotherVariable
  until variable==anotherVariable
end
Those are example for everything you need to indent. Press TAB once or SPACE twice to indent.
Edited on 16 February 2015 - 03:19 AM
TechedZombie #3
Posted 18 February 2015 - 12:02 AM
You should localize your variables
Here's a tutorial on it: http://www.computerc...c/15688-locals/
Also, your indentations are weird. Here's how they work:

variable=anotherVariable
function myFunction()
  --we indent here
  variable=anotherVariable
  if variable == anotherVariable then
	variable=anotherVariable
  else
	variable=anotherVariable
  end
  while true do
	variable=anotherVariable
	break
  end
  for i=1,1 do
	variable=anotherVariable
  end
  repeat
	 variable=anotherVariable
  until variable==anotherVariable
end
Those are example for everything you need to indent. Press TAB once or SPACE twice to indent.


I know how to indent but between switching from notepad to the IG editor and making quick changes by adding a line my neatly indented program became a mess very quicly although I do appreciate the advice on localizing variables I will look into that :)/> .