Jump to content




Simple Variable Toggle on redstone input


  • You cannot reply to this topic
6 replies to this topic

#1 Xenthera

  • Members
  • 170 posts

Posted 29 July 2012 - 12:29 AM

I'm new to programming, and I need some help re-writing a bit of code.
I wrote this code to toggle a variable on a redstone input, but is there anyway else to do it that's a lot more simpler, and reliable?

while true do
shell.run("clear")
print(toggle)
os.pullEvent("redstone")
shell.run("clear")
toggle = 1 - toggle
print(toggle)
sleep(1)
os.pullEvent("redstone")
shell.run("clear")
toggle = 1 - toggle
print(toggle)
sleep(1)
end



#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 29 July 2012 - 01:00 AM

Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.

local toggle = false
while true do
	os.pullEvent("redstone")
	toggle = rs.getInput("side")
	if toggle then
		print("true")
	else
		print("false")
	end
end


#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 July 2012 - 01:02 AM

local toggle = false

while true do
  term.clear()
  term.setCursorPos(1, 1)
  print(toggle)
  os.pullEvent("redstone")
  toggle = not toggle
end

EDIT: ninja'd :ph34r:/>

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 29 July 2012 - 01:04 AM

I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.

#5 Xenthera

  • Members
  • 170 posts

Posted 29 July 2012 - 01:06 AM

View PostLyqyd, on 29 July 2012 - 01:00 AM, said:

Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.

local toggle = false
while true do
	os.pullEvent("redstone")
	toggle = rs.getInput("side")
	if toggle then
		print("true")
	else
		print("false")
	end
end

That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.

#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 29 July 2012 - 01:09 AM

View PostLyqyd, on 29 July 2012 - 01:04 AM, said:

I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.
It converts anything to a string, since it uses tostring on every parameter. The only thing that it doesn't print is nil (and everything after a nil), since it stops when there's one.

View PostXenthera, on 29 July 2012 - 01:06 AM, said:

View PostLyqyd, on 29 July 2012 - 01:00 AM, said:

Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.

local toggle = false
while true do
	os.pullEvent("redstone")
	toggle = rs.getInput("side")
	if toggle then
		print("true")
	else
		print("false")
	end
end

That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.
os.pullEvent("redstone") will make it wait for a change in redstone (turn on or off), so it can't loop twice with the same "pulse", unless there's another input that changes.

#7 Xenthera

  • Members
  • 170 posts

Posted 29 July 2012 - 01:16 AM

View PostMysticT, on 29 July 2012 - 01:09 AM, said:

View PostLyqyd, on 29 July 2012 - 01:04 AM, said:

I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.
It converts anything to a string, since it uses tostring on every parameter. The only thing that it doesn't print is nil (and everything after a nil), since it stops when there's one.

View PostXenthera, on 29 July 2012 - 01:06 AM, said:

View PostLyqyd, on 29 July 2012 - 01:00 AM, said:

Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.

local toggle = false
while true do
	os.pullEvent("redstone")
	toggle = rs.getInput("side")
	if toggle then
		print("true")
	else
		print("false")
	end
end

That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.
os.pullEvent("redstone") will make it wait for a change in redstone (turn on or off), so it can't loop twice with the same "pulse", unless there's another input that changes.

You're Right. I need to get around it using the Redstone signal turning off, as a redstone activity. Because I turn the button on, it toggles the variable, but then when the button turns off, it toggles the variable again, (like it's supposed to I guess)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users