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

[CC1.63] I need help with a loop that isn't working

Started by EliteGaming, 08 August 2014 - 09:06 AM
EliteGaming #1
Posted 08 August 2014 - 11:06 AM
Ok so I have been working on a program for 3 days straight and I can't quite figure it out. I am very close but not finished. My intention is making a Touch Screen Menu to turn off my machines in my mob spawner. I connected them through MFR RedNet cables.

os.loadAPI("button")
local m = peripheral.wrap("left")
local buttonColors = {}
m.clear()
  
function mobSpawner()
m.clear()
button.setTable("Door", door, 19,31,10,13)
button.setTable("Lights", lights, 4,16,10,13)
button.setTable("Grinder", grinder, 34,46,10,13)
button.setTable("MobSpawner", spawner, 19,31,16,19)
button.screen()
defaults()
end
function defaults()
rs.setAnalogOutput("bottom", 6)
rs.setAnalogOutput("top", 0)
button.toggleButton("Grinder")
rs.setAnalogOutput("right", 3)
button.toggleButton("Door")
rs.setAnalogOutput("back", 2)
button.toggleButton("Lights")
end
function spawner()
while true do
if  rs.setAnalogOutput("bottom", 0) then
   rs.setAnalogOutput("bottom", 6)
button.toggleButton("Spawner")
sleep(1)
else
rs.setAnalogOutput("bottom", 0)
button.toggleButton("Spawner")
sleep(1)
end
end
end
function grinder()
while true do
if rs.setAnalogOutput("top", 5) then
   rs.setAnalogOutput("bottom", 0)
button.toggleButton("Grinder")
sleep(1)
else
rs.setAnalogOutput("bottom", 5)
button.toggleButton("Grinder")
sleep(1)
end
end
end

function door()
while true do
if rs.setAnalogOutput("right", 0) then
   rs.setAnalogOutput("right", 3)
button.toggleButton("Door")
sleep(1)
else
rs.setAnalogOutput("right", 0)
button.toggleButton("Door")
sleep(1)
end
end
end
function lights()
while true do
if rs.setAnalogOutput("back", 0) then
   rs.setAnalogOutput("back", 2)
button.toggleButton("Lights")
sleep(1)
else
rs.setAnalogOutput("back", 0)
button.toggleButton("Lights")
sleep(1)
end
end
end
function getClick()
   local event,side,x,y = os.pullEvent()
   if event=="monitor_touch" then
	 button.checkxy(x,y)
   end
end

mobSpawner()
while true do
   getClick()
end


Ok so the buttons work but then after activated it doesn't de-activate once pushed again. I need to know how to make this GUI properly work as many activates and de-activates it comes across. The other API is from DireWolf20's Button API :
http://pastebin.com/1iwuzsh8
natedogith1 #2
Posted 08 August 2014 - 09:25 PM
If I'm reading your code right, it seems you have several functions that'll never exit from a while true do loop. Honestly, I'm not sure why those loops are there in the first place.
EliteGaming #3
Posted 08 August 2014 - 09:48 PM
Do you think you can help me fix the code? I am really new to this computer craft and all I want is a a simple menu I can control my mob spawner with.
natedogith1 #4
Posted 08 August 2014 - 10:01 PM
If you simply remove the 'while true do' and its associated 'end' I imagine it'll work. Also, 'rs.setAnalogOutput(side,strength)' doesn't return anything, so it'll skip over all of those if statements and go directly to the else. Also also, I'm curious as to why you're using setAnalogOutput rather than setOutput?
EliteGaming #5
Posted 08 August 2014 - 10:53 PM
setOutput wasn't working that well for me, in lua mode. So I tried setAnalog and it seemed to work a bit. So what should i replace 'rs.setAnalogOutput(side,strength)' with?
TheOddByte #6
Posted 09 August 2014 - 12:24 AM
setOutput wasn't working that well for me, in lua mode. So I tried setAnalog and it seemed to work a bit. So what should i replace 'rs.setAnalogOutput(side,strength)' with?
Are you sure you're using it right?

rs.setOutput( side, boolean )

--# Example
rs.setOutput( "back", true )
natedogith1 #7
Posted 09 August 2014 - 12:26 AM
you're probably looking for 'rs.getAnalogOutput(side)==strength', though I'm curious as to what you mean when you say setOutput wasn't working well
LeonTheMisfit #8
Posted 09 August 2014 - 12:32 AM
Since you stated that you're working with MFR Rednet cable you should be using rs.setBundledOutput()
Edited on 08 August 2014 - 10:33 PM
EliteGaming #9
Posted 10 August 2014 - 10:05 PM
Yes sorry I meant that rs.setBundledOutput() wasn't working for me well. So I was forced to switch to rs.setAnalogOutput() So how can I make my GUI work properly so I can just walk up to it and use it as many times as I want because as of the fixes you guys have told me to change the buttons can only work once or I have to reboot the program. And I don't want to have to do that.