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

Title: How To Create A Program That Will Use The Light Sensor As A Variable

Started by DannySMc, 31 July 2013 - 06:43 PM
DannySMc #1
Posted 31 July 2013 - 08:43 PM
Okay so I am using a mod called: Wireless red stone; which literally puts transmitter/receiver blocks.

I have built a little house that on the ceiling there are pistons that move the ceiling and lights around.

I wanted to make it that when it's day a certain program will run, that turns the lights on and when it is night I want them to be turned off. Now see if you keep up with me as it gets a bit messy…

I have made the on and off programs and they will make the lights go on and off. The Program names are called: "on" and "off".

My computer has 3 transmitter blocks 1 on the left, 1 on the back, and 1 on the right, that the on/off programmes use to control the pistons on the house. I then have a day light sensor on the top of the computer. Now I wanted to know how to make a program that will use the daylights sensors input ("true"/"false") to control the lights. I have made a simple test program which is as follows:

local solar = rs.getInput("top")
print(solar)
if solar == true then
  shell.run("off")
else
  shell.run("on")
end
Now this works…to an extent. I have to keep restarting it at day/night for it to do it automatically.

The problem is if the "on" or "off" programmes run more than once and its the same value:
EXAMPLE:
If you run the "on" program twice then it will keep moving the pistons around then when it's set to on, if the program is run again it will reset the pistons then set them to on again, which makes lots of noise.

I want the program to literally say, hey guess what?! It's daytime, lets turn off the lights! but then the program keeps running until night time comes and then it literally says, hey guess what?! it's night time, lets turn the lights on!. And this will run forever and ever! but it just can't keep making the "on"/"off" programs constantly running, and it needs to run it once in the day to turn off and once in the night to turn the lights on.

If anyone can help I will be hugely thankful!!

If you don't understand it then I will give you my IP so you can connect :D/>
Just email me as it will come straight to my phone:
danny.smc95@gmail.com

Thanks :)/>
campicus #2
Posted 31 July 2013 - 08:48 PM
Something like this should work? EDIT: You want to avoid running programs within programs. Use functions:


function on()
  <the code that turns stuff on>
end

function off()
  <the code that turns stuff off>
end

while true do
  if rs.getInput("top") == true then
	off()
  else
	on()
  end
  sleep(1)
end
DannySMc #3
Posted 31 July 2013 - 08:52 PM
Something like this should work? EDIT: You want to avoid running programs within programs. Use functions:


function on()
  <the code that turns stuff on>
end

function off()
  <the code that turns stuff off>
end

while true do
  if rs.getInput("top") == true then
	off()
  else
	on()
  end
  sleep(1)
end

I have tried this method, it works, but the problem is it runs the program every second…I need it to do it only when the daylight sensors variable changes,

But thanks :P/>
DannySMc #4
Posted 31 July 2013 - 08:56 PM
Something like this should work? EDIT: You want to avoid running programs within programs. Use functions:


function on()
  <the code that turns stuff on>
end

function off()
  <the code that turns stuff off>
end

while true do
  if rs.getInput("top") == true then
	off()
  else
	on()
  end
  sleep(1)
end

I have tried this method, it works, but the problem is it runs the program every second…I need it to do it only when the daylight sensors variable changes,

But thanks :P/>

Ignore that I didn't see the top part it didn't load…I shall try it
DannySMc #5
Posted 31 July 2013 - 09:09 PM
Something like this should work? EDIT: You want to avoid running programs within programs. Use functions:


function on()
  <the code that turns stuff on>
end

function off()
  <the code that turns stuff off>
end

while true do
  if rs.getInput("top") == true then
	off()
  else
	on()
  end
  sleep(1)
end

I did try this but now the actual on/off code won't run properly:/ gah, can I do it with os.setAlarm??? would that be easier? and if so what are the time sets?
campicus #6
Posted 31 July 2013 - 09:11 PM
Can you post your full code, with the function declared? Pastebin it if you want
PixelToast #7
Posted 31 July 2013 - 09:15 PM

