int led = 2;
int buz = 3;
int dotTime = 100;
int dashTime = 250;
int spaceTime = 150;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(buz, OUTPUT);
Serial.begin(9600);
digitalWrite(buz, LOW);
digitalWrite(led, LOW);
Serial.println("Write something");
delay((500));
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
char receivedChar = Serial.read();
space();
morse(receivedChar);
Serial.println(receivedChar);
}
}
void dot() {
int startTime = millis();
digitalWrite(led, HIGH);
do {
digitalWrite(buz, HIGH);
delay(1);
digitalWrite(buz, LOW);
delay(1);
}
while (millis() - startTime < dotTime);
digitalWrite(led, LOW);
delay(spaceTime);
}
void dash() {
int startTime = millis();
digitalWrite(led, HIGH);
do {
digitalWrite(buz, HIGH);
delay(1);
digitalWrite(buz, LOW);
delay(1);
}
while (millis() - startTime < dashTime);
digitalWrite(led, LOW);
delay(spaceTime);
}
void space()
{
delay(spaceTime);
delay(spaceTime);
}
void longSpace()
{
delay(6 * spaceTime);
}
void morse(char x)
{
if (x == 'a'||x=='A')
{
dot(); dash();
}
else if (x == 'b'||x=='B')
{
dash(); dot(); dot(); dot();
}
else if (x == 'c'||x=='C')
{
dash(); dot(); dash(); dot();
}
else if (x == 'd'||x=='C')
{
dash(); dot(); dot();
}
else if (x == 'e'||x=='E')
{
dot();
}
else if (x == 'f'||x=='F')
{
dot(); dot(); dash(); dot();
}
else if (x == 'g'||x=='G')
{
dash(); dash(); dot();
}
else if (x == 'h'||x=='H')
{
dot(); dot(); dot(); dot();
}
else if (x == 'i'||x=='I')
{
dot(); dot();
}
else if (x == 'j'||x=='J')
{
dot(); dash(); dash(); dash();
}
else if (x == 'k'||x=='K')
{
dash(); dot(); dash();
}
else if (x == 'l'||x=='L')
{
dot(); dash(); dot(); dot();
}
else if (x == 'm'||x=='M')
{
dash(); dash();
}
else if (x == 'n'||x=='N')
{
dash(); dot();
}
else if (x == 'o'||x=='O')
{
dash(); dash(); dash();
}
else if (x == 'p'||x=='P')
{
dot(); dash(); dash(); dot();
}
else if (x == 'q'||x=='Q')
{
dash(); dash(); dot(); dash();
}
else if (x == 'r'||x=='R')
{
dot(); dash(); dot();
}
else if (x == 's'||x=='S')
{
dot(); dot(); dot();
}
else if (x == 't'||x=='T')
{
dash();
}
else if (x == 'u'||x=='U')
{
dot(); dot(); dash();
}
else if (x == 'v'||x=='V')
{
dash(); dash(); dash(); dot();
}
else if (x == 'w'||x=='W')
{
dot(); dash(); dash();
}
else if (x == 'x'||x=='X')
{
dash(); dot(); dot(); dash();
}
else if (x == 'y'||x=='Y')
{
dash(); dot(); dash(); dash();
}
else if (x == 'z'||x=='Z')
{
dash(); dash(); dot(); dot();
}
else if (x == ' ')
{
longSpace();
}
}