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

[Lua] [question] help debugging wireless flashing hello sine

Started by jadelade, 22 May 2012 - 08:25 AM
jadelade #1
Posted 22 May 2012 - 10:25 AM
hay ppl

i have bean working on a remote flashing hello sine for my friends server and this is the code

function hello()
	print(" ")
	print(" ")
	print(" ")
	print(" ")
	print("	__	__   _______   __   __	 ______")
	print("   |  |  |  | |	   | |  | |  |   /	  ")
	print("   |  |  |  | |   ____| |  | |  |  /  ____  ")
	print("   |  |__|  | |  |____  |  | |  | /  /	  ")
	print("   |		| |	   | |  | |  | |  |	|  |")
	print("   |   __   | |   ____| |  | |  | |  |	|  |")
	print("   |  |  |  | |  |____  |  | |  |   ____/  /")
	print("   |  |  |  | |	   | |  | |  |  		/")
	print("   |__|  |__| |_______| |__| |__|   ______/")
end
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
OOS = 0
while true do
	C,ID,MS = os.pullEvent()
	if MS == "1" then
		OOS = 1
	end
	while OOS == 1 do
		C,ID,MS = os.pullEvent()
		hello()
		sleep(0.5)
		term.clear()
		term.setCursorPos(1,1)
		sleep(0.1)
		if MS == "2" then
			OOS = 0
		end
	end
end


it is suppose to tern on and blink wen it gets a 1 and tern off wen it gets a 2 but it only blinks one time
please help
Luanub #2
Posted 22 May 2012 - 10:43 AM
It only blinks once because its runs through the loop once then stops at the os.pullEvent() and waits for an event. There is a couple of ways to solve this. I would probably just use a timer to trigger an event allowing the loop to proceed.


while OOS == 1 do
    os.startTimer(0.5)
    C,ID,MS = os.pullEvent()
    hello()
    sleep(0.5)
    term.clear()
    term.setCursorPos(1,1)
    sleep(0.1)
    if MS == "2" then
        OOS = 0
    end
end
MysticT #3
Posted 22 May 2012 - 06:17 PM
The problem is, like luanub said, that the os.pullEvent call makes your program wait for an event (a key press, a rednet message, etc.). You can use a timer to do the blink part while receiving the rednet message to stop.
Try with this:

local function blink()
  local on = false
  local timer = os.startTimer(0.5) -- start the timer
  while true do
    local evt, arg, msg = os.pullEvent() -- get an event
    if evt == "timer then -- if it's a timer event
	  if arg == timer then -- and it's our timer
	    on = not on -- switch the blinking status
	    if on then
		  hello() -- draw the text
	    else
		  term.clear() -- clear the screen
		  term.setCursorPos(1, 1)
	    end
	    timer = os.startTimer(0.5) -- restart the timer
	  end
    elseif evt == "rednet_message" then -- if it's a rednet message
	  if msg == "2" then -- and it's the stop message
	    term.clear() -- clear the screen
	    term.setCursorPos(1, 1)
	    break -- break the loop
	  end
    end
  end
end

while true do
  local id, msg = rednet.receive() -- wait for the message
  if msg == "1" then -- check if it's the start message
    blink() -- start blinking
  end
end
It waits for a message to start (the message "1"), and then it starts showing the blinking text on the screen while waiting for a message to stop (the message "2").
jadelade #4
Posted 22 May 2012 - 09:10 PM
thanks it helped PS Mystic you did not close of your string on line 6 "timer" lol
MysticT #5
Posted 22 May 2012 - 09:23 PM
thanks it helped PS Mystic you did not close of your string on line 6 "timer" lol
:P/>/> that's what happens when you write the code in the forum and don't test it.
jadelade #6
Posted 23 May 2012 - 06:54 AM
lol