Advertisement
Guest User

Arduino Uno, 4x4 keypad, 16x2 lcd, morse encoder.

a guest
Feb 17th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.10 KB | None | 0 0
  1. #include <LiquidCrystal.h> //include the lcd library
  2.  
  3. const int unit=60;
  4. const byte morseoutpin=2;
  5. char selectedletter;
  6.  
  7. const byte row1=3; //keypad row pins, shared with the lcd data lines
  8. const byte row2=4;
  9. const byte row3=5;
  10. const byte row4=6;
  11. const byte col1=7; //keypad column lines
  12. const byte col2=8;
  13. const byte col3=9;
  14. const byte col4=10;
  15. LiquidCrystal lcd(12, 11, row4, row3, row2, row1); //lcd object
  16. byte numpressed; //latest number pressed on the keypad
  17. byte timespressed; //times the number has been pressed
  18. byte cursorx=0; //cursor x position
  19. byte cursory=0; //cursor y position
  20. char letter; //stores letter that needs to be printed to the lcd
  21. const int wait=1000; //time to wait for additional presses to same number
  22. const int preventholddelay=150; //time to wait to prevent cycling through things too quickly
  23. unsigned long basetime; //base time for while loop
  24. unsigned long elapsed=0; //elapsed time in while loop
  25. byte lastnumpressed; //the initial number pressed on the keypad
  26. bool disablespacedelay=false; //disables the delay in the space function in the case that a different number is pressed while in while loop
  27. const byte maxtimespressed[16]={
  28. 1,3,4,4,4,4,4,4,4,4,1,7,1,1,1,1}; //stores maximum number of times any given key can be pressed before looping back to its first letter (used by incrementtimespressed function)
  29. char typedtext[140]; //stores typed text for printout to the serial console
  30. int positionintypedtext=0; //position in typedtext character array
  31. int charremaining; //remaining characters in message
  32. bool promptkeypress=false; //used for waiting for, and keypress detection, at the end-of composition prompt
  33.  
  34. void setup(){
  35. Serial.begin(115200); //start serial communication so that we can print the typed text to the serial console
  36. lcd.begin(16,2); //initialize the lcd
  37. lcd.setCursor(cursorx,cursory); //set the lcd cursor
  38. lcd.noCursor(); //turn off the cursor
  39. pinMode(morseoutpin, OUTPUT);
  40. pinMode(row1,OUTPUT); //set the rows as outputs
  41. pinMode(row2,OUTPUT);
  42. pinMode(row3,OUTPUT);
  43. pinMode(row4,OUTPUT);
  44. pinMode(col1,INPUT); //set the columns as inputs
  45. pinMode(col2,INPUT);
  46. pinMode(col3,INPUT);
  47. pinMode(col4,INPUT);
  48. delay(wait);
  49. lcd.cursor(); //turn the cursor on again after a delay equivalent to wait
  50. rowshigh(); //sets all rows high
  51. }
  52.  
  53. void loop(){
  54. numpressed=16; //reset "numpressed" (16 doesn't refer to any button on the pad)
  55. if (findpress()){ //look for presses, if one has occurred, identify it and continue
  56. timespressed=0; //reset "timespressed"
  57. if (numpressed==0){ //if zero on the pad was pressed,
  58. dozero(); //print zero
  59. letter='0'; //manually seed a zero as the character for the text storage
  60. textstorage(1); //regular character storage
  61. }
  62. if (numpressed==10){ //if shift on the pad was pressed,
  63. textstorage(2); //perform a space in storage
  64. dospace(); //do a space
  65. }
  66. if (numpressed==14){ //if the arrows on the pad were pressed,
  67. textstorage(3); //perform a backspace in storage
  68. dobackspace(); //do a backspace
  69. }
  70. if (numpressed==15){ //if enter on the pad was pressed,
  71. outputserial(); //output message and display remaining characters to serial console
  72. }
  73. if ((numpressed<10&&numpressed>0)||numpressed==11){ //if 1,2,3,4,5,6,7,8,9, or 11 was pressed (any one of the keys with multiple characters assigned),
  74. lastnumpressed=numpressed; //record which number was pressed,
  75. basetime=millis(); //and take a base time for the while loop
  76. while (elapsed<wait){ //while below the time to wait,
  77. if(findpress()){ //look for presses, if one has occurred, identify it and continue
  78. if (numpressed==lastnumpressed){ //if it was the same as before,
  79. incrementtimespressed(); //increment "timespressed"
  80. basetime=basetime+(wait-(wait-elapsed)); //roll up the base time, to allow another wait period until next press of the same button
  81. definepress(); //use "numpressed" and "timespressed" to define "letter"
  82. lcd.print(letter); //print the letter that was defined
  83. lcd.setCursor(cursorx,cursory); //maintain cursor position
  84. rowshigh(); //return all rows high
  85. delay(preventholddelay); //delay a little to prevent continuous cycling through "timespressed" during a single press
  86. }
  87. else{ //if the number that was pressed was different than before,
  88. disablespacedelay=true; //disable the delay in the space function to allow the press to be detected a second time, at the beginning of void loop
  89. break; //break out of the while loop
  90. }
  91. }
  92. elapsed=millis()-basetime; //refresh the elapsed time for the while loop
  93. }
  94. elapsed=0; //reset the elapsed time for the while loop
  95. textstorage(1); //store character
  96. dospace(); //do a space
  97. }
  98. }
  99. if (positionintypedtext==139){ //if the end of the stored text has been reached,
  100. promptkeypress=false; //reset keypress detection
  101. cursorx=0; //set cursor to the beginning of first row
  102. cursory=0;
  103. lcd.setCursor(cursorx,cursory);
  104. lcd.print("Msg end. <>=back"); //print this out to the lcd
  105. cursorx=0; //set cursor to the beginning of second row
  106. cursory=1;
  107. lcd.setCursor(cursorx,cursory);
  108. lcd.print("enter=serial out"); //print this out to the lcd
  109. rowshigh(); //sets all rows high
  110. numpressed=16; //reset "numpressed" (16 doesn't refer to any button on the pad)
  111. while(!promptkeypress){ //while no relevant keypresses have occurred,
  112. if (findpress()){ //look for presses, if one has occurred, identify it and continue
  113. timespressed=0; //reset "timespressed"
  114. if (numpressed==14){ //if the arrows on the pad were pressed,
  115. promptkeypress=true; //take note so that the while loop can be broken
  116. textstorage(3); //perform a backspace in storage
  117. for (int i=0;i<16;i++){ //print out to the first line on the lcd from the stored text
  118. cursorx=i;
  119. cursory=0;
  120. lcd.setCursor(cursorx,cursory);
  121. lcd.print(typedtext[108+i]);
  122. }
  123. for (int j=0;j<16;j++){ //print out to the second line on the lcd from the stored text
  124. cursorx=j;
  125. cursory=1;
  126. lcd.setCursor(cursorx,cursory);
  127. lcd.print(typedtext[123+j]);
  128. }
  129. cursorx=15; //set cursor to the beginning of second row
  130. cursory=1;
  131. lcd.setCursor(cursorx,cursory);
  132. rowshigh(); //sets all rows high
  133. }
  134. if (numpressed==15){ //if enter on the pad was pressed,
  135. promptkeypress=true; //take note so that the while loop can be broken
  136. Serial.print("Final message: "); //print this to the serial console
  137. Serial.println(typedtext); //print out all the text typed so far to the serial console
  138. Serial.println(); //print a blank line
  139. sendmorse();
  140. for (int i=0;i<140;i++){ //write all positions in the stored text to be blank
  141. typedtext[i]=' ';
  142. }
  143. positionintypedtext=0; //reset the position in the stored text to the beginning
  144. doclear();
  145. rowshigh(); //sets all rows high
  146. }
  147. }
  148. }
  149. delay(preventholddelay); //delay a little to prevent continuous cycling
  150. }
  151. }
  152.  
  153. void rowshigh(){ //sets all rows high
  154. digitalWrite(row1,HIGH); //write all the rows high
  155. digitalWrite(row2,HIGH);
  156. digitalWrite(row3,HIGH);
  157. digitalWrite(row4,HIGH);
  158. }
  159.  
  160. bool findpress(){ //finds a press to define "numpressed", if any press occurs, returns true
  161. bool pressfound=false; //variable for any press detection, is returned by this function
  162.  
  163. digitalWrite(row1,LOW); //write all rows low
  164. digitalWrite(row2,LOW);
  165. digitalWrite(row3,LOW);
  166. digitalWrite(row4,LOW);
  167.  
  168. digitalWrite(row1,HIGH); //write first row high
  169. if (digitalRead(col1)==HIGH){ //if the first column is now high, "1" has been pressed
  170. numpressed = 1;
  171. pressfound=true;
  172. }
  173. if (digitalRead(col2)==HIGH){ //if the second column is now high, "2" has been pressed
  174. numpressed = 2;
  175. pressfound=true;
  176. }
  177. if (digitalRead(col3)==HIGH){ //if the third column is now high, "3" has been pressed
  178. numpressed = 3;
  179. pressfound=true;
  180. }
  181. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "reset" has been pressed
  182. numpressed = 12;
  183. pressfound=true;
  184. }
  185. digitalWrite(row1,LOW); //return first row low
  186.  
  187. digitalWrite(row2,HIGH); //write second row high
  188. if (digitalRead(col1)==HIGH){ //if the first column is now high, "4" has been pressed
  189. numpressed = 4;
  190. pressfound=true;
  191. }
  192. if (digitalRead(col2)==HIGH){ //if the second column is now high, "5" has been pressed
  193. numpressed = 5;
  194. pressfound=true;
  195. }
  196. if (digitalRead(col3)==HIGH){ //if the third column is now high, "6" has been pressed
  197. numpressed = 6;
  198. pressfound=true;
  199. }
  200. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "dial" has been pressed
  201. numpressed = 13;
  202. pressfound=true;
  203. }
  204. digitalWrite(row2,LOW); //return second row low
  205.  
  206. digitalWrite(row3,HIGH); //write third row high
  207. if (digitalRead(col1)==HIGH){ //if the first column is now high, "7" has been pressed
  208. numpressed = 7;
  209. pressfound=true;
  210. }
  211. if (digitalRead(col2)==HIGH){ //if the second column is now high, "8" has been pressed
  212. numpressed = 8;
  213. pressfound=true;
  214. }
  215. if (digitalRead(col3)==HIGH){ //if the third column is now high, "9" has been pressed
  216. numpressed = 9;
  217. pressfound=true;
  218. }
  219. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, the arrows have been pressed
  220. numpressed = 14;
  221. pressfound=true;
  222. }
  223. digitalWrite(row3,LOW); //return third row low
  224.  
  225. digitalWrite(row4,HIGH); //write fourth row high
  226. if (digitalRead(col1)==HIGH){ //if the first column is now high, "shift" has been pressed
  227. numpressed = 10;
  228. pressfound=true;
  229. }
  230. if (digitalRead(col2)==HIGH){ //if the second column is now high, "0" has been pressed
  231. numpressed = 0;
  232. pressfound=true;
  233. }
  234. if (digitalRead(col3)==HIGH){ //if the third column is now high, "." has been pressed
  235. numpressed = 11;
  236. pressfound=true;
  237. }
  238. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "enter" has been pressed
  239. numpressed = 15;
  240. pressfound=true;
  241. }
  242. digitalWrite(row4,LOW); //return fourth row low
  243.  
  244. rowshigh(); //write all rows high
  245.  
  246. return pressfound; //function returns true if any press found, otherwise returns false
  247. }
  248.  
  249. void definepress(){ //uses "lastnumpressed" and "timespressed" to define "letter"
  250. if (lastnumpressed==1){
  251. if (timespressed==1){
  252. letter='Q';
  253. }
  254. if (timespressed==2){
  255. letter='Z';
  256. }
  257. if (timespressed==3){
  258. letter='1';
  259. }
  260. }
  261. if (lastnumpressed==2){
  262. if (timespressed==1){
  263. letter='A';
  264. }
  265. if (timespressed==2){
  266. letter='B';
  267. }
  268. if (timespressed==3){
  269. letter='C';
  270. }
  271. if (timespressed==4){
  272. letter='2';
  273. }
  274. }
  275. if (lastnumpressed==3){
  276. if (timespressed==1){
  277. letter='D';
  278. }
  279. if (timespressed==2){
  280. letter='E';
  281. }
  282. if (timespressed==3){
  283. letter='F';
  284. }
  285. if (timespressed==4){
  286. letter='3';
  287. }
  288. }
  289. if (lastnumpressed==4){
  290. if (timespressed==1){
  291. letter='G';
  292. }
  293. if (timespressed==2){
  294. letter='H';
  295. }
  296. if (timespressed==3){
  297. letter='I';
  298. }
  299. if (timespressed==4){
  300. letter='4';
  301. }
  302. }
  303. if (lastnumpressed==5){
  304. if (timespressed==1){
  305. letter='J';
  306. }
  307. if (timespressed==2){
  308. letter='K';
  309. }
  310. if (timespressed==3){
  311. letter='L';
  312. }
  313. if (timespressed==4){
  314. letter='5';
  315. }
  316. }
  317. if (lastnumpressed==6){
  318. if (timespressed==1){
  319. letter='M';
  320. }
  321. if (timespressed==2){
  322. letter='N';
  323. }
  324. if (timespressed==3){
  325. letter='O';
  326. }
  327. if (timespressed==4){
  328. letter='6';
  329. }
  330. }
  331. if (lastnumpressed==7){
  332. if (timespressed==1){
  333. letter='P';
  334. }
  335. if (timespressed==2){
  336. letter='R';
  337. }
  338. if (timespressed==3){
  339. letter='S';
  340. }
  341. if (timespressed==4){
  342. letter='7';
  343. }
  344. }
  345. if (lastnumpressed==8){
  346. if (timespressed==1){
  347. letter='T';
  348. }
  349. if (timespressed==2){
  350. letter='U';
  351. }
  352. if (timespressed==3){
  353. letter='V';
  354. }
  355. if (timespressed==4){
  356. letter='8';
  357. }
  358. }
  359. if (lastnumpressed==9){
  360. if (timespressed==1){
  361. letter='W';
  362. }
  363. if (timespressed==2){
  364. letter='X';
  365. }
  366. if (timespressed==3){
  367. letter='Y';
  368. }
  369. if (timespressed==4){
  370. letter='9';
  371. }
  372. }
  373. if (lastnumpressed==11){
  374. if (timespressed==1){
  375. letter='.';
  376. }
  377. if (timespressed==2){
  378. letter='?';
  379. }
  380. if (timespressed==3){
  381. letter='!';
  382. }
  383. if (timespressed==4){
  384. letter=',';
  385. }
  386. if (timespressed==5){
  387. letter='\'';
  388. }
  389. if (timespressed==6){
  390. letter='"';
  391. }
  392. if (timespressed==7){
  393. letter='-';
  394. }
  395. }
  396. }
  397.  
  398. void incrementtimespressed(){ //increment "timespressed" until at max value stored in maxtimespressed for that lastnumpressed, then roll over to 1
  399. if (timespressed==maxtimespressed[lastnumpressed]){ //if at the maximum,
  400. timespressed=1; //roll over timespressed to one
  401. }
  402. else{ //otherwise,
  403. timespressed++; //increment timespressed
  404. }
  405. }
  406.  
  407. void dozero(){ //prints zero
  408. lcd.print('0'); //print 0
  409. lcd.setCursor(cursorx,cursory); //maintain cursor position
  410. dospace(); //space
  411. }
  412.  
  413. void dospace(){ //moves cursor forward once, wraps to next line if necessary, clears and returns to top of display if at bottom
  414. if (cursory==1){ //if on the bottom row,
  415. if (cursorx==15){ //if at the end of the row,
  416. cursorx=0; //define new cursor position as the upper-left corner
  417. cursory=0;
  418. lcd.clear(); //clear the lcd
  419. }
  420. else{ //otherwise,
  421. cursorx++; //increment the cursor to the right
  422. }
  423. }
  424. else{ //if on the top row,
  425. if (cursorx==15){ //if at the end of the row,
  426. cursorx=0; //define new cursor position as the bottom-left corner
  427. cursory=1;
  428. }
  429. else{ //otherwise,
  430. cursorx++; //increment the cursor to the right
  431. }
  432. }
  433. lcd.setCursor(cursorx,cursory); //set cursor to defined location
  434. rowshigh(); //sets all rows high
  435. if (disablespacedelay){ //if the delay has been disabled,
  436. disablespacedelay=false; //reset its being disabled
  437. }
  438. else{ //otherwise,
  439. delay(preventholddelay); //delay a bit
  440. }
  441. }
  442.  
  443. void doclear(){ //clears and returns to top-left of display
  444. cursorx=0;
  445. cursory=0;
  446. lcd.clear();
  447. lcd.setCursor(cursorx,cursory);
  448. rowshigh(); //sets all rows high
  449. delay(preventholddelay);
  450. }
  451.  
  452. void dobackspace(){ //does a backspace, essentially the opposite of dospace
  453. if (cursory==1){
  454. if (cursorx==0){
  455. cursorx=15;
  456. cursory=0;
  457. }
  458. else{
  459. cursorx--;
  460. }
  461. }
  462. else{
  463. if (cursorx==0){
  464. cursorx=0;
  465. cursory=0;
  466. }
  467. else{
  468. cursorx--;
  469. }
  470. }
  471. lcd.setCursor(cursorx,cursory);
  472. lcd.print(" ");
  473. lcd.setCursor(cursorx,cursory);
  474. rowshigh(); //sets all rows high
  475. delay(preventholddelay);
  476. }
  477.  
  478. void textstorage(byte mode){ //contains functions for text storage
  479. if (mode==1){ //regular character storage
  480. typedtext[positionintypedtext]=letter; //store letter that was printed to LCD in typedtext
  481. positionintypedtext++; //increment position in typedtext
  482. }
  483. if (mode==2){ //do a space in stored text
  484. typedtext[positionintypedtext]=' '; //set current position in typedtext to a space
  485. positionintypedtext++; //increment position in typedtext
  486. }
  487. if (mode==3){ //does a backspace in the stored text
  488. positionintypedtext--; //decrement position in typedtext
  489. typedtext[positionintypedtext]=' '; //set current position in typedtext to a space
  490. }
  491. }
  492.  
  493. void outputserial(){ //output message and display remaining characters to serial console
  494. sendmorse();
  495. Serial.print("Message: ");
  496. Serial.println(typedtext); //print out all the text typed so far to the serial console
  497. charremaining=(139-(positionintypedtext)); //calculate remaining characters
  498. Serial.print(charremaining); //display remaining characters
  499. if (charremaining==1){ //making sure that the plural is only used correctly
  500. Serial.println(" character remaining");
  501. }
  502. else{
  503. Serial.println(" characters remaining");
  504. }
  505. Serial.println(); //print a blank line
  506.  
  507. delay(preventholddelay); //delay a little to prevent continuous cycling
  508. }
  509.  
  510. void dot(){
  511. digitalWrite(morseoutpin,HIGH);
  512. delay(unit);
  513. digitalWrite(morseoutpin,LOW);
  514. delay(unit);
  515. }
  516.  
  517. void dash(){
  518. digitalWrite(morseoutpin,HIGH);
  519. delay(3*unit);
  520. digitalWrite(morseoutpin,LOW);
  521. delay(unit);
  522. }
  523.  
  524.  
  525. void sendmorse(){
  526. dash();
  527. dash();
  528. dash();
  529. dash();
  530. dash(); //start signal, start of transmission
  531. for (int i=0; i<(positionintypedtext); i++){ //loop through the typed message
  532. Serial.println(i);
  533. Serial.println(millis());
  534. selectedletter=typedtext[i];
  535. Serial.println(selectedletter);
  536. switch (selectedletter) {
  537. case 'Q' :
  538. dash();
  539. dash();
  540. dot();
  541. dash();
  542. break;
  543. case 'Z' :
  544. dash();
  545. dash();
  546. dash();
  547. dash();
  548. break;
  549. case '1':
  550. dash();
  551. dash();
  552. dash();
  553. dash();
  554. dash();
  555. break;
  556. case 'A':
  557. dash();
  558. dash();
  559. break;
  560. case 'B':
  561. dash();
  562. dash();
  563. dash();
  564. dash();
  565. break;
  566. case 'C':
  567. dash();
  568. dash();
  569. dash();
  570. dash();
  571. break;
  572. case '2':
  573. dash();
  574. dash();
  575. dash();
  576. dash();
  577. dash();
  578. break;
  579. case 'D':
  580. dash();
  581. dash();
  582. dash();
  583. break;
  584. case 'E':
  585. dash();
  586. break;
  587. case 'F':
  588. dash();
  589. dash();
  590. dash();
  591. dash();
  592. break;
  593. case '3':
  594. dash();
  595. dash();
  596. dash();
  597. dash();
  598. dash();
  599. break;
  600. case 'G':
  601. dash();
  602. dash();
  603. dash();
  604. break;
  605. case 'H':
  606. dash();
  607. dash();
  608. dash();
  609. dash();
  610. break;
  611. case 'I':
  612. dash();
  613. dash();
  614. break;
  615. case '4':
  616. dash();
  617. dash();
  618. dash();
  619. dash();
  620. dash();
  621. break;
  622. case 'J':
  623. dash();
  624. dash();
  625. dash();
  626. dash();
  627. break;
  628. case 'K':
  629. dash();
  630. dash();
  631. dash();
  632. break;
  633. case 'L':
  634. dash();
  635. dash();
  636. dash();
  637. dash();
  638. break;
  639. case '5':
  640. dash();
  641. dash();
  642. dash();
  643. dash();
  644. dash();
  645. break;
  646. case 'M':
  647. dash();
  648. dash();
  649. break;
  650. case 'N':
  651. dash();
  652. dash();
  653. break;
  654. case 'O':
  655. dash();
  656. dash();
  657. dash();
  658. break;
  659. case '6':
  660. dash();
  661. dash();
  662. dash();
  663. dash();
  664. dash();
  665. break;
  666. case 'P':
  667. dash();
  668. dash();
  669. dash();
  670. dash();
  671. break;
  672. case 'R':
  673. dash();
  674. dash();
  675. dash();
  676. break;
  677. case 'S':
  678. dash();
  679. dash();
  680. dash();
  681. break;
  682. case '7':
  683. dash();
  684. dash();
  685. dash();
  686. dash();
  687. dash();
  688. break;
  689. case 'T':
  690. dash();
  691. break;
  692. case 'U':
  693. dash();
  694. dash();
  695. dash();
  696. break;
  697. case 'V':
  698. dash();
  699. dash();
  700. dash();
  701. dash();
  702. break;
  703. case '8':
  704. dash();
  705. dash();
  706. dash();
  707. dash();
  708. dash();
  709. break;
  710. case 'W':
  711. dash();
  712. dash();
  713. dash();
  714. break;
  715. case 'X':
  716. dash();
  717. dash();
  718. dash();
  719. dash();
  720. break;
  721. case 'Y':
  722. dash();
  723. dash();
  724. dash();
  725. dash();
  726. break;
  727. case '9':
  728. dash();
  729. dash();
  730. dash();
  731. dash();
  732. dash();
  733. break;
  734. case '.':
  735. dash();
  736. dash();
  737. dash();
  738. dash();
  739. dash();
  740. dash();
  741. break;
  742. case '?':
  743. dash();
  744. dash();
  745. dash();
  746. dash();
  747. dash();
  748. dash();
  749. break;
  750. case '!':
  751. //no morse equivalent, sends period instead
  752. dash();
  753. dash();
  754. dash();
  755. dash();
  756. dash();
  757. dash();
  758. break;
  759. case ',':
  760. dash();
  761. dash();
  762. dash();
  763. dash();
  764. dash();
  765. dash();
  766. break;
  767. case '\'':
  768. dash();
  769. dash();
  770. dash();
  771. dash();
  772. dash();
  773. dash();
  774. break;
  775. case '"':
  776. dash();
  777. dash();
  778. dash();
  779. dash();
  780. dash();
  781. dash();
  782. break;
  783. case '-':
  784. dash();
  785. dash();
  786. dash();
  787. dash();
  788. dash();
  789. dash();
  790. break;
  791. case ' ':
  792. delay(4*unit);
  793. break;
  794. case '0':
  795. dash();
  796. dash();
  797. dash();
  798. dash();
  799. dash();
  800. break;
  801. }
  802. delay(2*unit);
  803.  
  804. }
  805. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement