本帖最后由 panda 于 2011-12-18 22:41 编辑
今天看书,有个例子是从电脑输入字符,转换为摩斯码,再从led输出的。但书上的例子只有a~z,A~Z,0~9,其他符号没有,所以在网上查了一下摩斯码的电码,然后改了一下程序。
- /*
- Arduino Exp 04
-
- Read input characters from Serial port, and Output Morse code by LED.
- */
- #define ledPin 12
- #define dotDelay 100
- #define dashDelay 300
- char* asc2morse[] ={
- "S","-*-*--","*-**-*","0","***-**-","0","*-***","*----*","-*--*","-*--*-","0","*-*-*","--**--","-****-","*-*-*-","-**-*",
- "-----","*----","**---","***--","****-","*****","-****","--***","---**","----*","---***","-*-*-*","0","-***-","0",
- "**--**","*--*-*","*-","-***","-*-*","-**","*","**-*","--*","****","**","*---","-*-","*-**","--","-*","---","*--*",
- "--*-","*-*","***","-","**-","***-","*--","-**-","-*--","--**","0","0","0","0","0","0","*-","-***","-*-*","-**","*",
- "**-*","--*","****","**","*---","-*-","*-**","--","-*","---","*--*","--*-","*-*","***","-","**-","***-","*--","-**-","-*--","--**",
- }; // ASCII code mapping to Morse code, Start 32,End 122
- void setup() {
- pinMode(ledPin,OUTPUT); // setting LED to
- Serial.begin(9600);
- }
- void flashMorseCode(char c) {
- if(c == 'S') {
- delay(dotDelay*5);
- }else if(c=='*'||c=='-'){
- digitalWrite(ledPin,HIGH);
- switch (c) {
- case '*': delay(dotDelay); // dot
- break;
- case '-': delay(dashDelay); // dash
- break;
- }
- digitalWrite(ledPin,LOW);
- delay(dotDelay);
- }
- }
- void flashSequence(char* seq) {
- int i = 0;
- while ( seq[i]!=NULL) {
- flashMorseCode(seq[i]);
- Serial.write(seq[i]);
- i++;
- }
- delay(dashDelay);
- Serial.write(' ');
- }
- void loop() {
- char ch;
- if(Serial.available()) { // if Usb Serial is available
- ch = Serial.read(); // read char from Serial
- if(ch>31 && ch<123) {
- flashSequence(asc2morse[ch-32]);
- }
- }
- }
复制代码 在原例子上增加了向串口回传摩斯码的语句。
|