Last active
December 1, 2021 16:25
-
-
Save KeitetsuWorks/1aa8ea0065af0815fdcd1b33b0d2ced6 to your computer and use it in GitHub Desktop.
Arduino Sample Sketch: Blink LED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file leds_blink_millis.ino | |
* @brief Blink LED | |
* @author Keitetsu | |
* @date 2021/12/01 | |
* @copyright Copyright (c) 2021 Keitetsu | |
* @par License | |
* This software is released under the MIT License. | |
*/ | |
#define LED_RED_PIN 10 /**< 赤色LEDを接続したデジタルピン */ | |
#define LED_AMBER_PIN 11 /**< 橙色LEDを接続したデジタルピン */ | |
#define LED_GREEN_PIN 12 /**< 橙色LEDを接続したデジタルピン */ | |
#define LED_OFF HIGH /**< LEDはHIGHで消灯 */ | |
#define LED_ON LOW /**< LEDはLOWで点灯 */ | |
/** | |
* @class BlinkLED | |
* @brief LED点滅クラス | |
*/ | |
class BlinkLED | |
{ | |
/** | |
* @enum led_state_e | |
* @brief LEDの状態 | |
* | |
* @typedef LED_STATE_E | |
* @brief LEDの状態 | |
*/ | |
typedef enum led_state_e | |
{ | |
LED_BLINK_OFF = 0, /**< LED点滅中,LED消灯 */ | |
LED_BLINK_ON /**< LED点滅中,LED点灯 */ | |
} LED_STATE_E; | |
private: | |
uint8_t m_pin; /**< 接続ピン */ | |
unsigned long m_on_time; /**< 点灯時間 */ | |
unsigned long m_off_time; /**< 消灯時間 */ | |
LED_STATE_E m_state; /**< 点灯状態 */ | |
unsigned long m_prev_time; /**< 前回に点灯状態が変化した時刻 */ | |
public: | |
/** | |
* @brief 点滅させるLEDを初期化する | |
* @param[in] pin 接続ピン | |
* @param[in] on_time 点灯時間 | |
* @param[in] off_time 消灯時間 | |
*/ | |
BlinkLED(uint8_t pin, unsigned long on_time, unsigned long off_time); | |
/** | |
* @brief 点灯状態を更新する (周期実行) | |
*/ | |
void update(); | |
}; | |
BlinkLED::BlinkLED(uint8_t pin, unsigned long on_time, unsigned long off_time) | |
{ | |
m_pin = pin; | |
m_on_time = on_time; | |
m_off_time = off_time; | |
pinMode(m_pin, OUTPUT); | |
m_prev_time = millis(); | |
digitalWrite(m_pin, LED_OFF); | |
m_state = LED_BLINK_OFF; | |
return; | |
} | |
void BlinkLED::update() | |
{ | |
unsigned long curr_time; | |
// 現在時刻を取得する | |
curr_time = millis(); | |
switch (m_state) | |
{ | |
case LED_BLINK_ON: | |
if ((curr_time - m_prev_time) >= m_on_time) | |
{ | |
m_prev_time += m_on_time; | |
// LED点滅中,LED消灯にする | |
digitalWrite(m_pin, LED_OFF); | |
m_state = LED_BLINK_OFF; | |
} | |
break; | |
case LED_BLINK_OFF: | |
if ((curr_time - m_prev_time) >= m_off_time) | |
{ | |
m_prev_time += m_off_time; | |
// LED点滅中,LED点灯にする | |
digitalWrite(m_pin, LED_ON); | |
m_state = LED_BLINK_ON; | |
} | |
break; | |
default: | |
break; | |
} | |
return; | |
} | |
/** | |
* @brief セットアップ関数 | |
*/ | |
void setup() | |
{ | |
} | |
/** | |
* @brief ループ関数 | |
*/ | |
void loop() | |
{ | |
// LEDの初期化 | |
static BlinkLED led_red(LED_RED_PIN, 1000, 1000); | |
static BlinkLED led_amber(LED_AMBER_PIN, 800, 200); | |
static BlinkLED led_green(LED_GREEN_PIN, 500, 1500); | |
// LEDの点灯状態を更新する | |
led_red.update(); | |
led_amber.update(); | |
led_green.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment