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

Light system?

Started by Traumah, 12 August 2012 - 05:16 AM
Traumah #1
Posted 12 August 2012 - 07:16 AM
Hello, i'm trying to make a program that when you type "lights" it turns the lights on but if you type "lights" when the lights are already active it turns them off. I've been trying for atleast an hour to get this working, however i'm not having any luck at all.

Thanks in advance!
BigSHinyToys #2
Posted 12 August 2012 - 07:36 AM
This should do it
Spoiler

local sSide = "left" -- the side on input
local pState = false -- this shows if it was on before so er dont have to print again
while true do -- start of a loop
    term.clear()
    term.setCursorPos(1,1)
    print("lighting controll")
    local e = read()
    if e == "lights" and not pState then -- checks if somthing redstone has changes also checks if it was the side we selected an cheches wether it was on bofore
	    print(sSide.." Turned On") -- prints to screen
	    rs.setOutput(sSide,true)
	    pState = true -- stores its previous state
    elseif e == "lights" and pState then  -- checks if somthing redstone has changes also checks if it was the side we selected an cheches wether it was on bofore
	    print(sSide.." Turned Off") -- prints to screen
	    rs.setOutput(sSide,false)
	    pState = false -- stores its previous state
    end -- end of IF statent
end -- end of while loop
Pharap #3
Posted 12 August 2012 - 08:33 AM
First of all, have all your lights connect to one single 'cable'. Have that cable link to one of the sides of the computer (for argument's sake, we'll say the back)
then do something like this:

code for lights:

side = "back"
if rs.getOutput(side) == true then
rs.setOutput(side, false)
elseif rs.getOutput(side) == false then
rs.setOutput(side, true)
end

Any time you run that program, named lights, it will change the lights from on to off or off to on. If you want to do it inside another program though, that's a bit more complicated.
Traumah #4
Posted 12 August 2012 - 11:19 AM
First of all, have all your lights connect to one single 'cable'. Have that cable link to one of the sides of the computer (for argument's sake, we'll say the back)
then do something like this:

code for lights:

side = "back"
if rs.getOutput(side) == true then
rs.setOutput(side, false)
elseif rs.getOutput(side) == false then
rs.setOutput(side, true)
end

Any time you run that program, named lights, it will change the lights from on to off or off to on. If you want to do it inside another program though, that's a bit more complicated.

Thank you, worked perfectly.
Pharap #5
Posted 12 August 2012 - 11:22 AM
First of all, have all your lights connect to one single 'cable'. Have that cable link to one of the sides of the computer (for argument's sake, we'll say the back)
then do something like this:

code for lights:

side = "back"
if rs.getOutput(side) == true then
rs.setOutput(side, false)
elseif rs.getOutput(side) == false then
rs.setOutput(side, true)
end

Any time you run that program, named lights, it will change the lights from on to off or off to on. If you want to do it inside another program though, that's a bit more complicated.

Thank you, worked perfectly.

Glad it helped.

I did something similar to this before with an app I made, where I had to change a button between two different settings when it was clicked.
Lyqyd #6
Posted 12 August 2012 - 05:34 PM
First of all, have all your lights connect to one single 'cable'. Have that cable link to one of the sides of the computer (for argument's sake, we'll say the back)
then do something like this:

code for lights:

side = "back"
if rs.getOutput(side) == true then
rs.setOutput(side, false)
elseif rs.getOutput(side) == false then
rs.setOutput(side, true)
end

Any time you run that program, named lights, it will change the lights from on to off or off to on. If you want to do it inside another program though, that's a bit more complicated.

This works, but it could be condensed down to:


side = "back"
rs.setOutput(side, not rs.getOutput(side))
Pharap #7
Posted 12 August 2012 - 06:34 PM
First of all, have all your lights connect to one single 'cable'. Have that cable link to one of the sides of the computer (for argument's sake, we'll say the back)
then do something like this:

code for lights:

side = "back"
if rs.getOutput(side) == true then
rs.setOutput(side, false)
elseif rs.getOutput(side) == false then
rs.setOutput(side, true)
end

Any time you run that program, named lights, it will change the lights from on to off or off to on. If you want to do it inside another program though, that's a bit more complicated.

This works, but it could be condensed down to:


side = "back"
rs.setOutput(side, not rs.getOutput(side))

Yes, I know, but if someone is asking about something like a lighting system, they clearly aren't that confident with programming yet, so it's best to write something that's easier for them to understand than something higher level that they are likely to use but not understand. In which case they will only come back asking why they have an error when they change something that conflicts.

Building knowledge from the ground up is the way forward, not throwing people in at the deep end.
Richiarde #8
Posted 02 September 2012 - 07:02 PM
Hello,

i tried to connect the world sensor, but i wasnt succesfull. I want automatic light system where the light turn on only during the night. Is it possible?
IceCream #9
Posted 03 September 2012 - 05:04 AM
Rich, You can do that, CC Isn't needed

Here's a video that will help

http://www.youtube.com/watch?v=0-13GnVUJUE&feature=plcp
Mmmmmmmm #10
Posted 03 September 2012 - 08:18 AM
Hello,

i tried to connect the world sensor, but i wasnt succesfull. I want automatic light system where the light turn on only during the night. Is it possible?
I think you can use os.time() to do that.
Richiarde #11
Posted 03 September 2012 - 03:09 PM
Hello,

i tried to connect the world sensor, but i wasnt succesfull. I want automatic light system where the light turn on only during the night. Is it possible?
I think you can use os.time() to do that.

thanks. I will try this for sure. Iam just afraid of the returned number format. Guess it will be the old give it a try method..
Pharap #12
Posted 06 September 2012 - 02:08 AM
Rich, You can do that, CC Isn't needed

Here's a video that will help

That's assuming the player has redpower logic. This is a site for and regarding computercraft, not Redpower, you can't just assume someone has redpower when giving answers. Also that clock is flawed as it can be disrupted by other light sources and isn't fixed to the world clock like the time functions in computercraft.