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

While True Do not working

Started by Nuiofrd, 30 April 2013 - 09:56 PM
Nuiofrd #1
Posted 30 April 2013 - 11:56 PM
Title: While True Do not working
This is the Computer Program
Spoiler
side = "back:blue"
m = peripheral.wrap(side)
m.setTextColor(colors.white)
on = false
rednet.open("right")
open = false
Nuiofrd = true
function set_screen()
  m.clear()
  m.setTextScale(2)
  m.setCursorPos(1,1)
  m.write("	 -=[Spawning]=-	")
  m.setCursorPos(1,3)
  m.write(" Spawner  XP  Hactches")
  if on == true and open == true then
	m.setCursorPos(1,4)
	m.write(" [On]		   [Open]")
  end
  if on == true and open == false then
	m.setCursorPos(1,4)
	m.write(" [On]		   [Close]")
  end
  if on == false and open == false then
	m.setCursorPos(1,4)
	m.write(" [Off]		  [Close]")
  end
  if on == false and open == true then
	m.setCursorPos(10,4)
	m.write(" [Off]		  [Open]")
  end
  m.setCursorPos(10,4)
  m.write(xp_levels)
end
function set_rs()
  if on == true and open == true then
	rs.setBundledOutput("bottom",colors.red)
  end
  if on == true and open == false then
	rs.setBundledOutput("bottom",colors.combine(colors.lime,colors.red))
  end
  if on == false and open == false then
	rs.setBundledOutput("bottom",colors.lime)
  end
  if on == false and open == true then
	rs.setBundledOutput("bottom",colors.cyan)
  end
end
function set_states()
  event, side, x, y = os.pullEvent("monitor_touch")
  if (x>=2) and (x<=4) and (y==4) then
	if on == true then
	  on = false
	else
	  on = true
	end
  end
  if (x>=15) and (x<=23) and (y==4) then
	if on == true then
	  open = false
	else
	  open = true
	end
  end
end
function get_xplevels()
  s, message, d = os.pullEvent("rednet_message")
  message = xp_levels
end
while true do
  get_xplevels()
  set_screen()
  set_rs()
  set_states()
end
This is the XP Turtle Program
Spoiler
m=peripheral.wrap("left")
m.setAutoCollect(true)
rednet.open("right")
rnb = 38 --rednet base
while true do
  if m.getLevels() >=30 then
	print("Enchantment Commencing")
	turtle.select(16)
	turtle.transferTo(15,1)
	turtle.select(15)
	m.enchant(30)
	print("Enchantment Done")
	turtle.dropDown()
  else
	sleep(1)
	xp_levels = m.getLevels()
	rednet.send(rnb, xp_levels)
  end
end

So i wrote the program above To help me learn how to use advanced Monitors and Advanced computers. I have it set up so you Click where is says, "[Off]" or "[On]" to turn off a spawner i have set up to a misc peripheral Xp turtle and that is working. I have a second button controlling hatches to let me in to wire places. So when I run the program it finishes, but it should not do that because of the "while true do". I am stumped.
The Xp is the levels of xp in the Xp Turtle Both Programs Are above,

This is a picture of the monitor, computer and spawner.



I have CCcables and Immbis Peripherals (I believe it thats the name i know it is Immbis Something) installed and MiscPeripherals installed.
i have it wire correct i had working before.
Imgoodisher #2
Posted 01 May 2013 - 07:49 PM
For one thing, you have

  message = xp_levels
I believe you want

  xp_levels = message

Also, the way you have it set up you can't change the spawner options while its waiting for an update from the turtle. Similarly, the xp won't update when its waiting for you to click. It would probably be better to have one os.pullEvent in the main loop and then call functions based on what event it gets

For example,

while true do
  event, arg1, arg2, arg3 = os.pullEvent()

  if event == "rednet_message" then
	xp_levels = arg2 -- the message is arg2

  elseif event == "monitor_touch" then
	set_states(arg1, arg2, arg3) -- send the side, x, and y to the function

  end

  set_rs()
  set_states()
end
Nuiofrd #3
Posted 02 May 2013 - 09:00 AM
I didn't work. I took your suggestions and still no luck.
The loop doesn't run forever like i want it to. It will just end. I believe the "while true do is broken

My program now looks like this
Spoiler
side = "back:blue"
m = peripheral.wrap(side)
m.setTextColor(colors.white)
on = false
rednet.open("right")
open = false
m.clear()
m.setTextScale(2)
function set_screen()
  if on == true and open == true then
	m.setCursorPos(1,4)
	m.clearLine()
	m.write(" [On]		   [Open]")
  end
  if on == true and open == false then
	m.setCursorPos(1,4)
	m.clearLine()
	m.write(" [On]		   [Close]")
  end
  if on == false and open == false then
	m.setCursorPos(1,4)
	m.clearLine()
	m.write(" [Off]		  [Close]")
  end
  if on == false and open == true then
	m.setCursorPos(1,4)
	m.clearLine()
	m.write(" [Off]		  [Open]")
  end
  m.setCursorPos(10,4)
  m.write(xp_levels)
