情報技術の四方山話

AI、IoT、ヤマハルータ、VPN、無線LAN、Linux、クラウド、仮想サーバと情報セキュリティのよもやま話

ChatGPTを使ってArduinoマイコンソフト開発-Arduino/XIAOにコマンドを送るコード

2024-01-03 23:57:57 | AI,IoT,SensorNetworking
Arduino/XIAOにシリアルポートからコマンドを受け付けて、動作を変更するサンプルコード。このような例を見ないので、私がChatGPT4を支援者として使って書いた。計算力・通信力のあるホストから状況に応じてセンサーまたは表示デバイスに情報を送ることができるようになる



使い方:
1.Arduino IDEでコンパイルしArduino/SeeedStudio XIAOにダウンロードし起動
LEDが一秒間隔で点滅
2.Arduino IDEのシリアルモニタから"BLINK"コマンドを送信
LEDが125msec間隔で点滅
3.Arduino IDEのシリアルモニタから"INTERVAL=500"コマンドを送信
LEDが500msec間隔で明滅。値を変えるとその値で点滅

日本語でコメントを書いたが、ChatGPT4から"英語の方がより広く伝わって良いでしょう"と英語に翻訳されたので、そのまま使うことに。コメントはChatGPT3.5でほぼ意味が分かるように日本語に翻訳することができることを確認している

(注)インデントが反映されないのは、ブログ機能の制約。近い将来GitHUBに置きたい


// 2024-01-03 TANIYAMA Ryoji
// This code is released into the public domain.
//
// This code is designed for Arduino / XIAO and is compatible with the Arduino IDE.
//
// If you require comments in Japanese, please refer to translations provided by ChatGPT.
// These translations have been tested for accuracy.

#define INTERVAL_BLINK 125 // Blink interval when in BLINK mode (milliseconds)
#define INTERVAL_BOOTUP 1000 // Blink interval during startup (milliseconds)
#define SERIAL_LINE_96 9600 // Serial communication speed
#define PIN_USER_LED 13 // Pin number for USER LED on Arduino

// Executed only once at startup
void setup() {
pinMode(PIN_USER_LED, OUTPUT); // Set USER LED pin to output mode
Serial.begin(SERIAL_LINE_96); // Begin serial communication (baud rate 9600)
}

// Executed continuously, appearing as if it's always running
// Do not confuse it with being called only once like main()
// Use static declaration for variables that are continuously used within the function
// Global declarations can make the code harder to read
void loop() {
static unsigned long interval = INTERVAL_BOOTUP; // Current blinking interval. Set initial value
static String incomingCommand; // Variable to store received commands

// Check for incoming data on the serial port
if (Serial.available() > 0) {
// Receive data from serial port until newline "\n"
// Returned value does not include "\n"
incomingCommand = Serial.readStringUntil('\n');
// Echo back for debugging purposes. println() method adds "\n"
Serial.println(incomingCommand);
}

// "BLINK" command received
if (incomingCommand == "BLINK") {
// Set interval to predefined BLINK interval
interval = INTERVAL_BLINK;
}

// "INTERVAL=N" command received, where N is in milliseconds
if (incomingCommand.startsWith("INTERVAL=")) {
// Convert string following "=" to a number and set the blinking interval
interval = incomingCommand.substring(9).toInt();
}

// Echo back any change in blinking interval for debugging
IsIntervalChanged(interval);

// Blink the LED with the specified pin number and interval
blinkLed(PIN_USER_LED, interval);
}

// Detect change in blinking interval, echo back the latest value to serial, and return it
int IsIntervalChanged(int interval)
{
static int last_interval; // Holds the current blinking interval to detect changes

if(interval != last_interval) {
Serial.print("Interval set to ");
Serial.print(interval);
Serial.println(" ms");
}

// Hold and return the current blinking interval
return (last_interval = interval);
}

// Function to handle the LED blinking
void blinkLed(int ledPin, int interval) {
digitalWrite(ledPin, HIGH); // Turn ON the LED
delay(interval); // Wait for the specified interval
digitalWrite(ledPin, LOW); // Turn OFF the LED
delay(interval); // Wait for the specified interval
}

いつもアクセスありがとうございます。Arduino/XIAOにシリアルポートからコマンドを送るコードを共有します

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« ChatGPTを使ってArduinoマイ... | トップ | 最新のArduio開発環境と実装... »
最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。

AI,IoT,SensorNetworking」カテゴリの最新記事