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

Advanced door security system

Started by enforcer4100, 29 June 2014 - 07:24 PM
enforcer4100 #1
Posted 29 June 2014 - 09:24 PM
I have been trying to do something special with computercraft.
As a student I used to work in the pharmaceutical industry as a labworker.
In this lab we had those sterile zones where they kept their bacteria and in order to access them you had to use a keycard and a code in order to open the door. So I have been trying to design something similar by combining keycard code and using most of nanobobs touchscreen code. I have been working on this for a while and it has been overwhelming me a little bit.


--High Security Door System
--peripherals and redstone
--computer, not needed
c=peripheral.wrap("computer_1")
--monitor
m=peripheral.wrap("monitor_2")
--disk drive
d=peripheral.wrap("drive_0")

--redstone
redstoneSide="bottom"
--Code and ID
code="12345"
id="enforcer"
--Keypad input functions
inputs={}
function drawNumpad()
m.clear()
clearInput()
m.setBackgroundColor(128)
m.setCursorPos(2,1)
m.write("	 ")

m.setBackgroundColor(256)
m.setCursorPos(1,1)
m.write(" ")
m.setCursorPos(7,1)
m.write(" ")
m.setCursorPos(1,2)
m.write("	   ")

m.setBackgroundColor(32768)
m.setCursorPos(2,2)
m.write("1 2 3")
m.setCursorPos(2,3)
m.write("4 5 6")
m.setCursorPos(2,4)
m.write("7 8 9")
m.setCursorPos(2,5)
m.write("  0  ")


m.setBackgroundColor(256)
m.setCursorPos(1,3)
m.write(" ")
m.setCursorPos(1,4)
m.write(" ")
m.setCursorPos(1,5)
m.write(" ")
m.setCursorPos(7,3)
m.write(" ")
m.setCursorPos(7,4)
m.write(" ")
m.setCursorPos(7,5)
m.write(" ")

m.setBackgroundColor(32)
m.setCursorPos(6,5)
m.write("Ok")

m.setBackgroundColor(16384)
m.setCursorPos(1,5)
m.write("< ")

end

function inputNumber()
event,side,x,y=os.pullEvent("monitor_touch")
if x==2 and y==2 then
  digit=1
elseif x==4 and y==2 then
  digit=2
elseif x==6 and y==2 then
  digit=3
elseif x==2 and y==3 then
  digit=4
elseif x==4 and y==3 then
  digit=5
elseif x==6 and y==3 then
  digit=6
elseif x==2 and y==4 then
  digit=7
elseif x==4 and y==4 then
  digit=8
elseif x==6 and y==4 then
  digit=9
elseif x==4 and y==5 then
  digit=0
elseif x>=2 and y==5 then
  digit="ok"
elseif x<=6 and y==5 then
  digit="clear"
end

if digit=="ok" then
  checkNumber()
elseif digit=="clear" then
  backspaceInput()
