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

Light toggle Help

Started by Zukamimozu, 24 March 2012 - 04:28 PM
Zukamimozu #1
Posted 24 March 2012 - 05:28 PM
Okay so I have been trying to get lights in my house to turn on when I say On, and Off when I say Off in the computer. Although whenever I run it it just automatically turns the lights on and leaves the program. Also I'm trying to get it so after you type On or off it will remember the redstone output but restart the program. So the program I wrote…

term.clear()
term.setCursorPos(16, 1)
print "Light Control"
term.setCursorPos(1, 3)
print "On or Off: "

if input == On then
rs.setOutput("back", true)
print "Lights On"

else

if input == Off then
rs.setOutput("back", false)
print "Lights Off" end
end
Sebra #2
Posted 24 March 2012 - 05:31 PM

while true do
 ... -- insert your code here
end
To terminate use ctrl-T
Espen #3
Posted 24 March 2012 - 05:38 PM
In addition to the while loop you need to actually read the input from the user.
I've tried to type up a quick example:

term.clear()
term.setCursorPos(16, 1)
print "Light Control"

while true do
  term.setCursorPos(1, 3)
  write "On or Off: "
  local input = read()

  if input == string.lower("on") then
	rs.setOutput("back", true)
	print "Lights On"
  end

  if input == string.lower("off") then
	rs.setOutput("back", false)
	print "Lights Off" end
  end

  if input == string.lower("exit") then
	break  -- Breaks out of the loop and thus exits the program (since there is nothing else outside of the loop in this case).
  end
end

Not tested ingame, but should work. Holler if not. ^^
Edited on 24 March 2012 - 04:39 PM
Zukamimozu #4
Posted 24 March 2012 - 05:53 PM
[string "startup"]:16: no loop to break

hold on missed some of it
EDIT: okay still says no loop to break but on line 22
NeoHummel #5
Posted 24 March 2012 - 06:24 PM
Remove the "end" on the line with "Lights off", you're terminating the loop early because of it.

Also, you may want to use the string.lower() functions on the input variable instead.
kamnxt #6
Posted 24 March 2012 - 07:21 PM
You forgot double quotes in your original code. It should be "On", not On.
Espen #7
Posted 24 March 2012 - 07:47 PM
[string "startup"]:16: no loop to break

hold on missed some of it
EDIT: okay still says no loop to break but on line 22
What NeoHummel said.
Derped up there, sorry. Will test it the next time. :(/>/>
Zukamimozu #8
Posted 24 March 2012 - 09:32 PM
It works :(/>/> although when you type On it says lights on, but when you go to type off it doesn't replace anything yet, so like if I type On it will say lights on, then I type off and it says lights Off, type on again and it says lights onf.. lol
Espen #9
Posted 24 March 2012 - 09:45 PM
It works :(/>/> although when you type On it says lights on, but when you go to type off it doesn't replace anything yet, so like if I type On it will say lights on, then I type off and it says lights Off, type on again and it says lights onf.. lol
I see what you mean. Here's a version with all fixes applied + clearing the line to prevent what you just said:

term.clear()
term.setCursorPos(16, 1)
print "Light Control"

while true do
  term.setCursorPos(1, 3)
  term.clearLine()
  write "On or Off: "
  local input = string.lower( read() )

  if input == "on" then
	rs.setOutput("back", true)
	print "Lights On"
  end

  if input == "off" then
	rs.setOutput("back", false)
	print "Lights Off"
  end

  if input == "exit" then
	break  -- Breaks out of the loop and thus exits the program (since there is nothing else outside of the loop in this case).
  end
end

Tested and should work. :)/>/>
Zukamimozu #10
Posted 24 March 2012 - 09:51 PM
either I did something wrong or you tested wrong, because it says onf on the second time you turn it on…
nvm fixxed it by doing waht you said but on print "Lights On" I put it as print "Lights On "
Espen #11
Posted 25 March 2012 - 12:54 AM
either I did something wrong or you tested wrong, because it says onf on the second time you turn it on… nvm fixxed it by doing waht you said but on print "Lights On" I put it as print "Lights On "
Oh, that's what you meant.
I've fixed the wrong line then. Thought you meant the input line.^^
To make it clear the whole line before printing anything new on it, you can just call term.clearLine() before the print("Lights on") etc.
Sorry for the misunderstanding.