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

Multiple Flashing Monitors

Started by diealone, 28 August 2013 - 08:15 PM
diealone #1
Posted 28 August 2013 - 10:15 PM
I am trying to write a program where 6 different monitors connected with wired modems and network cable, with only one modem on computer. Where they all flash semi simultaneously red and black for 60 seconds. I pieced this code together in my research. Its not written for the full run time(would love something simpler than repeating color/clear commands) for testing. But currently it sends the colors to all monitors but only one monitor at a time then moves to the next then the next. I would like it to send it to all at once, what am I doing wrong? Any help is greatly appreciated

local function getPeripherals(sType)
local t = {}
for _,name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == sType then
t[#t + 1] = peripheral.wrap(name)
end
end
return t
end

local t = getPeripherals("monitor")
for i = 1, #t do
t.setBackgroundColor(colors.red)
t.clear()
sleep(0.5)
t.setBackgroundColor(colors.black)
t.clear()
sleep(0.5)
t.setBackgroundColor(colors.red)
t.clear()
sleep(0.5)
t.setBackgroundColor(colors.black)
t.clear()
sleep(0.5)
t.setBackgroundColor(colors.red)
t.clear()
sleep(0.5)
t.setBackgroundColor(colors.black)
t.clear()
sleep(0.5)
end
Lyqyd #2
Posted 28 August 2013 - 10:45 PM
The code is doing exactly what you told it to do. Take a look at your for loop. It takes the first monitor, does all this stuff to it, then does it all with the next, and the next, etc.
diealone #3
Posted 28 August 2013 - 11:57 PM
Thanks and that is very true but I basically am asking what code do I need to change for this action. Is it something I just need to adjust or do I need to go about it a whole nother way? Truth is I dont understand entirely what the above code is doing. Like I said I copied the code from other similar projects and dont know what it does, because its close to what I want. I have read hundreds or posts and articles about similar things and dont get it yet. Nor have I found alot of info thats shows many do. I just would like to get pointed in the right direction so I can learn to fix this quickly.
reububble #4
Posted 29 August 2013 - 01:18 AM
You need to get rid of the sleeping between each monitor process.
Make a loop around your process that will sleep AFTER cycling through each monitor.
Try a similar code to this

for j = 1, X do
   for i = 1, #t do
	 t[i].setBackgroundColor(colors.red)
	 t[i].clear()
   end
   sleep(0.5)
   for i = 1, #t do
	 t[i].setBackgroundColor(colors.black)
	 t[i].clear()
   end
   sleep(0.5)
end
You could shorten this but for your understanding I've left in most of your original code.
Lyqyd #5
Posted 29 August 2013 - 01:19 AM
You should learn to fix it slowly instead. Slow down and take the time to understand what the code is doing and it should become clearer how to change it to achieve the desired effect.
reububble #6
Posted 29 August 2013 - 01:29 AM
Feel the heart of the codes -_-/> like a well oiled engine your program will run smoothly with enough TLC
Engineer #7
Posted 29 August 2013 - 06:56 AM
When you do understand this code, I suggest that you do something like this somewhere:

for c = 0, 15 do
  for i = 1, #t do
    t[i].setBackgroundColor( 2 ^ c )
    t[i].clear()
  end
  sleep( 1 )
end

it basically then just rotaties through all the colors available in CC, bit I just think this is a neat little trick :)/>
kreezxil #8
Posted 06 September 2013 - 01:23 PM
-snip-

Truth is I dont understand entirely what the above code is doing.

-snip-

A good place to get the basics of lua down pat in a hurry without any downloads is at http://www.locofilm.co.uk/LuaCow/