else
  if #inputs<5 then
   inputs[#inputs+1]=digit
   m.setBackgroundColor(128)
   m.setCursorPos(#inputs+1,1)
   m.write(tostring(digit))
  end
end

end

function backspaceInput()
inputs[#inputs]=nil
m.setCursorPos(2,1)
m.write("	 ")
m.setCursorPos(2,1)
m.setBackgroundColor(128)
for i,number in pairs(inputs) do
  m.write(tostring(number))
end
end
function clearInput()
inputs={}
m.setCursorPos(2,1)
m.setBackgroundColor(128)
m.write("	 ")
end
--Keycard input functions
while true do
  if disk.isPresent(d) then -- diskdrive side
		h = fs.open("disk/passcode", "r")
  end
end

--Access system functions
function checkAccess()
if (input==code) and (h.readAll() == id) then
  redstone.setOutput(redstoneSide,true)
  clearInput()
  sleep(5)
  redstone.setOutput(redstoneSide,false)
else
  clearInput()
end
end
--Access system
checkAccess()


Most of the code is standard except for the last few bits that I changed to my needs.
The problem I have is that I get an error on line 34 (peripheral:34: string expected) but I am not sure why I am getting it.
I used the touchscreen program on it's own without problems. First I thought that I wrapped my peripheral wrong but it doesn't give an error for the lines above so I don't know what is wrong.

Could you people check for errors? And if possible check if my code is correct at all?

Thank you in advance.

edit: The computer, diskdrive and monitor are connected via wired modems. I made sure they are all activated.
Edited on 29 June 2014 - 07:25 PM
Bomb Bloke #2
Posted 30 June 2014 - 02:15 AM
You wrapped your peripheral wrong. :P/>

Notice that the error is occurring on the 34th line inside the peripheral API - not the 34th line of your script. Even so, it's only erroring because you passed it an invalid valid when you called it.

Sometimes, functions you call are nice enough to tell you the line they were called from if that call made them fall over. Often, they aren't: You've got to work out the stack trace yourself.

In this case, the function that's failing is peripheral.getType(), which wants a string. That's being called by disk.isDrive() (which wants a string), which in turn is being called by disk.isPresent() (which wants a string), which is being called by line 128 in your script (which is passing a peripheral).

Change line 8 to:

d="drive_0"
NanoBob #3
Posted 30 June 2014 - 10:15 AM
Uhm… This code is not made by you. it's mine..
I'm trying to write a program that will have a nice looking touchscreen code lock much like one you would see on an iPhone. I am having problems with setting up the buttons and getting to program to restart once the passcode is entered incorrectly. I am using a 3 by 4 monitor setup with the computer in the back. The passcode is currently set to 2580 and any other questions are welcome. Please help thanks,
Nate

Pastebin: http://pastebin.com/faUqL0BY
I've made something similar to this but with a 1x1 monitor. Feel free to look at my code for examples on how to do things. (http://pastebin.com/cnrFuHQ4) But do not just copy and claim it to be yours.
Also I did not give you permision to use it. I gave people permision to look at it to help your program, not copy mine.
Edited on 30 June 2014 - 08:18 AM
enforcer4100 #4
Posted 30 June 2014 - 10:22 AM
and using most of nanobobs touchscreen code.

I mentioned it here. I currently use it as I don't understand touchscreens yet and this code is more like a proof of concept that needs to be changed (with my code) over time. In case you are worrying that I redistribute it, I am not going to.

You wrapped your peripheral wrong. :P/>

Notice that the error is occurring on the 34th line inside the peripheral API - not the 34th line of your script. Even so, it's only erroring because you passed it an invalid valid when you called it.

Sometimes, functions you call are nice enough to tell you the line they were called from if that call made them fall over. Often, they aren't: You've got to work out the stack trace yourself.

In this case, the function that's failing is peripheral.getType(), which wants a string. That's being called by disk.isDrive() (which wants a string), which in turn is being called by disk.isPresent() (which wants a string), which is being called by line 128 in your script (which is passing a peripheral).

Change line 8 to:

d="drive_0"

Thank you. I should have noted it was in the peripheral API but I haven't been playing MC in a while.
I could have sworn though that the different wrapping isn't on the wiki.

edit: Spelling :wacko:/>
Edited on 30 June 2014 - 08:33 AM
NanoBob #5
Posted 30 June 2014 - 10:33 AM
I mentioned it here. I currently use it as I don't understand touchscreens yet and this code is more like concept that needs to be changed (with my code) over time.
In case you are worrying that I redistribute it, I am not going to.
Alright, Let me explain you how to use the touchScreens.
So first of all the touching of a monitor triggers an event. An event called 'monitor_touch'
You can check for this event to happend by doing:

event,side,x,y=os.pullEvent("monitor_touch")
os.pullEvent() is a function that waits for any event to be triggered, and will not do anything untill an event triggers.
The first argument for os.pullEvent() Is a way to specifiy what kind of event you want to handle. In this case it's monitor_touch.
Then when the event gets triggered by a player right clicking the monitor it will return a couple variables.
The first variable returns what event has been triggered, this is in this case not important since you've already specified the event type.
The second one is the side the monitor is on. Unless you have multiple monitors attached this does not matter either.
the x and y represent the coordinates on the screen that the player clicked. You can use this to see if it is within your 'button'
So this is an example that would check for when a player clicks somewhere. And if it is your button it will print something to the chatbox.

--Let's first create the 'button''
monitor=peripheral.wrap("right")  --Assuming the monitor is on the right
monitor.setCursorPos(5,5)
monitor.write("Button")  -- This writes the word button at the coordinates 5,5. Every letter uses one 'pixel' (not really a pixel but whatever)
while true do
 event,side,x,y=os.pullEvent("monitor_touch")
 if x>=5 and x<=11 and y==5 then --This would be the box you could click on, the minimum x. THe maximum x (5+the ammount of letters in the word button)	and the y coordinate
  print("You clicked the button")
 end
end
Hope this helps
Edited on 30 June 2014 - 08:34 AM