1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#include <SoftwareSerial.h>
/*
Implementes the Morse algorithm for a simple speaker
@author Felix
@date 07.05.2011
*/
int inByte = 0;
int i = 0;
int j = 0;
int char_avail = 0;
/* You can touch this: */
#define DIT_DELAY 50
/* But you cannot touch this: */
#define WORD_DELAY (DIT_DELAY * 7)
#define DIT_CYCLES (DIT_DELAY/2)
#define CHAR_DELAY (DIT_DELAY * 3)
#define dsym() delay(DIT_DELAY);
#define char_delay() delay(CHAR_DELAY);
#define word_delay() delay(WORD_DELAY);
static char* latin_upper[] = {
". -\0" , /* A */
"- . . .\0"
"- . . .\0",
"- . - .\0",
"- . .\0",
".\0",
". . - .\0",
"- - .\0",
". . . .\0",
". .\0",
". - - -\0",
"- . -\0",
". - . .\0",
"- -\0",
"- .\0",
"- - -\0",
". - - .\0",
"- - . -\0",
". - .\0",
". . .\0",
"-\0",
". . -\0",
". . . -\0",
". - -\0",
"- . . -\0",
"- . - -\0",
"- - . .\0" /* Z */
};
static char* numbers[] = {
"- - - - -\0", /* 0 */
". - - - -\0",
". . - - -\0",
". . . - -\0",
". . . . -\0",
". . . . .\0",
"- . . . .\0",
"- - . . .\0",
"- - - . .\0",
"- - - - .\0" /* 9 */
};
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
char_avail = Serial.available();
if(char_avail > 0) {
for (j = 0; j < char_avail;j++)
{
char on_line = Serial.read();
to_beep(on_line);
char_delay();
}
}
}
void to_beep(char data)
{
char* mdata;
if (data == ' ')
{
word_delay();
Serial.println("word end");
return;
}
else{
if (data == '\n') {
word_delay();
Serial.println("EOL");
return;
} else
if (data >= 'A' && data <= 'Z' )
mdata = latin_upper[data-'A'];
else
if (data >= 'a' && data <= 'z' )
mdata = latin_upper[data-'a'];
else
if (data >= '0' && data <= '9')
mdata = numbers[data-'0'];
else
{
Serial.print(data);
Serial.println(": not implemented");
return;
}
}
Serial.println(mdata);
int sdata = strlen( mdata);
for( char i = 0; i < sdata; i ++)
{
char token = mdata[i];
if (token == '.')
{
dit();
}
if (token == '-')
{
dah();
}
if (token == ' ')
{
dsym();
}
}
}
void dah() {
for (int i=0;i<3;i++)
{
dit();
}
}
void dit(){
for (int i=0;i<DIT_CYCLES;i++) // Number of DIT Cycles , sleep 2 milliseconds in a cycle
{
digitalWrite(13, HIGH); // set the LED on
delay(1);
digitalWrite(13, LOW); // set the LED off
delay(1);
}
}
|