Skip to content

Instantly share code, notes, and snippets.

@pyu666
Last active December 20, 2017 18:26
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 pyu666/b15578ba8c8bf666b812ed071f9e3e53 to your computer and use it in GitHub Desktop.
Save pyu666/b15578ba8c8bf666b812ed071f9e3e53 to your computer and use it in GitHub Desktop.
arduinoでモールス信号送信 (ついでにシリアルでも送信)
//文字およびモールス符号、符号の長さの定義
String moji = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char tontu[][5] = {"sl", "lsss", "lsls", "lss", "s", "ssls", "lls", "ssss", "ss", "slll", "lsl", "slss", "ll", "ls", "lll", "slls", "llsl", "sls", "sss", "l", "ssl", "sssl", "sll", "lssl", "lsll", "llss", "k"};
//点灯時間の設定
int ton = 100; //単符 (s,1)
int tu = ton * 3; //長符(l,3)
int kyuhu = ton * 3; //文字間空白(3)
int kuhaku = ton * 4; //kyuhu+kuhaku = 単語間空白(k,7)
//LEDピン設定 13
int ledPin = 13;
//表示したい文字列設定(A~Z, 空白)
String tar = "HOGE HOGE";//大文字で入力
void setup() {
pinMode(ledPin, OUTPUT);//13番ピン
}
void loop() {
for (int i = 0; i < tar.length(); i++) { //一文字単位でモールス符号の配列場所読み取り
int s = moji.indexOf(tar[i]); //モールス符号が書かれた文字列の場所が返ってくる(0~)
showChr(s);
}
}
void showChr(int s) { //一文字分の符号読み取りと投げる
int j = 0;
while (tontu[s][j] != '\0') {
showHugou(String(tontu[s][j]));//符号単位でshowHugouに投げる(ついでにChar→String変換)
j++;
}
delay(kyuhu);//文字間空白
}
void showHugou(String chara) { //符号をもらって種類に応じて点灯させる。
int time = 0;
if (chara.equals("s")) {//トン
time = ton;
digitalWrite(ledPin, HIGH);
} else if (chara.equals("l")) {//ツー
time = tu;
digitalWrite(ledPin, HIGH);
} else if (chara.equals("k")) {//空白
time = kuhaku;
} else {
delay(0);
}
delay(time);//符号間休符
digitalWrite(ledPin, LOW);
delay(ton);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment