Advertisement
Guest User

Untitled

a guest
Feb 18th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.09 KB | None | 0 0
  1. #include <LiquidCrystal.h> //include the lcd library
  2.  
  3. #include <avr/pgmspace.h>
  4. #include <MorseEnDecoder.h>
  5.  
  6. // Pin mappings
  7. const byte morseInPin = A0;
  8. const byte morseOutPin = 13;
  9.  
  10. // Instantiate Morse objects
  11. morseDecoder morseInput(morseInPin, MORSE_KEYER, MORSE_ACTIVE_LOW);
  12. morseEncoder morseOutput(morseOutPin);
  13.  
  14. // Variables dealing with formatting the output somewhat
  15. // by inserting CR's (carriage returns)
  16. long lastTransmissionTime;
  17. long currentTime;
  18. boolean transmissionEnded = true; // Flag to mark old transmission is finished
  19.  
  20. // Minimum transmission pause time to insert carriage returns (CR)
  21. // Adjust depending on Morse speed. IE 13 wpm = 646 ms between words (no CR).
  22. const long transmissionPaused = 646; // Suitable for 13 wpm?
  23.  
  24.  
  25. const byte row1=3; //keypad row pins, shared with the lcd data lines
  26. const byte row2=4;
  27. const byte row3=5;
  28. const byte row4=6;
  29. const byte col1=7; //keypad column lines
  30. const byte col2=8;
  31. const byte col3=9;
  32. const byte col4=10;
  33. LiquidCrystal lcd(12, 11, row4, row3, row2, row1); //lcd object
  34. byte numpressed; //latest number pressed on the keypad
  35. byte timespressed; //times the number has been pressed
  36. byte cursorx=0; //cursor x position
  37. byte cursory=0; //cursor y position
  38. char letter; //stores letter that needs to be printed to the lcd
  39. const int wait=1000; //time to wait for additional presses to same number
  40. const int preventholddelay=150; //time to wait to prevent cycling through things too quickly
  41. unsigned long basetime; //base time for while loop
  42. unsigned long elapsed=0; //elapsed time in while loop
  43. byte lastnumpressed; //the initial number pressed on the keypad
  44. bool disablespacedelay=false; //disables the delay in the space function in the case that a different number is pressed while in while loop
  45. const byte maxtimespressed[16]={
  46. 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)
  47. char typedtext[140]; //stores typed text for printout to the serial console
  48. int positionintypedtext=0; //position in typedtext character array
  49. int charremaining; //remaining characters in message
  50. bool promptkeypress=false; //used for waiting for, and keypress detection, at the end-of composition prompt
  51.  
  52. void setup(){
  53. Serial.begin(9600); //start serial communication so that we can print the typed text to the serial console
  54. lcd.begin(16,4); //initialize the lcd
  55. lcd.setCursor(cursorx,cursory); //set the lcd cursor
  56. lcd.noCursor(); //turn off the cursor
  57. pinMode(row1,OUTPUT); //set the rows as outputs
  58. pinMode(row2,OUTPUT);
  59. pinMode(row3,OUTPUT);
  60. pinMode(row4,OUTPUT);
  61. pinMode(col1,INPUT); //set the columns as inputs
  62. pinMode(col2,INPUT);
  63. pinMode(col3,INPUT);
  64. pinMode(col4,INPUT);
  65. delay(wait);
  66. lcd.cursor(); //turn the cursor on again after a delay equivalent to wait
  67. rowshigh(); //sets all rows high
  68. // Setting Morse speed in wpm - words per minute
  69. // If not set, 13 wpm is default anywas
  70. morseInput.setspeed(13);
  71. morseOutput.setspeed(13);
  72.  
  73. lastTransmissionTime = (long)millis();
  74. }
  75.  
  76. void loop(){
  77. numpressed=16; //reset "numpressed" (16 doesn't refer to any button on the pad)
  78. if (findpress()){
  79. timespressed=0;
  80. switch (numpressed){
  81. case 15:
  82. for (int i=0; i<positionintypedtext; i++){
  83. currentTime = (long)millis();
  84. morseOutput.available();
  85. // Needs to call these once per loop
  86. morseInput.decode();
  87. morseOutput.encode();
  88.  
  89. // SEND MORSE (OUTPUT)
  90. // Encode and send text received from the serial port (serial monitor)
  91. //if (morseOutput.available())
  92. //{
  93. // Get character from serial and send as Morse code
  94. char sendMorse = typedtext[i];
  95. morseOutput.write(sendMorse);
  96.  
  97. // Not strictly needed, but used to get morseSignalString before it is destroyed
  98. // (E.g. for morse training purposes)
  99. morseOutput.encode();
  100.  
  101. // Also write sent character + Morse code to serial port/monitor
  102. Serial.write(' ');
  103. Serial.write(sendMorse);
  104. Serial.write(morseOutput.morseSignalString);
  105. //}
  106. }
  107. }
  108.  
  109. }
  110. else if (findpress()){ //look for presses, if one has occurred, identify it and continue
  111. timespressed=0; //reset "timespressed"
  112. switch (numpressed){ //if zero on the pad was pressed,
  113. case 0:
  114. dozero(); //print zero
  115. letter='0'; //manually seed a zero as the character for the text storage
  116. textstorage(1); //regular character storage
  117. break;
  118. case 10: //if shift on the pad was pressed,
  119. textstorage(2); //perform a space in storage
  120. dospace(); //do a space
  121. break;
  122. case 14: //if the arrows on the pad were pressed,
  123. textstorage(3); //perform a backspace in storage
  124. dobackspace(); //do a backspace
  125. break;
  126. }
  127. 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),
  128. lastnumpressed=numpressed; //record which number was pressed,
  129. basetime=millis(); //and take a base time for the while loop
  130. while (elapsed<wait){ //while below the time to wait,
  131. if(findpress()){ //look for presses, if one has occurred, identify it and continue
  132. if (numpressed==lastnumpressed){ //if it was the same as before,
  133. incrementtimespressed(); //increment "timespressed"
  134. basetime=basetime+(wait-(wait-elapsed)); //roll up the base time, to allow another wait period until next press of the same button
  135. definepress(); //use "numpressed" and "timespressed" to define "letter"
  136. lcd.print(letter); //print the letter that was defined
  137. lcd.setCursor(cursorx,cursory); //maintain cursor position
  138. rowshigh(); //return all rows high
  139. delay(preventholddelay); //delay a little to prevent continuous cycling through "timespressed" during a single press
  140. }
  141. else{ //if the number that was pressed was different than before,
  142. 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
  143. break; //break out of the while loop
  144. }
  145. }
  146. elapsed=millis()-basetime; //refresh the elapsed time for the while loop
  147. }
  148. elapsed=0; //reset the elapsed time for the while loop
  149. textstorage(1); //store character
  150. dospace(); //do a space
  151. }
  152. }
  153. if (positionintypedtext==139){ //if the end of the stored text has been reached,
  154. promptkeypress=false; //reset keypress detection
  155. cursorx=0; //set cursor to the beginning of first row
  156. cursory=0;
  157. lcd.setCursor(cursorx,cursory);
  158. lcd.print("Msg end. <>=back"); //print this out to the lcd
  159. cursorx=0; //set cursor to the beginning of second row
  160. cursory=1;
  161. lcd.setCursor(cursorx,cursory);
  162. lcd.print("enter=serial out"); //print this out to the lcd
  163. rowshigh(); //sets all rows high
  164. numpressed=16; //reset "numpressed" (16 doesn't refer to any button on the pad)
  165. while(!promptkeypress){ //while no relevant keypresses have occurred,
  166. if (findpress()){ //look for presses, if one has occurred, identify it and continue
  167. timespressed=0; //reset "timespressed"
  168. switch (numpressed){ //if the arrows on the pad were pressed,
  169. case 14:
  170. promptkeypress=true; //take note so that the while loop can be broken
  171. textstorage(3); //perform a backspace in storage
  172. for (int i=0;i<16;i++){ //print out to the first line on the lcd from the stored text
  173. cursorx=i;
  174. cursory=0;
  175. lcd.setCursor(cursorx,cursory);
  176. lcd.print(typedtext[108+i]);
  177. }
  178. for (int j=0;j<16;j++){ //print out to the second line on the lcd from the stored text
  179. cursorx=j;
  180. cursory=1;
  181. lcd.setCursor(cursorx,cursory);
  182. lcd.print(typedtext[123+j]);
  183. }
  184. cursorx=15; //set cursor to the beginning of second row
  185. cursory=1;
  186. lcd.setCursor(cursorx,cursory);
  187. rowshigh(); //sets all rows high
  188. break;
  189. case 15: //if enter on the pad was pressed,
  190. for (int i=0; i<positionintypedtext; i++){
  191. currentTime = (long)millis();
  192.  
  193. // Needs to call these once per loop
  194. morseInput.decode();
  195. morseOutput.encode();
  196.  
  197. // SEND MORSE (OUTPUT)
  198. // Encode and send text received from the serial port (serial monitor)
  199. if (morseOutput.available())
  200. {
  201. // Get character from serial and send as Morse code
  202. char sendMorse = typedtext[i];
  203. morseOutput.write(sendMorse);
  204.  
  205. // Not strictly needed, but used to get morseSignalString before it is destroyed
  206. // (E.g. for morse training purposes)
  207. morseOutput.encode();
  208.  
  209. // Also write sent character + Morse code to serial port/monitor
  210. Serial.write(' ');
  211. Serial.write(sendMorse);
  212. Serial.write(morseOutput.morseSignalString);
  213. }
  214. }
  215. delay(preventholddelay); //delay a little to prevent continuous cycling
  216. break;
  217.  
  218. }
  219. }
  220. }
  221. }
  222. }
  223.  
  224.  
  225. void rowshigh(){ //sets all rows high
  226. digitalWrite(row1,HIGH); //write all the rows high
  227. digitalWrite(row2,HIGH);
  228. digitalWrite(row3,HIGH);
  229. digitalWrite(row4,HIGH);
  230. }
  231.  
  232. bool findpress(){ //finds a press to define "numpressed", if any press occurs, returns true
  233. bool pressfound=false; //variable for any press detection, is returned by this function
  234.  
  235. digitalWrite(row1,LOW); //write all rows low
  236. digitalWrite(row2,LOW);
  237. digitalWrite(row3,LOW);
  238. digitalWrite(row4,LOW);
  239.  
  240. digitalWrite(row1,HIGH); //write first row high
  241. if (digitalRead(col1)==HIGH){ //if the first column is now high, "1" has been pressed
  242. numpressed = 1;
  243. pressfound=true;
  244. }
  245. if (digitalRead(col2)==HIGH){ //if the second column is now high, "2" has been pressed
  246. numpressed = 2;
  247. pressfound=true;
  248. }
  249. if (digitalRead(col3)==HIGH){ //if the third column is now high, "3" has been pressed
  250. numpressed = 3;
  251. pressfound=true;
  252. }
  253. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "reset" has been pressed
  254. numpressed = 12;
  255. pressfound=true;
  256. }
  257. digitalWrite(row1,LOW); //return first row low
  258.  
  259. digitalWrite(row2,HIGH); //write second row high
  260. if (digitalRead(col1)==HIGH){ //if the first column is now high, "4" has been pressed
  261. numpressed = 4;
  262. pressfound=true;
  263. }
  264. if (digitalRead(col2)==HIGH){ //if the second column is now high, "5" has been pressed
  265. numpressed = 5;
  266. pressfound=true;
  267. }
  268. if (digitalRead(col3)==HIGH){ //if the third column is now high, "6" has been pressed
  269. numpressed = 6;
  270. pressfound=true;
  271. }
  272. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "dial" has been pressed
  273. numpressed = 13;
  274. pressfound=true;
  275. }
  276. digitalWrite(row2,LOW); //return second row low
  277.  
  278. digitalWrite(row3,HIGH); //write third row high
  279. if (digitalRead(col1)==HIGH){ //if the first column is now high, "7" has been pressed
  280. numpressed = 7;
  281. pressfound=true;
  282. }
  283. if (digitalRead(col2)==HIGH){ //if the second column is now high, "8" has been pressed
  284. numpressed = 8;
  285. pressfound=true;
  286. }
  287. if (digitalRead(col3)==HIGH){ //if the third column is now high, "9" has been pressed
  288. numpressed = 9;
  289. pressfound=true;
  290. }
  291. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, the arrows have been pressed
  292. numpressed = 14;
  293. pressfound=true;
  294. }
  295. digitalWrite(row3,LOW); //return third row low
  296.  
  297. digitalWrite(row4,HIGH); //write fourth row high
  298. if (digitalRead(col1)==HIGH){ //if the first column is now high, "shift" has been pressed
  299. numpressed = 10;
  300. pressfound=true;
  301. }
  302. if (digitalRead(col2)==HIGH){ //if the second column is now high, "0" has been pressed
  303. numpressed = 0;
  304. pressfound=true;
  305. }
  306. if (digitalRead(col3)==HIGH){ //if the third column is now high, "." has been pressed
  307. numpressed = 11;
  308. pressfound=true;
  309. }
  310. if (digitalRead(col4)==HIGH){ //if the fourth column is now high, "enter" has been pressed
  311. numpressed = 15;
  312. pressfound=true;
  313. }
  314. digitalWrite(row4,LOW); //return fourth row low
  315.  
  316. rowshigh(); //write all rows high
  317.  
  318. return pressfound; //function returns true if any press found, otherwise returns false
  319. }
  320.  
  321. void definepress(){ //uses "lastnumpressed" and "timespressed" to define "letter"
  322. switch (lastnumpressed){
  323. case 1 :
  324. switch (timespressed){
  325. case 1:
  326. letter='1';
  327. break;
  328. case 2 :
  329. letter='.';
  330. break;
  331. case 3 :
  332. letter=',';
  333. break;
  334. }
  335. break;
  336. case 2 :
  337. switch (timespressed){
  338. case 1:
  339. letter='A';
  340. break;
  341. case 2 :
  342. letter='B';
  343. break;
  344. case 3 :
  345. letter='C';
  346. break;
  347. case 4 :
  348. letter='2';
  349. break;
  350. }
  351. break;
  352. case 3 :
  353. switch (timespressed){
  354. case 1:
  355. letter='D';
  356. break;
  357. case 2 :
  358. letter='E';
  359. break;
  360. case 3 :
  361. letter='F';
  362. break;
  363. case 4 :
  364. letter='3';
  365. break;
  366. }
  367. break;
  368. case 4 :
  369. switch (timespressed){
  370. case 1:
  371. letter='G';
  372. break;
  373. case 2 :
  374. letter='H';
  375. break;
  376. case 3 :
  377. letter='I';
  378. break;
  379. case 4 :
  380. letter='4';
  381. break;
  382. }
  383. break;
  384. case 5 :
  385. switch (timespressed){
  386. case 1:
  387. letter='J';
  388. break;
  389. case 2 :
  390. letter='K';
  391. break;
  392. case 3 :
  393. letter='L';
  394. break;
  395. case 4 :
  396. letter='5';
  397. break;
  398. }
  399. break;
  400. case 6 :
  401. switch (timespressed){
  402. case 1:
  403. letter='M';
  404. break;
  405. case 2 :
  406. letter='N';
  407. break;
  408. case 3 :
  409. letter='O';
  410. break;
  411. case 4 :
  412. letter='6';
  413. break;
  414. }
  415. break;
  416. case 7 :
  417. switch (timespressed){
  418. case 1 :
  419. letter='P';
  420. break;
  421. case 2 :
  422. letter='Q';
  423. break;
  424. case 3 :
  425. letter='R';
  426. break;
  427. case 4 :
  428. letter='S';
  429. break;
  430. case 5 :
  431. letter='7';
  432. break;
  433. }
  434. break;
  435. case 8 :
  436. switch (timespressed){
  437. case 1:
  438. letter='T';
  439. break;
  440. case 2 :
  441. letter='U';
  442. break;
  443. case 3 :
  444. letter='V';
  445. break;
  446. case 4 :
  447. letter='8';
  448. break;
  449. }
  450. break;
  451.  
  452. case 9 :
  453. switch (timespressed){
  454. case 1 :
  455. letter='W';
  456. break;
  457. case 2 :
  458. letter='X';
  459. break;
  460. case 3 :
  461. letter='Y';
  462. break;
  463. case 4 :
  464. letter='Z';
  465. break;
  466. case 5 :
  467. letter='9';
  468. break;
  469. }
  470. break;
  471. case 11 :
  472. switch (timespressed){
  473. case 1:
  474. letter='?';
  475. break;
  476. case 2 :
  477. letter='-';
  478. break;
  479. case 3 :
  480. letter='/';
  481. break;
  482. case 4 :
  483. letter=':';
  484. break;
  485. case 5 :
  486. letter='%';
  487. break;
  488. }
  489. break;
  490. }
  491. }
  492.  
  493. void incrementtimespressed(){ //increment "timespressed" until at max value stored in maxtimespressed for that lastnumpressed, then roll over to 1
  494. if (timespressed==maxtimespressed[lastnumpressed]){ //if at the maximum,
  495. timespressed=1; //roll over timespressed to one
  496. }
  497. else{ //otherwise,
  498. timespressed++; //increment timespressed
  499. }
  500. }
  501.  
  502. void dozero(){ //prints zero
  503. lcd.print('0'); //print 0
  504. lcd.setCursor(cursorx,cursory); //maintain cursor position
  505. dospace(); //space
  506. }
  507.  
  508. void dospace(){ //moves cursor forward once, wraps to next line if necessary, clears and returns to top of display if at bottom
  509. if (cursory==1){ //if on the bottom row,
  510. if (cursorx==15){ //if at the end of the row,
  511. cursorx=0; //define new cursor position as the upper-left corner
  512. cursory=0;
  513. lcd.clear(); //clear the lcd
  514. }
  515. else{ //otherwise,
  516. cursorx++; //increment the cursor to the right
  517. }
  518. }
  519. else{ //if on the top row,
  520. if (cursorx==15){ //if at the end of the row,
  521. cursorx=0; //define new cursor position as the bottom-left corner
  522. cursory=1;
  523. }
  524. else{ //otherwise,
  525. cursorx++; //increment the cursor to the right
  526. }
  527. }
  528. lcd.setCursor(cursorx,cursory); //set cursor to defined location
  529. rowshigh(); //sets all rows high
  530. if (disablespacedelay){ //if the delay has been disabled,
  531. disablespacedelay=false; //reset its being disabled
  532. }
  533. else{ //otherwise,
  534. delay(preventholddelay); //delay a bit
  535. }
  536. }
  537.  
  538. void doclear(){ //clears and returns to top-left of display
  539. cursorx=0;
  540. cursory=0;
  541. lcd.clear();
  542. lcd.setCursor(cursorx,cursory);
  543. rowshigh(); //sets all rows high
  544. delay(preventholddelay);
  545. }
  546.  
  547. void dobackspace(){ //does a backspace, essentially the opposite of dospace
  548. if (cursory==1){
  549. if (cursorx==0){
  550. cursorx=15;
  551. cursory=0;
  552. }
  553. else{
  554. cursorx--;
  555. }
  556. }
  557. else{
  558. if (cursorx==0){
  559. cursorx=0;
  560. cursory=0;
  561. }
  562. else{
  563. cursorx--;
  564. }
  565. }
  566. lcd.setCursor(cursorx,cursory);
  567. lcd.print(" ");
  568. lcd.setCursor(cursorx,cursory);
  569. rowshigh(); //sets all rows high
  570. delay(preventholddelay);
  571. }
  572.  
  573. void textstorage(byte mode){ //contains functions for text storage
  574. if (mode==1){ //regular character storage
  575. typedtext[positionintypedtext]=letter; //store letter that was printed to LCD in typedtext
  576. positionintypedtext++; //increment position in typedtext
  577. }
  578. if (mode==2){ //do a space in stored text
  579. typedtext[positionintypedtext]=' '; //set current position in typedtext to a space
  580. positionintypedtext++; //increment position in typedtext
  581. }
  582. if (mode==3){ //does a backspace in the stored text
  583. positionintypedtext--; //decrement position in typedtext
  584. typedtext[positionintypedtext]=' '; //set current position in typedtext to a space
  585. }
  586. }
  587.  
  588. void outputserial(){ //output message and display remaining characters to serial console
  589.  
  590. morseOutput.available();
  591. for (int i=0; i<positionintypedtext; i++){
  592. currentTime = (long)millis();
  593. Serial.println(i);
  594. // Needs to call these once per loop
  595. morseInput.decode();
  596. morseOutput.encode();
  597.  
  598. // SEND MORSE (OUTPUT)
  599. // Encode and send text received from the serial port (serial monitor)
  600. if (morseOutput.available()){
  601. // Get character from serial and send as Morse code
  602. char sendMorse = typedtext[i];
  603. morseOutput.write(sendMorse);
  604.  
  605. // Not strictly needed, but used to get morseSignalString before it is destroyed
  606. // (E.g. for morse training purposes)
  607. morseOutput.encode();
  608.  
  609. // Also write sent character + Morse code to serial port/monitor
  610. Serial.write(' ');
  611. Serial.write(sendMorse);
  612. Serial.write(morseOutput.morseSignalString);
  613. }
  614. }
  615. delay(preventholddelay); //delay a little to prevent continuous cycling
  616. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement