6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elixir(AtomVM)でデジタルフラッグ 〜 Raspberry Pi Pico 〜

Last updated at Posted at 2025-11-26

今回はElixir(AtomVM)でデジタルフラッグを作ります

前提知識

Raspberry Pi Pico でLチカの知識を前提とします
Lチカの差分です

実行イメージ

配線

  • LED青 GP2
  • LED黄 GP3
  • LED赤 GP4
  • GND 220Ωの抵抗→各LED

Raspberry Pi Pico ピンアサイン参考

Lチカからの差分ソース

mix.exs
defmodule RpiPicoLed.MixProject do
  use Mix.Project

  def project do
    [
      app: :rpi_pico_led,
      version: "0.1.0",
      elixir: "~> 1.17",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      atomvm: [
-       start: RpiPicoLed,
+       start: RpiPicoLed02,
        lash_offset: 0x210000
      ]
    ]
  end

## 省略 ###

lib/rpi_pico_led02.exを追加

lib/rpi_pico_led02.ex
defmodule RpiPicoLed02 do
  @moduledoc """
  Documentation for `RpiPicoLed`.
  """
  @blue_pin 2
  @yellow_pin 3
  @red_pin 4

  # コンパイルする際の警告を抑制する
  # AtomVMのライブラリGPIOが対象です

  def start do
    init(@blue_pin)
    init(@yellow_pin)
    init(@red_pin)
    loop()
  end

  defp loop() do
    on_off(@blue_pin)
    flashes(@yellow_pin, :low, 20)
    on_off(@red_pin)
    loop()
  end

  @compile {:no_warn_undefined, [GPIO]}
  defp init(pin) do
    GPIO.init(pin)
    GPIO.set_pin_mode(pin, :output)
  end

  @compile {:no_warn_undefined, [GPIO]}
  defp on_off(pin) do
    GPIO.digital_write(pin, :high)
    Process.sleep(5000)
    GPIO.digital_write(pin, :low)
  end

  @compile {:no_warn_undefined, [GPIO]}
  defp flashes(pin, _level, 0), do: GPIO.digital_write(pin, :low)

  defp flashes(pin, level, flashes_count) do
    GPIO.digital_write(pin, level)
    Process.sleep(200)
    flashes(pin, toggle(level), flashes_count - 1)
  end

  defp toggle(:high), do: :low
  defp toggle(:low), do: :high
end
  • startがエントリポイントになります

    • GPIOの初期処理が終わったら
      • 各LEDは出力設定にします
    • 無限ループ処理
  • 無限ループ

    • 青LEDは5秒点灯
    • 黄LEDは200ミリ間隔の点滅を20回
    • 赤LEDは5秒点灯

おまけ

毎回ビルドして転送はめんどくさいですよね
スクリプト作りました

cp.sh
#!/bin/sh

mix atomvm.uf2create --family_id rp2040
cp rpi_pico_led.uf2 /media/user/RPI-RP2

ソース

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?