Last active
February 13, 2021 16:38
-
-
Save KeitetsuWorks/ec331db76ee58365acb4a9c874967bc7 to your computer and use it in GitHub Desktop.
LED Controller
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 conv.ino | |
* @brief LED Controller | |
* @author Keitetsu | |
* @date 2021/02/13 | |
* @copyright Copyright (c) 2021 Keitetsu | |
* @par License | |
* This software is released under the MIT License. | |
*/ | |
#define LED_PIN 3 /**< LEDを接続するanalogWrite対応ピン */ | |
#define LED_DUTY_MIN 0 /**< LEDの最小デューティ比.消灯 */ | |
#define LED_DUTY_MAX 255 /**< LEDの最大デューティ比.最大輝度で点灯 */ | |
/** | |
* @brief セットアップ関数 | |
*/ | |
void setup() | |
{ | |
Serial.begin(9600); | |
// LEDのPWM制御のデューティ比を0に設定する | |
analogWrite(LED_PIN, LED_DUTY_MIN); | |
} | |
/** | |
* @brief ループ関数 | |
*/ | |
void loop() | |
{ | |
String line; // 受信文字列 | |
int line_len; // 受信文字列の長さ | |
long led_duty; // LEDのPWM制御のデューティ比 | |
// シリアル通信で1行(セミコロンまで)読み込む | |
line = Serial.readStringUntil(';'); | |
// 1行の文字数を取得する | |
line_len = line.length(); | |
// 文字列が1文字から3文字の間であれば,文字列を整数に変換する | |
if ((line_len >= 1) && (line_len <= 3)) { | |
// 文字列を整数に変換する | |
led_duty = line.toInt(); | |
// デバッグ用文字列をシリアル通信で送信する | |
Serial.print("LED Duty: "); | |
Serial.println(led_duty); | |
// 整数がLED_DUTY_MIN以上LED_DUTY_MAX以下であれば,LEDのデューティ比として設定する | |
if ((led_duty >= LED_DUTY_MIN) && (led_duty <= LED_DUTY_MAX)) { | |
analogWrite(LED_PIN, led_duty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment