Skip to content

Instantly share code, notes, and snippets.

@shiopon01
Last active July 22, 2016 05:52
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 shiopon01/a03cdf4d4cdfa5bd93f15418c31b1f5e to your computer and use it in GitHub Desktop.
Save shiopon01/a03cdf4d4cdfa5bd93f15418c31b1f5e to your computer and use it in GitHub Desktop.
arduino_morse-code
int incomingByte = 0; // 受信データ用
int ten = 200;
int sen = 600;
int space = 1400;
int ac_minus = 0;
// 32 space
// 48-57 num 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// 65-90 english a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
// 97-122 ENGLISH A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
char* num[10] = {"22222", "12222", "11222", "11122", "11112", "11111", "21111", "22111", "22211", "22221"};
char* english[26] = {"12", "2111", "2121", "211", "1", "1121", "221", "1111", "11", "1222", "212", "1211", "22", "21", "222", "1221", "2212", "121", "111", "2", "112", "1112", "122", "2112", "2122", "2211"};
// 渡された文字コードの信号で光る!
void ascii_blink(int ascii_code)
{
// space
if(32 == ascii_code)
{
delay(space);
}
// num
if((48 <= ascii_code) && (ascii_code <= 57))
{
int len = strlen(num[ascii_code - 48]);
for(int i = 0; i < len; i++)
{
blink(num[ascii_code - 48][i]);
}
}
// english
if( ((65 <= ascii_code) && (ascii_code <= 90)) || ((97 <= ascii_code) && (ascii_code <= 122)) )
{
if((65 <= ascii_code) && (ascii_code <= 90 )) {ac_minus = 65;}
if((97 <= ascii_code) && (ascii_code <= 122)) {ac_minus = 97;}
int len = strlen(english[ascii_code - ac_minus]);
for(int i = 0; i < len; i++)
{
blink(english[ascii_code -ac_minus][i]);
}
}
}
// 渡された数字によって点滅 1:点 2:線
void blink(int standby)
{
digitalWrite(13, HIGH);
delay((standby % 2)? ten : sen);
digitalWrite(13, LOW);
delay(200);
}
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
ascii_blink(incomingByte);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment