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

Security/shield Program Problem, No Error. Help Please.

Started by popdog15, 24 January 2013 - 01:34 PM
popdog15 #1
Posted 24 January 2013 - 02:34 PM
Well, this is a security/door program. When the user presses space it is meant to toggle the state from open to closed, or vice-versa. Anyone know why this isn't doing as intended?




function updateText()
os.sleep(1)
  term.clear()
  door1()
  door1scan()
  updateText()
end
function door1()
if doorStat1 == true then
 
  term.setCursorPos(1,2)
   term.clearLine()
  term.setTextColor(colors.green)
   term.write"Door 1 is OPEN."
  else
 
   term.setCursorPos(1,2)
    term.clearLine()
   term.setTextColor(colors.red)
    term.write"Door 1 is CLOSED"
end
end

function door1scan()
  local event, scancode = os.pullEvent("key")
   if scancode == 57 and doorStat1 == false then
    doorStat1 = true
   end
  if scancode == 57 and doorStat1 == true then
   doorStat1 = not doorStat1
  end
end
updateText()
theoriginalbit #2
Posted 24 January 2013 - 02:38 PM
it is because you have the structure of the program the wrong way around… a function should be declared BEFORE it is used…

EDIT: Also your door1scan() can be changed to this


function door1scan()
  local event, scancode = os.pullEvent("key")
  if scancode  == 57 then
	doorStat1 = not doorStat1
  end
end
you had extra redundant logic in there :)/>

EDIT #2: Also you don't seem to have any loop in there, meaning that the program will just terminate when you press space… I suggest adding an infinite while loop in there… :)/>

Something like this would work well
Spoiler

while true do
  term.setCursorPos(1,2)
  term.clearLine()
  term.setTextColor( doorStat1 and colors.green or colors.red ) -- if doorStat1 is true it will be green, if false or nil will be red
  write( "Door 1 is "..( doorStat1 and "OPEN" or "CLOSED" ) -- if doorStat1 is true it will be"OPEN", if false or nil will be "CLOSED"

  local event, scancode = os.pullEvent("key")
  if scancode == 57 then
    doorStat1 = not doorStat1
  end
end
Edited on 24 January 2013 - 01:47 PM
popdog15 #3
Posted 24 January 2013 - 03:14 PM
Thanks!