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

Help with detecting peripherals

Started by WolfyXK, 23 December 2015 - 04:17 PM
WolfyXK #1
Posted 23 December 2015 - 05:17 PM
I have wrote a program to control colorful lights from the computronics mod which are placed on a christmas tree the code currently looks like this.


print("|WolfOS| Christmas Light Controller")

while true do

local random = math.random(6,53) -- Chooses a random lamp
local color = math.random(0,32767) -- Chooses a random color

lamp = peripheral.wrap("colorful_lamp_" .. random) -- Connect to selected lamp
lamp.setLampColor(color) -- Set selected lamps color to selected color

print("Lamp " .. random .. " changed to color " .. color)

sleep(0.5)
end

I currently have to manually set the name of the lights I have connected, I was wondering if there was any way to automatically detect them, Thanks again. - WolfyXK

Lupus590 #2
Posted 23 December 2015 - 06:45 PM
http://computercraft...Peripheral.find

this should do what you need
Edited on 23 December 2015 - 05:45 PM
WolfyXK #3
Posted 23 December 2015 - 06:57 PM
Thanks for the reply Lupus, how would I then change the colour of the lights now that they are in a table.
Lyqyd #4
Posted 23 December 2015 - 07:54 PM
Moved to Ask a Pro.
zguystudios #5
Posted 23 December 2015 - 08:51 PM
Perhaps this segment of code may be of some help

local perilist = peripheral.getNames()
for i=1,#perilist do
  if peripheral.getType(perilist[i]) == "colorful_lamp" then -- or whatever the lamps are called
	local color = math.random(0,32767) -- Chooses a random color
	local lamp = peripheral.wrap(perilist[i]) -- Connect to selected lamp
	lamp.setLampColor(color) -- Set selected lamps color to selected color
  end
end

Also, this code will work with any lamps attached via a wired modem.

<EDIT> If this code doesn't work, please let me know and I will work on another solution.
Edited on 23 December 2015 - 07:54 PM
Dragon53535 #6
Posted 23 December 2015 - 09:43 PM

local tbl = {}

--#Find and automatically wrap our lights.
for a,v in pairs(peripheral.getNames()) do
  if (peripheral.getType(v) == "colorful_lamp" ) then
    tbl[#tbl + 1] = peripheral.wrap(v)
  end
end

--#Choose a random light from the wrapped lights and change it's color
local random = math.random(1,#tbl)
tbl[random].setLampColor(color)
TheOddByte #7
Posted 23 December 2015 - 10:39 PM
I'd like to point out that this

local color = math.random( 0, 32767 )
would be better to change into this IMO

local color = 2 ^ math.random( 0, 15 )
because the code above gets the exact same values as the colors have on the wiki, and probably has a higher chance of getting more random colors because some values are probably going to give the same color if you use the on you're using. And I'm not a 100% sure, but the way you're currently choosing the color I belive it can error.
zguystudios #8
Posted 07 January 2016 - 08:48 PM

local tbl = {}

--#Find and automatically wrap our lights.
for a,v in pairs(peripheral.getNames()) do
  if (peripheral.getType(v) == "colorful_lamp" ) then
	tbl[#tbl + 1] = peripheral.wrap(v)
  end
end

--#Choose a random light from the wrapped lights and change it's color
local random = math.random(1,#tbl)
tbl[random].setLampColor(color)
This vaguely looks like my code, however…
It's much more amazing. You took a way in which I never would've thought possible. Guess I got to brush up on programming.
Lyqyd #9
Posted 07 January 2016 - 09:04 PM
I'm not sure why Dragon53535 didn't just use peripheral.find to grab the table of wrapped peripherals, though.
TheOddByte #10
Posted 07 January 2016 - 09:07 PM
And to make it even more awesome, you could cramp these lines

local random = math.random(1,#tbl)
tbl[random].setLampColor(color)
into this

tbl[math.random( #tbl )].setLampColor( 2 ^ math.random( 0, 15 ) )
Dragon53535 #11
Posted 07 January 2016 - 09:10 PM
Because I'm still not used to using peripheral.find… I find that using peripheral.getNames/modem.getNamesRemote is more natural to me, even though I know peripheral.find exists.


Edit: Gonna rephrase that, peripheral.getNames and modem.getNamesRemote are more used by me, so I remember them more than I remember peripheral.find.
Edited on 07 January 2016 - 08:16 PM
TheOddByte #12
Posted 08 January 2016 - 01:38 AM
- snip -
The only thing that actually is better with using peripheral.getNames this way now is backwards-compability, and I have to say that you're not the only one still doing this.
I find my self doing this when I want to find if a modem is attached and I want to make sure it's wireless.
Lyqyd #13
Posted 08 January 2016 - 05:28 AM
There are a limited number of versions that it's backwards compatible with, though. If you wanted full backwards compatibility, you'd be limited to rs.getSides. May as well either use peripheral.find or go all-out and use an if tree to select the latest available method for whatever version of CC it's run under.
TheOddByte #14
Posted 08 January 2016 - 03:29 PM
- snip -
If you'd really want to have full backwards compability you'd probably want to do something like this then? I was bored and wrapped up this code snippet because, well… I was bored.

local a, b = {}, "monitor"
if peripheral.find then
    a = peripheral.find( b )

elseif peripheral.getNames then
    for i, v in ipairs( peripheral.getNames() ) do
        if peripheral.getType( v ) == b then
            table.insert( a, v )
        end
    end

else
    for i, v in ipairs( rs.getSides() ) do
        if peripheral.getType( v ) == b then
            table.insert( a, v )
        end
    end
end
Lyqyd #15
Posted 08 January 2016 - 04:59 PM
Yep, except the second two would need to wrap v for consistent results. Could probably collapse the second two options like so:


else
  local func = peripheral.getNames or rs.getSides
  for _, v in ipairs(func()) do

Other than that little space saver, you've got probably the best solution there, if one cares about backwards compatibility.