Anyone interested in building Micromouse bots with Nerves?

I got the LED’s working. The address detection is not a standard so not always supported. Looks like that is the case here for the SN3218 in this setup. But I was able to write to the address and it was a matter of understanding the flow of how it expects to turn on.

You can dump this in IEx and get a little LED spin action with random colors:

{:ok, i2c} = Circuits.I2C.open("i2c-1")

# Docs say this should be 0xA8 (0b10101000), but the python uses
# this address and it works, so ¯\_(ツ)_/¯ 
addr = 0x54

# Enable LED control
Circuits.I2C.write(i2c, addr, <<0, 1>>)

# I don't know where the LED's are in this control register, so just turn
# them all on - #yolo
Circuits.I2C.write(i2c, addr, <<0x13, 255>>)
Circuits.I2C.write(i2c, addr, <<0x14, 255>>)
Circuits.I2C.write(i2c, addr, <<0x15, 255>>)

loop = fn l ->
  # SN3218 supports 18 LEDs. The Trilobot seems to have 6 RGB leds with
  # each color gamma tied to a single PWM LED register, effectively 
  # making the LED control triads of 3 register addresses
  #
  # |        LED         | Red register | Blue register | Green register |
  # | ------------------ | ------------ | ------------- | -------------- |
  # | LIGHT_FRONT_RIGHT  |      1       |      2        |        3       |
  # | LIGHT_FRONT_LEFT   |      4       |      5        |        6       |
  # | LIGHT_MIDDLE_LEFT  |      7       |      8        |        9       |
  # | LIGHT_REAR_LEFT    |      10      |      11       |        12      |
  # | LIGHT_REAR_RIGHT   |      13      |      14       |        15      |
  # | LIGHT_MIDDLE_RIGHT |      16      |      17       |        18      |
  for led <- 1..6 do
    # SN3218 has auto incrementing register addresses. This just means
    # we calculate the first register (red) and then write 3 bytes which
    # will write 1 byte to the 3 registers, starting with the initial (red)
    reg = led * 3
    rgb = for _ <- 1..3, do: Enum.random(0..255)
    # Adjust the PWM for each RGB of LED N
    Circuits.I2C.write(i2c, addr, [reg | rgb])
    
    # Apply all the settings
    Circuits.I2C.write(i2c, addr, <<0x16, 1>>)
    
    # Little sleep gives a rotating effect on trilobot
    :timer.sleep(100)
  end
  
  l.(l)
end

# Don't block our IEx
led_spin = spawn fn -> loop.(loop) end

tilobot_spin_led

4 Likes