3525| 4
|
[入门] 亮度传感器识别光线摩尔斯代码 |
根据摩尔斯代码表由一个Arduino编辑并发送光摩尔斯代码 另一个arduino学习摩尔斯代码t值,接收并显示信息。 这是发送端代码 volatile int t_base = 200; //莫尔斯电码基准时长 String sent; volatile int sent_l; void setup() { // put your setup code here, to run once: pinMode(2, OUTPUT); digitalWrite(2, LOW); delay(5000); digitalWrite(2, HIGH); delay(t_base); digitalWrite(2, LOW); delay(1000); } void loop() { sent = "welcome to dang jia zhuang school "; sent_l = String(sent).length(); for (int ii = 0; ii < sent_l; ii++) { fanyi(String(sent).charAt(ii)); } } void di() { digitalWrite(2, HIGH); delay(t_base); digitalWrite(2, LOW); delay(t_base); } void da() { digitalWrite(2, HIGH); delay(t_base * 3); digitalWrite(2, LOW); delay(t_base); } void fujian() { digitalWrite(2, LOW); delay(t_base * 3); } void zijian() { digitalWrite(2, LOW); delay(t_base * 6); } void fanyi(char fu) { switch (fu) { case 'a': a(); break; case 'b': b(); break; case 'c': c(); break; case 'd': d(); break; case 'e': e(); break; case 'f': f(); break; case 'g': g(); break; case 'h': h(); break; case 'i': i(); break; case 'j': j(); break; case 'k': k(); break; case 'l': l(); break; case 'm': m(); break; case 'n': n(); break; case 'o': o(); break; case 'p': p(); break; case 'q': q(); break; case 'r': r(); break; case 's': s(); break; case 't': t(); break; case 'u': u(); break; case 'v': v(); break; case 'w': w(); break; case 'x': x(); break; case 'y': y(); break; case 'z': z(); break; case '0': n0(); break; case '1': n1(); break; case '2': n2(); break; case '3': n3(); break; case '4': n4(); break; case '5': n5(); break; case '6': n6(); break; case '7': n7(); break; case '8': n8(); break; case '9': n9(); break; case ' ': zijian(); break; } } void n0() { da(); da(); da(); da(); da(); fujian(); } void n1() { di(); da(); da(); da(); da(); fujian(); } void n2() { di(); di(); da(); da(); da(); fujian(); } void n3() { di(); di(); di(); da(); da(); fujian(); } void n4() { di(); di(); di(); di(); da(); fujian(); } void n5() { di(); di(); di(); di(); di(); fujian(); } void n6() { da(); di(); di(); di(); di(); fujian(); } void n7() { da(); da(); di(); di(); di(); fujian(); } void n8() { da(); da(); da(); di(); di(); fujian(); } void n9() { da(); da(); da(); da(); di(); fujian(); } void a() { di(); da(); fujian(); } void b() { da(); di(); di(); di(); fujian(); } void c() { da(); di(); da(); di(); fujian(); } void d() { da(); di(); di(); fujian(); } void e() { di(); fujian(); } void f() { di(); di(); da(); di(); fujian(); } void g() { da(); da(); di(); fujian(); } void h() { di(); di(); di(); di(); fujian(); } void i() { di(); di(); fujian(); } void j() { di(); da(); da(); da(); fujian(); } void k() { da(); di(); da(); fujian(); } void l() { di(); da(); di(); di(); fujian(); } void m() { da(); da(); fujian(); } void n() { da(); di(); fujian(); } void o() { da(); da(); da(); fujian(); } void p() { di(); da(); da(); di(); fujian(); } void q() { da(); da(); di(); da(); fujian(); } void r() { di(); da(); di(); fujian(); } void s() { di(); di(); di(); fujian(); } void t() { da(); fujian(); } void u() { di(); di(); da(); fujian(); } void v() { di(); di(); di(); da(); fujian(); } void w() { di(); da(); da(); fujian(); } void x() { da(); di(); di(); da(); fujian(); } void y() { da(); di(); da(); da(); fujian(); } void z() { da(); da(); di(); di(); fujian(); } 这是接收端代码 #include <MsTimer2.h> #include <LiquidCrystal_I2C.h> #include <Wire.h> LiquidCrystal_I2C mylcd(0x20, 16, 2); volatile int t = 0;//莫尔斯电码基准时长 volatile int gy = 0;//计时变量 volatile int tmp;//临时存储LED亮度 String mylist = "";//存储滴答 String wrd = ""; //存储单词 volatile boolean i, l; //防止串口监视器连续回车 volatile int temp;//存储每一次的值,1代表嘀3代表嗒 void setup() { mylcd.init(); mylcd.backlight(); Serial.begin(9600); /*计时开始,1毫秒调用一次*/ MsTimer2::set(1, jishi); MsTimer2::start( ); tmp = analogRead(A0); } void loop() { if (t > 0) { /*如果亮度大于1000,开始计时 */ if (analogRead(A0) >= 1000) { /*如果上一次测得亮度小于1000,计时器归零, 也就是看是不是由暗变亮*/ if (tmp < 1000) { gy = 0; } } else { /*如果亮度小于10,开始计时 */ //if (analogRead(A0) < 10){ /*如果上一次测得亮度大于10,计时器归零, 也就是看是不是由亮变暗*/ if (tmp >= 1000) { mylcd.clear(); mylcd.setCursor(1 - 1, 1 - 1); mylcd.print(gy); mylcd.setCursor(1 - 1, 2 - 1); temp = round(gy / t); if (temp > 3) temp = 3; mylist = mylist + String(temp); mylcd.print(temp); Serial.print(temp); gy = 0;//计算间隔有多长 i = true;//防止串口监视器连续回车 l = true; } if (gy > 4 * t && l) { Serial.println(); l = false; mylcd.setCursor(5 - 1, 1 - 1); mylcd.print(wrd); wrd = ""; } else if (gy > 2 * t && i) { Serial.println(); i = false; mylcd.setCursor(4 - 1, 2 - 1); mylcd.print(mylist); char jieguo = zidian(mylist); mylist = ""; wrd = wrd + String(jieguo); mylcd.setCursor(10 - 1, 2 - 1); mylcd.print(jieguo); } } tmp = analogRead(A0); delay(5); } else { mylcd.setCursor(1 - 1, 1 - 1); mylcd.print("Learning T"); mylcd.setCursor(1 - 1, 2 - 1); mylcd.print("T=?"); t = check_t(); mylcd.setCursor(1 - 1, 2 - 1); mylcd.print("T="+String(t)); } } //计时函数 void jishi( ) { gy++; } //检测T值 int check_t() { int ch = 0; boolean tiaochu = true, tou = false; while (tiaochu) { if (!tou && analogRead(A0) < 10) ch++; if (!tou && analogRead(A0) >= 1000) ch = 0; if (ch >= 4990) { if (analogRead(A0) >= 1000) { if (tmp < 1000) { gy = 0; } } else { if (tmp >= 1000) { tiaochu = false; } } tou = true; } tmp = analogRead(A0); delay(1); } return round(gy / 10) * 10; } //字典 char zidian(String jieshou) { int x = jieshou.toInt(); char fanhui; switch (x) { case 33333: fanhui = '0'; break; case 13333: fanhui = '1'; break; case 11333: fanhui = '2'; break; case 11133: fanhui = '3'; break; case 11113: fanhui = '4'; break; case 11111: fanhui = '5'; break; case 31111: fanhui = '6'; break; case 33111: fanhui = '7'; break; case 33311: fanhui = '8'; break; case 33331: fanhui = '9'; break; case 13: fanhui = 'A'; break; case 3111: fanhui = 'B'; break; case 3131: fanhui = 'C'; break; case 311: fanhui = 'D'; break; case 1: fanhui = 'E'; break; case 1131: fanhui = 'F'; break; case 331: fanhui = 'G'; break; case 1111: fanhui = 'H'; break; case 11: fanhui = 'I'; break; case 1333: fanhui = 'J'; break; case 313: fanhui = 'K'; break; case 1311: fanhui = 'L'; break; case 33: fanhui = 'M'; break; case 31: fanhui = 'N'; break; case 333: fanhui = 'O'; break; case 1331: fanhui = 'P'; break; case 3313: fanhui = 'Q'; break; case 131: fanhui = 'R'; break; case 111: fanhui = 'S'; break; case 3: fanhui = 'T'; break; case 113: fanhui = 'U'; break; case 1113: fanhui = 'V'; break; case 133: fanhui = 'W'; break; case 3113: fanhui = 'X'; break; case 3133: fanhui = 'Y'; break; case 3311: fanhui = 'Z'; break; default: fanhui = '?'; break; } return fanhui; } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed