Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. /* Iambic keyer with lcd for cw practice
  2. Courtesy SV1OBT and OZ1JHM
  3. */
  4. #include <LiquidCrystal.h>
  5. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // LCD display pins
  6. #define TONE 400
  7. const int colums = 16;
  8. const int rows = 2;
  9.  
  10. //simple keypad for speed
  11. int lcd_key = 0;
  12. int adc_key_in = 0;
  13. #define btnRIGHT 0
  14. #define btnUP 1
  15. #define btnDOWN 2
  16. #define btnLEFT 3
  17. #define btnSELECT 4
  18. #define btnNONE 5
  19.  
  20. int lcdindex = 0;
  21. int line1[colums];
  22. int line2[colums];
  23. bool halt=false;
  24. long startttimelow;
  25. byte U_umlaut[8] = {B01010,B00000,B10001,B10001,B10001,B10001,B01110,B00000}; // 'Ü'
  26. byte O_umlaut[8] = {B01010,B00000,B01110,B10001,B10001,B10001,B01110,B00000}; // 'Ö'
  27. byte A_umlaut[8] = {B01010,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 'Ä'
  28. byte AE_capital[8] = {B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // 'Æ'
  29. byte OE_capital[8] = {B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // 'Ø'
  30. byte fullblock[8] = {B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
  31. byte AA_capital[8] = {B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 'Å'
  32. byte emtyblock[8] = {B00000,B00000,B00000,B00000,B00000,B00000,B00000,B00000};
  33. //#define DIT_PIN 8
  34. //#define DAH_PIN 10
  35. //#define EXC_PIN 9
  36. #define DIT_PIN A1
  37. #define DAH_PIN A2
  38. #define EXC_PIN A3
  39. #define LED 13
  40. int BAUD_DURATION = 48 ; //mSec
  41. int TOUCH_THRESHOLD= 10; //how long to wait in uSec, before sampling the touch pin.
  42. int INTERBAUD_DURATION =BAUD_DURATION*1;
  43. int INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
  44. int DIT_DURATION =BAUD_DURATION;
  45. int DAH_DURATION =BAUD_DURATION*3;
  46. int INTERWORD_DURATION =BAUD_DURATION*7;
  47.  
  48. enum{
  49. IDLE,
  50. DIT,
  51. DAH,
  52. PAUSE,
  53. };
  54. int dit,dah;
  55. int state;
  56. char code[20];
  57. void readDit()
  58. {
  59. digitalWrite(EXC_PIN,HIGH);
  60. delayMicroseconds(TOUCH_THRESHOLD);
  61. if(digitalRead(DIT_PIN)) dit=0; else dit=1;
  62. digitalWrite(EXC_PIN,LOW);
  63. }
  64. void readDah()
  65. {
  66. digitalWrite(EXC_PIN,HIGH);
  67. delayMicroseconds(TOUCH_THRESHOLD);
  68. if(digitalRead(DAH_PIN)) dah=0; else dah=1;
  69. digitalWrite(EXC_PIN,LOW);
  70. }
  71. void setup()
  72. {
  73. lcd.createChar(0, U_umlaut); // German
  74. lcd.createChar(1, O_umlaut); // German, Swedish
  75. lcd.createChar(2, A_umlaut); // German, Swedish
  76. lcd.createChar(3, AE_capital); // Danish, Norwegian
  77. lcd.createChar(4, OE_capital); // Danish, Norwegian
  78. lcd.createChar(5, fullblock);
  79. lcd.createChar(6, AA_capital); // Danish, Norwegian, Swedish
  80. lcd.createChar(7, emtyblock);
  81. lcd.clear();
  82. lcd.begin(colums, rows);
  83. for (int index = 0; index < colums; index++){
  84. line1[index] = 32;
  85. line2[index] = 32;
  86. }
  87. Serial.begin(115200);
  88. Serial.println("Serial is active");
  89. pinMode(EXC_PIN,OUTPUT);
  90. digitalWrite(EXC_PIN,LOW);
  91. pinMode(LED,OUTPUT);
  92. digitalWrite(LED,LOW);
  93. state = 0;
  94. }
  95. void contact(unsigned char state)
  96. {
  97. if(state) {
  98. digitalWrite(LED,HIGH);
  99. //analogWrite(11,127); //pin 11 drives an 8 Ohm speaker
  100. tone(11,TONE);
  101. }
  102. else{
  103. digitalWrite(LED,LOW);
  104. //analogWrite(11,0);
  105. noTone(11);
  106. }
  107. }
  108. void loop()
  109. {
  110. switch(state){
  111. case IDLE:
  112. if(halt & (millis() - startttimelow) >INTERWORD_DURATION){
  113. printascii(32);
  114. halt=false;
  115. startttimelow =millis();
  116. }
  117. readDit();
  118. if(dit) {
  119. state = DIT;
  120. }
  121. else{
  122. delayMicroseconds(30);
  123. readDah();
  124. if(dah) {
  125. state = DAH;
  126. }
  127. }
  128. lcd_key = read_LCD_buttons();
  129.  
  130. //start scan
  131. switch (lcd_key) // depending on which button was pushed, we perform an action
  132. {
  133. case btnRIGHT:
  134. {
  135. lcd.print("RIGHT FAST ");
  136. BAUD_DURATION = 40 ;
  137. INTERBAUD_DURATION =BAUD_DURATION*1;
  138. INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
  139. DIT_DURATION =BAUD_DURATION;
  140. DAH_DURATION =BAUD_DURATION*3;
  141. INTERWORD_DURATION =BAUD_DURATION*7;
  142. break;
  143. }
  144. case btnLEFT:
  145. {
  146. lcd.print("LEFT SLOW ");
  147. BAUD_DURATION = 160 ;
  148. INTERBAUD_DURATION =BAUD_DURATION*1;
  149. INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
  150. DIT_DURATION =BAUD_DURATION;
  151. DAH_DURATION =BAUD_DURATION*3;
  152. INTERWORD_DURATION =BAUD_DURATION*7;
  153.  
  154. break;
  155. }
  156. case btnUP:
  157. {
  158. lcd.clear();
  159. break;
  160. }
  161. case btnDOWN:
  162. {
  163. lcd.print("DOWN ");
  164. BAUD_DURATION = 80 ;
  165. INTERBAUD_DURATION =BAUD_DURATION*1;
  166. INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
  167. DIT_DURATION =BAUD_DURATION;
  168. DAH_DURATION =BAUD_DURATION*3;
  169. INTERWORD_DURATION =BAUD_DURATION*7;
  170. break;
  171. }
  172. case btnSELECT:
  173. {
  174. lcd.print("SELECT");
  175. break;
  176. }
  177. case btnNONE:
  178. {
  179. //lcd.print("NONE ");
  180. break;
  181. }
  182. }
  183. //end block
  184.  
  185.  
  186. break;
  187. case DIT:
  188. startttimelow =millis();
  189. contact(1);
  190. delay(DIT_DURATION);
  191. contact(0);
  192. delay(INTERBAUD_DURATION);
  193. Serial.print(".");
  194. strcat(code,".");
  195. //now, if dah is pressed go there, else check for dit
  196. readDah();
  197. if(dah){
  198. state = DAH;
  199. }
  200. else{
  201. //read dit now
  202. readDit();
  203. if(dit) {
  204. state = DIT;
  205. }
  206. else {
  207. delay(INTERLETTER_DURATION);
  208. Serial.print("|");
  209. docode();
  210. code[0] = '\0';
  211. state = IDLE;
  212. }
  213. }
  214. break;
  215. case DAH:
  216. startttimelow =millis();
  217. contact(1);
  218. delay(DAH_DURATION);
  219. contact(0);
  220. delay(INTERBAUD_DURATION);
  221. Serial.print("-");
  222. strcat(code,"-");
  223. readDit();
  224. if(dit){
  225. state = DIT;
  226. }
  227. else{
  228. //read dit now
  229. readDah();
  230. if(dah) {
  231. state = DAH;
  232. }
  233. else {
  234. delay(INTERLETTER_DURATION);
  235. Serial.print("|");
  236. docode();
  237. code[0] = '\0';
  238. state = IDLE;
  239. }
  240. }
  241. break;
  242. }//switch
  243. delay(1);
  244. }
  245. void docode(){
  246. if (strcmp(code,".-") == 0) printascii(65);
  247. if (strcmp(code,"-...") == 0) printascii(66);
  248. if (strcmp(code,"-.-.") == 0) printascii(67);
  249. if (strcmp(code,"-..") == 0) printascii(68);
  250. if (strcmp(code,".") == 0) printascii(69);
  251. if (strcmp(code,"..-.") == 0) printascii(70);
  252. if (strcmp(code,"--.") == 0) printascii(71);
  253. if (strcmp(code,"....") == 0) printascii(72);
  254. if (strcmp(code,"..") == 0) printascii(73);
  255. if (strcmp(code,".---") == 0) printascii(74);
  256. if (strcmp(code,"-.-") == 0) printascii(75);
  257. if (strcmp(code,".-..") == 0) printascii(76);
  258. if (strcmp(code,"--") == 0) printascii(77);
  259. if (strcmp(code,"-.") == 0) printascii(78);
  260. if (strcmp(code,"---") == 0) printascii(79);
  261. if (strcmp(code,".--.") == 0) printascii(80);
  262. if (strcmp(code,"--.-") == 0) printascii(81);
  263. if (strcmp(code,".-.") == 0) printascii(82);
  264. if (strcmp(code,"...") == 0) printascii(83);
  265. if (strcmp(code,"-") == 0) printascii(84);
  266. if (strcmp(code,"..-") == 0) printascii(85);
  267. if (strcmp(code,"...-") == 0) printascii(86);
  268. if (strcmp(code,".--") == 0) printascii(87);
  269. if (strcmp(code,"-..-") == 0) printascii(88);
  270. if (strcmp(code,"-.--") == 0) printascii(89);
  271. if (strcmp(code,"--..") == 0) printascii(90);
  272. if (strcmp(code,".----") == 0) printascii(49);
  273. if (strcmp(code,"..---") == 0) printascii(50);
  274. if (strcmp(code,"...--") == 0) printascii(51);
  275. if (strcmp(code,"....-") == 0) printascii(52);
  276. if (strcmp(code,".....") == 0) printascii(53);
  277. if (strcmp(code,"-....") == 0) printascii(54);
  278. if (strcmp(code,"--...") == 0) printascii(55);
  279. if (strcmp(code,"---..") == 0) printascii(56);
  280. if (strcmp(code,"----.") == 0) printascii(57);
  281. if (strcmp(code,"-----") == 0) printascii(48);
  282. if (strcmp(code,"..--..") == 0) printascii(63);
  283. if (strcmp(code,".-.-.-") == 0) printascii(46);
  284. if (strcmp(code,"--..--") == 0) printascii(44);
  285. if (strcmp(code,"-.-.--") == 0) printascii(33);
  286. if (strcmp(code,".--.-.") == 0) printascii(64);
  287. if (strcmp(code,"---...") == 0) printascii(58);
  288. if (strcmp(code,"-....-") == 0) printascii(45);
  289. if (strcmp(code,"-..-.") == 0) printascii(47);
  290. if (strcmp(code,"-.--.") == 0) printascii(40);
  291. if (strcmp(code,"-.--.-") == 0) printascii(41);
  292. if (strcmp(code,".-...") == 0) printascii(95);
  293. if (strcmp(code,"...-..-") == 0) printascii(36);
  294. if (strcmp(code,"...-.-") == 0) printascii(62);
  295. if (strcmp(code,".-.-.") == 0) printascii(60);
  296. if (strcmp(code,"...-.") == 0) printascii(126);
  297. if (strcmp(code,".-.-") == 0) printascii(3);
  298. if (strcmp(code,"---.") == 0) printascii(4);
  299. if (strcmp(code,".--.-") == 0) printascii(6);
  300. halt=true;
  301. }
  302. void sprintascii(int asciinumber){
  303. Serial.println(char(asciinumber));
  304. }
  305. void printascii(int asciinumber){
  306. sprintascii(asciinumber);
  307. int fail = 0;
  308. if (rows == 4 and colums == 16)fail = -4;
  309. if (lcdindex > colums-1){
  310. lcdindex = 0;
  311. if (rows==4){
  312. for (int i = 0; i <= colums-1 ; i++){
  313. lcd.setCursor(i,rows-3);
  314. lcd.write(line2[i]);
  315. line2[i]=line1[i];
  316. }
  317. }
  318. for (int i = 0; i <= colums-1 ; i++){
  319. lcd.setCursor(i+fail,rows-2);
  320. lcd.write(line1[i]);
  321. lcd.setCursor(i+fail,rows-1);
  322. lcd.write(32);
  323. }
  324. }
  325. line1[lcdindex]=asciinumber;
  326. lcd.setCursor(lcdindex+fail,rows-1);
  327. lcd.write(asciinumber);
  328. lcdindex += 1;
  329. }
  330.  
  331. int read_LCD_buttons()
  332. {
  333. adc_key_in = analogRead(0); // read the value from the sensor
  334. // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  335. // we add approx 50 to those values and check to see if we are close
  336. if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  337. // For V1.1 us this threshold
  338. if (adc_key_in < 50) return btnRIGHT;
  339. if (adc_key_in < 250) return btnUP;
  340. if (adc_key_in < 450) return btnDOWN;
  341. if (adc_key_in < 650) return btnLEFT;
  342. if (adc_key_in < 850) return btnSELECT;
  343.  
  344. // For V1.0 comment the other threshold and use the one below:
  345. /*
  346. if (adc_key_in < 50) return btnRIGHT;
  347. if (adc_key_in < 195) return btnUP;
  348. if (adc_key_in < 380) return btnDOWN;
  349. if (adc_key_in < 555) return btnLEFT;
  350. if (adc_key_in < 790) return btnSELECT;
  351. */
  352.  
  353.  
  354. return btnNONE; // when all others fail, return this...
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement