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

[LUA] [MiscPeripherals] Loops (solved)

Started by LikeableBump, 06 January 2013 - 02:54 AM
LikeableBump #1
Posted 06 January 2013 - 03:54 AM
I'm using MiscPeripherals and Forestry to try and automatically charge my machines. I'm using a gate reader to see how low the power is in the storage unit, and a computer connected to that to tell the engines when to turn on.

This is my code so far:


m = peripheral.wrap("left")
data = m.get()
--for i, j in pairs(data) do
--  print(tostring(i)..": "..tostring(j))
--end

  if (data["No Energy"]) then
  
	print ("Needs recharge...")
	redstone.setOutput("back", true)

  elseif (data["Full Energy"]) then
  
	print ("Turning off engines...")
	redstone.setOutput("back", false)
	
  end

What kind of a loop do I need to put that in to make it constantly update itself? I tried the infinite loop (while true do) but that didn't seem to work. I also don't really want it to print the messages every time it goes through the loop; only when it actually updates. Is there any way to make that not happen? Thanks in advance for your help; it's very much appreciated.
thislooksfun #2
Posted 06 January 2013 - 05:06 AM
You need to put in a check to see if the state has changed since the last update (you also can't have an unyielding infinite loop or else the program will quit, hence the sleep). Try this, but note that I only wrote code. For you because I am a little bored. This forum isn't for people to get code written for them. That said, good luck getting this to work!


m = peripheral.wrap("left")
data = m.get()

noEn = false
fullEn = false

while true do
  if (data["No Energy"]) and not noEn then
    print ("Needs recharge...")
    redstone.setOutput("back", true)
    noEn = true
  else
    noEn = false
  end

  if (data["Full Energy"]) and not fullEn then
    print ("Turning off engines...")
    redstone.setOutput("back", false)
    fullEn = true
  else
    fullEn = false
  end

  sleep(1)
end

-tlf
LikeableBump #3
Posted 06 January 2013 - 02:31 PM
That definitely works better than what I had, but it seems to get stuck in one section of the loop. I had it print the table every time it went through the loop, and even when I changed out the storage unit for a full one, the printed information stays the same. Is there a way to clear its memory every time it goes through the loop or something?
thislooksfun #4
Posted 06 January 2013 - 04:45 PM
That's really easy to do


m = peripheral.wrap("left")

noEn = false
fullEn = false

while true do
  data = m.get()
  if (data["No Energy"]) and not noEn then
	print ("Needs recharge...")
	redstone.setOutput("back", true)
	noEn = true
  else
	noEn = false
  end

  if (data["Full Energy"]) and not fullEn then
	print ("Turning off engines...")
	redstone.setOutput("back", false)
	fullEn = true
  else
	fullEn = false
  end

  sleep(1)
end

I just moved the 'data = m.get()' line inside the loop. Happy coding!
LikeableBump #5
Posted 06 January 2013 - 06:06 PM
Thanks! That works very well. I can't believe I didn't notice that; I had it in the loop before but moved it back outside the loop for some reason. I appreciate the help very much. :)/>
thislooksfun #6
Posted 07 January 2013 - 04:11 AM
You're welcome! [selfpromotion] BTW, check out the OS I wrote. [/selfpromotion]