Jump to content




[How to] On startup display message on monitor


11 replies to this topic

#1 Bruno

  • New Members
  • 7 posts

Posted 12 May 2012 - 12:57 AM

So, I built an office complex, and I want a monitor to display text messages centered on it saying "Welcome to Weegee Towers!"

How can I program it so that it says "Welcome to Weegee Towers!" on the monitor, which is located to the left? It is a 4x1 monitor display.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 May 2012 - 01:05 AM

Try this code:
local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.

#3 Bruno

  • New Members
  • 7 posts

Posted 12 May 2012 - 01:30 AM

Error.

bios:355: bad argument: string expected, got nil

#4 Bruno

  • New Members
  • 7 posts

Posted 12 May 2012 - 01:34 AM

Nevermind, it was user error.

Thank you very much.

How did you learn lua so I can learn it and stop pestering you? :)/>

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 May 2012 - 02:01 AM

Well, the language is really easy to learn, specially if you already know about programming (i learned c/c++, c#, java, ruby, and others before). I would recommend reading some tutorials and the lua manual on it's site and wiki. Then, to learn about CC specifics you can go to the wiki. And of course, the best way is to practice, try things out and learn from your errors.
You can always come back with any questions and we'll try to help you.
Happy coding! :)/>

#6 EmTeaKay

  • Members
  • 115 posts

Posted 12 May 2012 - 02:07 AM

I learned by reading others code. Which, by my opinion, is a good way to learn. Try doing a password system for a door first. That's one of the first ones I did. And after you did that, build apon that, make it cleaner code, more user friendly, make it into DoorOS. Most importantly, learn by your mistakes.

#7 Bruno

  • New Members
  • 7 posts

Posted 12 May 2012 - 03:19 AM

How can I modify the text? I increased screen size to 2x4, and there is room for two different rows of text, but when I add to the text line, both are clipped off the screen.

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 May 2012 - 06:13 PM

I made a few changes to the code:
local tText = {
"First line", -- you can change the lines here
"Second line",
"Some more",
"lines here"
-- and you can add more if you want, just don't forget to put the commas (,) at the end of the lines (except for the last one)
}
local sSide = "left"

local function printCenter(mon, t)
  local w, h = mon.getSize()
  local y = math.floor((h / 2) - (#t / 2)) - 1
  for i, line in ipairs(t) do
	local x = math.floor((w / 2) - (#line / 2))
	mon.setCursorPos(x, y + i)
	mon.write(line)
  end
end

printCenter(peripheral.wrap(sSide), tText)
It writes the lines of text you want, allways centered on the monitor.

#9 tigerman4527

  • New Members
  • 3 posts

Posted 13 July 2012 - 10:48 PM

Using the revised code, how would I change the text size?

#10 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 13 July 2012 - 10:58 PM

Well, this is a really old post, but anyway.
This should work:
local tText = {
"First line", -- you can change the lines here
"Second line",
"Some more",
"lines here"
-- and you can add more if you want, just don't forget to put the commas (,) at the end of the lines (except for the last one)
}
local sSide = "left"
local nTextSize = 1 -- change the text size here (from 1 to 5)

local function printCenter(mon, t)
  local w, h = mon.getSize()
  local y = math.floor((h / 2) - (#t / 2)) - 1
  for i, line in ipairs(t) do
	local x = math.floor((w / 2) - (#line / 2))
	mon.setCursorPos(x, y + i)
	mon.write(line)
  end
end

local mon = peripheral.wrap(sSide)
mon.setTextScale(nTextSize)
printCenter(mon, tText)


#11 Rubixmaster5567

  • New Members
  • 1 posts

Posted 08 August 2012 - 05:19 PM

 MysticT, on 12 May 2012 - 01:05 AM, said:

Try this code:
local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.

i typed in this codes EXACTLY but im get this error: "attempt to index ? (a nil value)"

HELP ME

#12 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 08 August 2012 - 07:55 PM

 Rubixmaster5567, on 08 August 2012 - 05:19 PM, said:

 MysticT, on 12 May 2012 - 01:05 AM, said:

Try this code:
local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.

i typed in this codes EXACTLY but im get this error: "attempt to index ? (a nil value)"

HELP ME
Check that sSide matches the side of your monitor.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users