【Arduino创意】基于蜂鸣器制作摩尔斯电码生成器

实验现象
从串行监视器输入任何字符,并通过蜂鸣器将其转换为摩尔斯电码等效的蜂鸣声。
理论学习
摩尔斯电码是一种通信系统,用于在两个不同持续时间的信号中编码任何字符,称为点和虚线。
【Arduino创意】基于蜂鸣器制作摩尔斯电码生成器_第1张图片
图1 从A到Z的字母表的摩尔斯电码
实验准备
arduino主板-1
面包板-1
数据线-1
无源蜂鸣器-1
跳线若干
连接电路
【Arduino创意】基于蜂鸣器制作摩尔斯电码生成器_第2张图片
实验代码

char stringToMorseCode[] = "";

int audio8 = 8;      // output audio on pin 8
int note = 1200;      // music note/pitch

int dotLen = 100;     // length of the morse code 'dot'
int dashLen = dotLen * 3;    // length of the morse code 'dash'

void setup() {  
  Serial.begin(9600);              
}

void loop()
{ 
  char inChar = 0;          
  char inData[100] = "";        // data length of 6 characters
  String variable = "";
  String variable1 = "";
  int index1 = 0;
 
  if ( Serial.available() > 0 ) {                      // Read from Rx from atmega16
    while (Serial.available() > 0 && index1 < 100)     // read till 6th character
    {
      delay(100);
      inChar = Serial.read();      // start reading serilly and save to variable
      inData[index1] = inChar;
      index1++;
      inData[index1] = '\0';         // Add a null at the end
    }
    variable.toUpperCase();       // convert to uppercase
    for (byte  i = 0 ; i < 100 ; i++) {
      variable.concat(String(inData[i]));    // concat strings
    }
    delay(20);
  }
  String  stringToMorseCode = String(variable);                          

  for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
  {
  char tmpChar = stringToMorseCode[i];
  tmpChar = toLowerCase(tmpChar);
  GetChar(tmpChar);
  }
}

void MorseDot()
{
  tone(audio8, note, dotLen); // start playing a tone
  delay(dotLen);              // hold in this position
}

void MorseDash()
{
  tone(audio8, note, dashLen);  // start playing a tone
  delay(dashLen);               // hold in this position
}

void GetChar(char tmpChar)
{
  switch (tmpChar) {
    case 'a': 
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'b':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'c':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'd':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100); 
    break;
    case 'e':
    MorseDot();
    delay(100);
    break;
    case 'f':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'g':
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'h':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'i':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'j':
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'k':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'l':
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'm':
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'n':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'o':
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'p':
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 'q':
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'r':
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 's':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    case 't':
    MorseDash();
    delay(100);
    break;
    case 'u':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'v':
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'w':
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'x':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'y':
    MorseDash();
    delay(100);
    MorseDot();
    delay(100);
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);
    break;
    case 'z':
    MorseDash();
    delay(100);
    MorseDash();
    delay(100);   
    MorseDot();
    delay(100);
    MorseDot();
    delay(100);
    break;
    default:
       break;
  }
}

可以安装morse解码的软件:morse code reader,自己生成码,然后自己解码。
实现效果
【Arduino创意】基于蜂鸣器制作摩尔斯电码生成器_第3张图片
相关文章
【Arduino创意】制作摩尔斯电码解码器

你可能感兴趣的:(Arduino)