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

on/off button switch

Started by Dustmuz, 10 November 2014 - 06:19 PM
Dustmuz #1
Posted 10 November 2014 - 07:19 PM
Hello here..

i'm just "curious" if my coding is "right"

after looking through this and this, i got lost :P/> hehe


while true do
  local e = os.pullEvent( "redstone" ) --# wait for a redstone event, look it up on the wiki!
  if redstone.getInput( "left" ) then
	shell.run( "myprogram" )
  end
end
credits to engineer for writing this in this post.

could i implement that piece of code into a function and use it to change a word on my monitor and change the state of my reactor??


local function active()
  while true do
	local e = os.pullEvent( "redstone" )
	if redstone.getInput( "left" ) then
	  mon.SetCursorPos(1,7)
	  mon.clearLine
	  if rea.setActive == false then
		rea.setActive(true)
		mon.write("Reactor is: ON")
	  else
		rea.setActive(false)
		mon.write("Reactor is: OFF")
	  end
	end
  end
end
Edited on 10 November 2014 - 06:28 PM
Dragon53535 #2
Posted 10 November 2014 - 07:31 PM
Yes. However for efficiency i would try and check if the redstone state actually changed on that side.

while true do
  local oldState = rs.getInput( "left" )
  os.pullEvent( "redstone" ) --# We don't need to put this to a variable
  if rs.getInput( "left" ) ~= oldState then --#If the output actually changed.
	if rs.getInput( "left" ) then --# your if statement to see if the redstone state was on.
	  --#Do your stuff
	end
  end --# We don't need an else for the state swapping since we don't want to do anything if it hasn't swapped.
end
I'm also assuming that state is defined as a global variable elsewhere.
Edited on 10 November 2014 - 06:32 PM
Dustmuz #3
Posted 10 November 2014 - 07:35 PM
Yes. However for efficiency i would try and check if the redstone state actually changed on that side.

while true do
  local oldState = rs.getInput( "left" )
  os.pullEvent( "redstone" ) --# We don't need to put this to a variable
  if rs.getInput( "left" ) ~= oldState then --#If the output actually changed.
	if rs.getInput( "left" ) then --# your if statement to see if the redstone state was on.
	  --#Do your stuff
	end
  end --# We don't need an else for the state swapping since we don't want to do anything if it hasn't swapped.
end
I'm also assuming that state is defined as a global variable elsewhere.

hey Dragon :D/> sorry for not being on skype.. working from my ipad..

buttons only send a short redstone burst, so arent they toggled on and off, right away..
so wouldnt that oldstate variable become "useless", since the state is changed a few ticks later??

i want it to be, so that when i press the button, the reactor either shuts off or turns on..
Edited on 10 November 2014 - 06:38 PM
Dustmuz #4
Posted 10 November 2014 - 08:17 PM
i tried implementing the code..

http://pastebin.com/cWMf5YnD

but at line 17, i get an error "=" expected
Edited on 10 November 2014 - 07:27 PM
Lyqyd #5
Posted 10 November 2014 - 09:07 PM
The answer to that error is covered in our helpful sticky post. You're missing some parentheses.
Dustmuz #6
Posted 10 November 2014 - 09:10 PM
The answer to that error is covered in our helpful sticky post. You're missing some parentheses.

Doh *goes off to somewhere to hide