LoginSignup
1
0

More than 1 year has passed since last update.

Grove IoT スターターキット for SORACOM で磁気スイッチを使う

Last updated at Posted at 2021-08-05

プログラムを動かして、磁石がついてない状態でLEDは青、
磁石をつけると LED が赤くなります。
磁気スイッチの Grove を D38 に接続します。

magnetic_switch/magnetic_switch.ino
// ---------------------------------------------------------------
/*
    magnetic_switch.ino

                    Aug/05/2021
*/
// ---------------------------------------------------------------
#include <WioLTEforArduino.h>

#define MAGNETIC_SWITCH_PIN (WIOLTE_D38)
#define INTERVAL            (100)

WioLTE Wio;
int icount = 0;
// ---------------------------------------------------------------
void setup()
{
    Wio.Init(); 

    pinMode(MAGNETIC_SWITCH_PIN, INPUT);
}

// ---------------------------------------------------------------
void loop()
{
    int switchState = digitalRead(MAGNETIC_SWITCH_PIN);
    SerialUSB.print(switchState ? '*' : '.');

    if (switchState)
        {
        Wio.LedSetRGB(1, 0, 0);
        }
    else
        {
        Wio.LedSetRGB(0, 0, 1);
        }

    delay(INTERVAL);

    if ((icount % 40) == 0)
        {
        SerialUSB.println("");
        }
    icount++;
}

// ---------------------------------------------------------------

磁気スイッチが OFF

IMG_20210806_075745.jpg

磁気スイッチが ON

IMG_20210806_075805.jpg

1
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
1
0