local last=rs.getInput("top")
while true do
	if last then
                rs.setOutput("back",false)
                sleep(0.5)
                rs.setOutput("right",false)
                sleep(0.5)
                rs.setOutput("left",true)
                sleep(0.5)
                rs.setOutput("back",true)
	else
                rs.setOutput("back",false)
                sleep(0.5)
                rs.setOutput("left",false)
                sleep(0.5)
                rs.setOutput("right",true)
                sleep(0.5)
                rs.setOutput("back",true)
	end
	while rs.getInput("top")==last do
		os.pullEvent("redstone")
	end
	last=rs.getInput("top")
end
thar :3
EDIT:
edited
DannySMc #8
Posted 31 July 2013 - 09:16 PM
Can you post your full code, with the function declared? Pastebin it if you want

Here you go:

function on()
rs.setOutput("back",false)
sleep(0.5)
rs.setOutput("right",false)
sleep(0.5)
rs.setOutput("left",true)
sleep(0.5)
rs.setOutput("back",true)
end
function off()
rs.setOutput("back",false)
sleep(0.5)
rs.setOutput("left",false)
sleep(0.5)
rs.setOutput("right",true)
sleep(0.5)
rs.setOutput("back",true)
end
while true do
  if rs.getInput("top") == true then
    off()
  else
    on()
  end
end
DannySMc #9
Posted 31 July 2013 - 09:22 PM

local last=rs.getInput("top")
while true do
	if last then
		-- on
	else
		-- off
	end
	while rs.getInput("top")==last do
		os.pullEvent("redstone")
	end
	last=rs.getInput("top")
end
thar :3
Thank you that worked,

question though would you um, explain how it works?:)/> like I don't really follow, well it's not "clicking".
DannySMc #10
Posted 31 July 2013 - 09:26 PM

local last=rs.getInput("top")
while true do
	if last then
				rs.setOutput("back",false)
				sleep(0.5)
				rs.setOutput("right",false)
				sleep(0.5)
				rs.setOutput("left",true)
				sleep(0.5)
				rs.setOutput("back",true)
	else
				rs.setOutput("back",false)
				sleep(0.5)
				rs.setOutput("left",false)
				sleep(0.5)
				rs.setOutput("right",true)
				sleep(0.5)
				rs.setOutput("back",true)
	end
	while rs.getInput("top")==last do
		os.pullEvent("redstone")
	end
	last=rs.getInput("top")
end
thar :3
EDIT:
edited
haha haven't tried this but thanks
PixelToast #11
Posted 31 July 2013 - 09:26 PM

local last=rs.getInput("top") -- define last
while true do
        if last then -- if last is true then
                -- on
        else
                -- off
        end
        while rs.getInput("top")==last do -- loop until the side changes
                os.pullEvent("redstone") -- wait for a redstone update
        end
        last=rs.getInput("top") -- redefine last so it registers in the if statement
end
its a bit advanced, you could just do this:

while true do
        if rs.getInput("top") then
                -- on
        else
                -- off
        end
        os.pullEvent("redstone") -- wait for a redstone update
end
but it would trigger the if statement every redstone update, no matter wich side its on
DannySMc #12
Posted 31 July 2013 - 09:30 PM

local last=rs.getInput("top") -- define last
while true do
		if last then -- if last is true then
				-- on
		else
				-- off
		end
		while rs.getInput("top")==last do -- loop until the side changes
				os.pullEvent("redstone") -- wait for a redstone update
		end
		last=rs.getInput("top") -- redefine last so it registers in the if statement
end
its a bit advanced, you could just do this:

while true do
		if rs.getInput("top") then
				-- on
		else
				-- off
		end
		os.pullEvent("redstone") -- wait for a redstone update
end
but it would trigger the if statement every redstone update, no matter wich side its on
Ahh I see thanks :3