Skip to content

Instantly share code, notes, and snippets.

@KeitetsuWorks
Last active February 13, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeitetsuWorks/ec331db76ee58365acb4a9c874967bc7 to your computer and use it in GitHub Desktop.
Save KeitetsuWorks/ec331db76ee58365acb4a9c874967bc7 to your computer and use it in GitHub Desktop.
LED Controller
/**
* @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