end
function set_rs()
  if on == true and open == true then
	rs.setBundledOutput("bottom",colors.red)
  end
  if on == true and open == false then
	rs.setBundledOutput("bottom",colors.combine(colors.lime,colors.red))
  end
  if on == false and open == false then
	rs.setBundledOutput("bottom",colors.lime)
  end
  if on == false and open == true then
	rs.setBundledOutput("bottom",colors.cyan)
  end
end
function set_states(arg1, arg2, arg3)
  if (arg2>=2) and (arg2<=4) and (arg3==4) then
	if on == true then
	  on = false
	else
	  on = true
	end
  end
  if (arg2>=15) and (arg2<=23) and (arg3==4) then
	if on == true then
	  open = false
	else
	  open = true
	end
  end
end

--{base of program
m.setCursorPos(1,1)
m.write("	 -=[Spawning]=-	")
m.setCursorPos(1,3)
m.write(" Spawner  XP  Hactches")
while true do
  event, arg1, arg2, arg3 = os.pullEvent()

  if event == "rednet_message" then
		xp_levels = arg2 -- the message is arg2

  elseif event == "monitor_touch" then
		set_states(arg1, arg2, arg3) -- send the side, x, and y to the function

  end

  set_screen()
  set_rs()
end
LordIkol #4
Posted 02 May 2013 - 09:13 AM
add a sleep command at the end of the while loop and check if its still not working
sleep(0.5)
theoriginalbit #5
Posted 02 May 2013 - 09:21 AM
add a sleep command at the end of the while loop and check if its still not working
sleep(0.5)
Not needed, the loop yields with the os.pullEvent

@OP here is some improved code read the comments to see what I have done. NOTE: With the code you have you have to hit the monitor before the details will appear
Spoiler


-- # define all our main variables at the top, and make them all local
local monSide = 'back'
local rsSide = 'bottom'
local redSide = 'right'
local on = false
local open = false
-- # we can declare nil values in this way
local xp_levels
local m = peripheral.wrap(monSide)

-- # make the term function calls refer to the monitor
term.redirect(m)
-- # see now we can use term calls
term.setTextColor(colors.white)

rednet.open(redSide)

term.clear()
-- # this one still needs to be directly for the monitor though
m.setTextScale(2)

function set_screen()
  term.setCursorPos(1, 4)
  term.clearLine()
  -- #if it is on, it will be [On] or else it will be [Off]
  local onState = on and '[On] ' or '[Off]'
  local openState = open and '[Open] ' or '[Close]'
  write(' '..onState..'			'..openState)
  term.setCursorPos(10,4)
  write(xp_levels)
end

function set_rs()
  local outputCol

  -- # saying on or not on is the same as saying == true and == false respectively
  if on and open then
	outputCol = colors.red
  elseif on and not open then
	outputCol = colors.combine(colors.lime, colors.red)
  elseif not on and not open then
	outputCol = colors.lime
  elseif not on and open then
	outputCol = colors.cyan
  end

  rs.setBundledOutput(rsSide, outputCol)
end

function set_states(arg1, arg2, arg3)
  if (arg2 >= 2) and (arg2 <= 4) and (arg3 == 4) then
	-- # we can toggle in this way
	on = not on
  end
  if (arg2>=15) and (arg2<=23) and (arg3==4) then
	open = not on
  end
end

-- # again we can use term now
term.setCursorPos(1,1)
write("		-=[Spawning]=- ")
term.setCursorPos(1,3)
write(" Spawner  XP  Hactches")

while true do
  local event, arg1, arg2, arg3 = os.pullEvent()

  if event == "rednet_message" then
	xp_levels = arg2 -- # the message is arg2
  elseif event == "monitor_touch" then
	set_states(arg1, arg2, arg3) -- # send the side, x, and y to the function
  end

  set_screen()
  set_rs()
end

-- # give control back to the normal terminal
term.restore()
Edited on 02 May 2013 - 07:37 AM
Nuiofrd #6
Posted 02 May 2013 - 09:38 PM
if (arg2 >= 2) and (arg2 <= 4) and (arg3 == 4) then
		-- # we can toggle in this way
		on = not on
  end
  if (arg2>=15) and (arg2<=23) and (arg3==4) then
		open = not on
I do not understand the "on = not on".
theoriginalbit #7
Posted 02 May 2013 - 10:10 PM
I do not understand the "on = not on".
It is identical to

if on == true then
  on = false
else
  on = true
end
Boolean logic dictates

NOT true = false
NOT false = true
so by using

on = not on
we are toggling the variable from true -> false or false -> true.
Nuiofrd #8
Posted 02 May 2013 - 11:08 PM
Thanks but i still am having no luck. Also I have tried reinstalling it on both the server and the client. Would it have something do with me editing in Text Wrangler, it is a code editor.
Nuiofrd #9
Posted 04 May 2013 - 02:15 AM
Thanks but i still am having no luck. Also I have tried reinstalling it on both the server and the client. Would it have something do with me editing in Text Wrangler, it is a code editor.
Nuiofrd #10
Posted 06 May 2013 - 08:35 PM
Anyone Got any ideas I continue to be stumped
Smiley43210 #11
Posted 06 May 2013 - 09:09 PM
Is the XP turtle outputting any redstone signals? And please post your current code, it makes it much easier to help you. Pastebin would probably be best.
Nuiofrd #12
Posted 06 May 2013 - 11:17 PM
The Turtle is Not

Turtle Code:
Spoilerm=peripheral.wrap("left")
m.setAutoCollect(true)
rednet.open("right")
rnb = 38 –rednet base
while true do
if m.getLevels() >=30 then
print("Enchantment Commencing")
turtle.select(16)
turtle.transferTo(15,1)
turtle.select(15)
m.enchant(30)
print("Enchantment Done")
turtle.dropDown()
else
sleep(1)
xp_levels = m.getLevels()
rednet.send(rnb, xp_levels)
end
end

Computer Code:
Spoilerside = "back:blue"
m = peripheral.wrap(side)
m.setTextColor(colors.white)
on = false
rednet.open("right")
open = false
m.clear()
m.setTextScale(2)
function set_screen()
if on == true and open == true then
m.setCursorPos(1,4)
m.clearLine()
m.write(" [On] [Open]")
end
if on == true and open == false then
m.setCursorPos(1,4)
m.clearLine()
m.write(" [On] [Close]")
end
if on == false and open == false then
m.setCursorPos(1,4)
m.clearLine()
m.write(" [Off] [Close]")
end
if on == false and open == true then
m.setCursorPos(1,4)
m.clearLine()
m.write(" [Off] [Open]")
end
m.setCursorPos(10,4)
m.write(xp_levels)
end
function set_rs()
if on == true and open == true then
rs.setBundledOutput("bottom",colors.red)
end
if on == true and open == false then
rs.setBundledOutput("bottom",colors.combine(colors.lime,colors.red))
end
if on == false and open == false then
rs.setBundledOutput("bottom",colors.lime)
end
if on == false and open == true then
rs.setBundledOutput("bottom",colors.cyan)
end
end
function set_states(arg1, arg2, arg3)
if (arg2>=2) and (arg2<=4) and (arg3==4) then
if on == true then
on = false
else
on = true
end
end
if (arg2>=15) and (arg2<=23) and (arg3==4) then
if on == true then
open = false
else
open = true
end
end
end
–{base of program
m.setCursorPos(1,1)
m.write(" -=[Spawning]=- ")
m.setCursorPos(1,3)
m.write(" Spawner XP Hactches")
while true do
while true do
event, arg1, arg2, arg3 = os.pullEvent()
if event == "rednet_message" then
xp_levels = arg2 – the message is arg2
elseif event == "monitor_touch" then
set_states(arg1, arg2, arg3) – send the side, x, and y to the function
end
set_screen()
set_rs()
end
end
LordIkol #13
Posted 07 May 2013 - 04:36 AM
Hi Nui,

I changed your code a bit to make it little bit smaller and easier to read.
I could not find an obvious error so far but i have 2 Questions.

1. What is the ":blue" in side = "back:blue" for?
2. why you have 2 while true do loops in the end?

for the Changes i did to the code you can find HERE

1. removed the blue from the side cause i dont know what its meant to do :)/>
2. changed the if if if parts to if elseif (not necessary but i had some problems with if if if things in the past and elseif is more efficent)
3. changed all the if on == true and open == false stuff to if on and not open etc… (not needed but its shorter)
4. added some spacing to the code so that its easier to read

hope that helps somehow. will test it when im back home and give feedback.
Nuiofrd #14
Posted 08 May 2013 - 09:43 AM
1: The ":blue" is for CCCables. That is a plugin for ComputerCraft 1.5.0.
2: Well i was trying to see if there was something breaking my code on in the Second "while true do"

And thanks for the code i will test it out when i get a chance
Nuiofrd #15
Posted 11 May 2013 - 03:22 PM
Continually No luck was Found. I don't think it is a problem with the program, but only the mod itself.