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

TimedFlashing message on computer

Started by robertjones6, 19 December 2012 - 04:17 PM
robertjones6 #1
Posted 19 December 2012 - 05:17 PM
My friend wants a message to flicker (flash) on a computer, he said he wants it flicker off for less than a second as if the power had failed

as i am not sure of the message yet can we use "enter message here" and i will replace it with the message when i know it
Orwell #2
Posted 19 December 2012 - 05:32 PM

local message = "enter message here"  -- change message with yours
local minDisplayTime = 5  -- minimum time to display message
local maxDisplayTime = 10  -- maximum time to display message
local offTime = 0.5  -- time to let the message disappear

local function cPrint(str)
  local w,h = term.getSize()
  local x = math.floor( (w-#str)/2 + 0.5 )
  local y = math.floor(h/2)
  term.setCursorPos(x,y)
  term.write(str)
end

while true do
  cPrint(message)
  sleep(math.random(minDisplayTime,maxDisplayTime))
  term.clear()
  sleep(offTime)
end
robertjones6 #3
Posted 20 December 2012 - 03:48 AM
what if i want to cause the message to go on to seperate lines

eg.

Merry
Christmas
Orwell #4
Posted 20 December 2012 - 04:26 AM
I think that this should work:

local message =
[[Merry
Christmas]]  -- change message with yours
local minDisplayTime = 3  -- minimum time to display message
local maxDisplayTime = 6  -- maximum time to display message
local offTime = 0.2  -- time to let the message disappear

local function cPrint(str)
  local w,h = term.getSize()
  local _,lines = string.gsub(str,"\n","\n")
  local y = math.floor( (h-lines)/2 + 0.5 )
  for dy=1,lines+1 do
	local index = string.find(str, "\n") or #str+1
	local l = string.sub(str,1, index-1)
	local x = math.floor( (w-#l)/2 + 0.5 )
	term.setCursorPos(x, y+dy)
	term.write(l)
	str = string.sub(str,index+1)
  end
end

while true do
  cPrint(message)
  sleep(math.random(minDisplayTime,maxDisplayTime))
  term.clear()
  sleep(offTime)
end

Edit: fixed it
robertjones6 #5
Posted 20 December 2012 - 04:45 AM
ok
Orwell #6
Posted 20 December 2012 - 06:58 AM
Sorry, something came in between. I edited my previous post with a working version for multiple lines.
robertjones6 #7
Posted 20 December 2012 - 11:07 AM
getting this message

startup:23: attempt to get length of number
robertjones6 #8
Posted 20 December 2012 - 02:37 PM
Anyone know anything about this?
Orwell #9
Posted 20 December 2012 - 03:44 PM
I just copied it to pastebin and downloaded it into CC and it works perfectly for me. Are you sure that you have '#l' with a lower case L in there, and not '#1'? Did you type it over or copied it?
robertjones6 #10
Posted 21 December 2012 - 01:26 AM
i got it working but the message i put in (i didnt design this by the way doing it for the server admin) is this:

local message =
[[WARNING! Connection to Central Lost….
Reconnect Attempt 2,000,000,000,000
Too many attempts….

Teleport Satus: Operational
Power Status: Emergency Only
Signal: Unknown
Location: Unknown
Advisery: Teleports to Unknown
Locations not Advised]] change message with yours
local minDisplayTime = 3 minimum time to display message
local maxDisplayTime = 6 maximum time to display message
local offTime = 0.2 time to let the message disappear

local function cPrint(str)
local w,h = term.getSize()
local _,lines = string.gsub(str,"\n","\n")
local y = math.floor( (h-lines)/2 + 0.5 )
for dy=1,lines+1 do
local index = string.find(str, "\n") or #str+1
local l = string.sub(str,1, index-1)
local x = math.floor( (w-#l)/2 + 0.5 )
term.setCursorPos(x, y+dy)
term.write(l)
str = string.sub(str,index+1)
end
end

while true do
cPrint(message)
sleep(math.random(minDisplayTime,maxDisplayTime))
term.clear()
sleep(offTime)
end

WARNING! Connection to Central Lost….
Reconnect Attempt 2,000,000,000,000
Too many attempts….

Teleport Satus: Operational
Power Status: Emergency Only
Signal: Unknown
Location: Unknown
Advisery: Teleports to Unknown
Locations not Advised

i have included links to pictures of what is seen on the computer, i am also trying to get it to work on the monitors that are hooked up

http://snag.gy/1ovID.jpg - what i see on the computer
http://snag.gy/hSgC2.jpg - trying to link it to that monitor
robertjones6 #11
Posted 21 December 2012 - 01:34 PM
BUMP TO TOP
Orwell #12
Posted 21 December 2012 - 01:43 PM
Hmm, I would say not to bump, but you were indeed totally forgotten. :P/> Apparently I screwed something up in calculating the newlines in a string (only tested it for 2 line strings :/). I don't really have the time to look into it now, maybe someone else has?
ChunLing #13
Posted 21 December 2012 - 02:53 PM
If it's displaying on the computer the way you want then probably you can just use term.redirect to put it on the monitor. But the computer display doesn't look all that great to me.
robertjones6 #14
Posted 21 December 2012 - 03:41 PM
so up to now its basically this
term.redirect

local message =
[[WARNING! Connection to Central Lost….
Reconnect Attempt 2,000,000,000,000
Too many attempts….

Teleport Satus: Operational
Power Status: Emergency Only
Signal: Unknown
Location: Unknown
Advisery: Teleports to Unknown
Locations not Advised]] change message with yours
local minDisplayTime = 3 minimum time to display message
local maxDisplayTime = 6 maximum time to display message
local offTime = 0.2 time to let the message disappear

local function cPrint(str)
local w,h = term.getSize()
local _,lines = string.gsub(str,"\n","\n")
local y = math.floor( (h-lines)/2 + 0.5 )
for dy=1,lines+1 do
local index = string.find(str, "\n") or #str+1
local l = string.sub(str,1, index-1)
local x = math.floor( (w-#l)/2 + 0.5 )
term.setCursorPos(x, y+dy)
term.write(l)
str = string.sub(str,index+1)
end
end

while true do
cPrint(message)
sleep(math.random(minDisplayTime,maxDisplayTime))
term.clear()
sleep(offTime)
end

but ye the display on the computer isnt looking too good
ChunLing #15
Posted 21 December 2012 - 04:06 PM
Umm…to use term.redirect you need to call it and pass a handle of a wrapped peripheral, like term.redirect(peripheral.wrap("left")) (you've got the monitor on the left, right?)

The cPrint() function isn't handling all the line returns in your string correctly, evidently some of them aren't matching \n (which is a common problem with formatting a multi-line string with double brackets).

Try just doing something like:
term.redirect(peripheral.wrap("left"))
local message =
[[WARNING! Connection to Central Lost....
Reconnect Attempt 2,000,000,000,000
Too many attempts....

Teleport Satus: Operational
Power Status: Emergency Only
Signal: Unknown
Location: Unknown
Advisery: Teleports to Unknown
Locations not Advised]]
local minDisplayTime =3-- minimum time to display message
local maxDisplayTime =6-- maximum time to display message
local offTime =0.2-- time to let the message disappear

while true do
    print(message)
    sleep(math.random(minDisplayTime,maxDisplayTime))
    term.clear()
    sleep(offTime)
end
You don't get the snazzy centering from cPrint, but that function will only work properly if you feed it a string where all the line returns use \n, and your current string doesn't fit (you can reformat the string and try using cPrint once you've got stuff showing on the monitor).