Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Iambic keyer with lcd for cw practice
- Courtesy SV1OBT and OZ1JHM
- */
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // LCD display pins
- #define TONE 400
- const int colums = 16;
- const int rows = 2;
- //simple keypad for speed
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- int lcdindex = 0;
- int line1[colums];
- int line2[colums];
- bool halt=false;
- long startttimelow;
- byte U_umlaut[8] = {B01010,B00000,B10001,B10001,B10001,B10001,B01110,B00000}; // 'Ü'
- byte O_umlaut[8] = {B01010,B00000,B01110,B10001,B10001,B10001,B01110,B00000}; // 'Ö'
- byte A_umlaut[8] = {B01010,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 'Ä'
- byte AE_capital[8] = {B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // 'Æ'
- byte OE_capital[8] = {B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // 'Ø'
- byte fullblock[8] = {B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
- byte AA_capital[8] = {B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 'Å'
- byte emtyblock[8] = {B00000,B00000,B00000,B00000,B00000,B00000,B00000,B00000};
- //#define DIT_PIN 8
- //#define DAH_PIN 10
- //#define EXC_PIN 9
- #define DIT_PIN A1
- #define DAH_PIN A2
- #define EXC_PIN A3
- #define LED 13
- int BAUD_DURATION = 48 ; //mSec
- int TOUCH_THRESHOLD= 10; //how long to wait in uSec, before sampling the touch pin.
- int INTERBAUD_DURATION =BAUD_DURATION*1;
- int INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
- int DIT_DURATION =BAUD_DURATION;
- int DAH_DURATION =BAUD_DURATION*3;
- int INTERWORD_DURATION =BAUD_DURATION*7;
- enum{
- IDLE,
- DIT,
- DAH,
- PAUSE,
- };
- int dit,dah;
- int state;
- char code[20];
- void readDit()
- {
- digitalWrite(EXC_PIN,HIGH);
- delayMicroseconds(TOUCH_THRESHOLD);
- if(digitalRead(DIT_PIN)) dit=0; else dit=1;
- digitalWrite(EXC_PIN,LOW);
- }
- void readDah()
- {
- digitalWrite(EXC_PIN,HIGH);
- delayMicroseconds(TOUCH_THRESHOLD);
- if(digitalRead(DAH_PIN)) dah=0; else dah=1;
- digitalWrite(EXC_PIN,LOW);
- }
- void setup()
- {
- lcd.createChar(0, U_umlaut); // German
- lcd.createChar(1, O_umlaut); // German, Swedish
- lcd.createChar(2, A_umlaut); // German, Swedish
- lcd.createChar(3, AE_capital); // Danish, Norwegian
- lcd.createChar(4, OE_capital); // Danish, Norwegian
- lcd.createChar(5, fullblock);
- lcd.createChar(6, AA_capital); // Danish, Norwegian, Swedish
- lcd.createChar(7, emtyblock);
- lcd.clear();
- lcd.begin(colums, rows);
- for (int index = 0; index < colums; index++){
- line1[index] = 32;
- line2[index] = 32;
- }
- Serial.begin(115200);
- Serial.println("Serial is active");
- pinMode(EXC_PIN,OUTPUT);
- digitalWrite(EXC_PIN,LOW);
- pinMode(LED,OUTPUT);
- digitalWrite(LED,LOW);
- state = 0;
- }
- void contact(unsigned char state)
- {
- if(state) {
- digitalWrite(LED,HIGH);
- //analogWrite(11,127); //pin 11 drives an 8 Ohm speaker
- tone(11,TONE);
- }
- else{
- digitalWrite(LED,LOW);
- //analogWrite(11,0);
- noTone(11);
- }
- }
- void loop()
- {
- switch(state){
- case IDLE:
- if(halt & (millis() - startttimelow) >INTERWORD_DURATION){
- printascii(32);
- halt=false;
- startttimelow =millis();
- }
- readDit();
- if(dit) {
- state = DIT;
- }
- else{
- delayMicroseconds(30);
- readDah();
- if(dah) {
- state = DAH;
- }
- }
- lcd_key = read_LCD_buttons();
- //start scan
- switch (lcd_key) // depending on which button was pushed, we perform an action
- {
- case btnRIGHT:
- {
- lcd.print("RIGHT FAST ");
- BAUD_DURATION = 40 ;
- INTERBAUD_DURATION =BAUD_DURATION*1;
- INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
- DIT_DURATION =BAUD_DURATION;
- DAH_DURATION =BAUD_DURATION*3;
- INTERWORD_DURATION =BAUD_DURATION*7;
- break;
- }
- case btnLEFT:
- {
- lcd.print("LEFT SLOW ");
- BAUD_DURATION = 160 ;
- INTERBAUD_DURATION =BAUD_DURATION*1;
- INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
- DIT_DURATION =BAUD_DURATION;
- DAH_DURATION =BAUD_DURATION*3;
- INTERWORD_DURATION =BAUD_DURATION*7;
- break;
- }
- case btnUP:
- {
- lcd.clear();
- break;
- }
- case btnDOWN:
- {
- lcd.print("DOWN ");
- BAUD_DURATION = 80 ;
- INTERBAUD_DURATION =BAUD_DURATION*1;
- INTERLETTER_DURATION =BAUD_DURATION*2; //extra time after a baud
- DIT_DURATION =BAUD_DURATION;
- DAH_DURATION =BAUD_DURATION*3;
- INTERWORD_DURATION =BAUD_DURATION*7;
- break;
- }
- case btnSELECT:
- {
- lcd.print("SELECT");
- break;
- }
- case btnNONE:
- {
- //lcd.print("NONE ");
- break;
- }
- }
- //end block
- break;
- case DIT:
- startttimelow =millis();
- contact(1);
- delay(DIT_DURATION);
- contact(0);
- delay(INTERBAUD_DURATION);
- Serial.print(".");
- strcat(code,".");
- //now, if dah is pressed go there, else check for dit
- readDah();
- if(dah){
- state = DAH;
- }
- else{
- //read dit now
- readDit();
- if(dit) {
- state = DIT;
- }
- else {
- delay(INTERLETTER_DURATION);
- Serial.print("|");
- docode();
- code[0] = '\0';
- state = IDLE;
- }
- }
- break;
- case DAH:
- startttimelow =millis();
- contact(1);
- delay(DAH_DURATION);
- contact(0);
- delay(INTERBAUD_DURATION);
- Serial.print("-");
- strcat(code,"-");
- readDit();
- if(dit){
- state = DIT;
- }
- else{
- //read dit now
- readDah();
- if(dah) {
- state = DAH;
- }
- else {
- delay(INTERLETTER_DURATION);
- Serial.print("|");
- docode();
- code[0] = '\0';
- state = IDLE;
- }
- }
- break;
- }//switch
- delay(1);
- }
- void docode(){
- if (strcmp(code,".-") == 0) printascii(65);
- if (strcmp(code,"-...") == 0) printascii(66);
- if (strcmp(code,"-.-.") == 0) printascii(67);
- if (strcmp(code,"-..") == 0) printascii(68);
- if (strcmp(code,".") == 0) printascii(69);
- if (strcmp(code,"..-.") == 0) printascii(70);
- if (strcmp(code,"--.") == 0) printascii(71);
- if (strcmp(code,"....") == 0) printascii(72);
- if (strcmp(code,"..") == 0) printascii(73);
- if (strcmp(code,".---") == 0) printascii(74);
- if (strcmp(code,"-.-") == 0) printascii(75);
- if (strcmp(code,".-..") == 0) printascii(76);
- if (strcmp(code,"--") == 0) printascii(77);
- if (strcmp(code,"-.") == 0) printascii(78);
- if (strcmp(code,"---") == 0) printascii(79);
- if (strcmp(code,".--.") == 0) printascii(80);
- if (strcmp(code,"--.-") == 0) printascii(81);
- if (strcmp(code,".-.") == 0) printascii(82);
- if (strcmp(code,"...") == 0) printascii(83);
- if (strcmp(code,"-") == 0) printascii(84);
- if (strcmp(code,"..-") == 0) printascii(85);
- if (strcmp(code,"...-") == 0) printascii(86);
- if (strcmp(code,".--") == 0) printascii(87);
- if (strcmp(code,"-..-") == 0) printascii(88);
- if (strcmp(code,"-.--") == 0) printascii(89);
- if (strcmp(code,"--..") == 0) printascii(90);
- if (strcmp(code,".----") == 0) printascii(49);
- if (strcmp(code,"..---") == 0) printascii(50);
- if (strcmp(code,"...--") == 0) printascii(51);
- if (strcmp(code,"....-") == 0) printascii(52);
- if (strcmp(code,".....") == 0) printascii(53);
- if (strcmp(code,"-....") == 0) printascii(54);
- if (strcmp(code,"--...") == 0) printascii(55);
- if (strcmp(code,"---..") == 0) printascii(56);
- if (strcmp(code,"----.") == 0) printascii(57);
- if (strcmp(code,"-----") == 0) printascii(48);
- if (strcmp(code,"..--..") == 0) printascii(63);
- if (strcmp(code,".-.-.-") == 0) printascii(46);
- if (strcmp(code,"--..--") == 0) printascii(44);
- if (strcmp(code,"-.-.--") == 0) printascii(33);
- if (strcmp(code,".--.-.") == 0) printascii(64);
- if (strcmp(code,"---...") == 0) printascii(58);
- if (strcmp(code,"-....-") == 0) printascii(45);
- if (strcmp(code,"-..-.") == 0) printascii(47);
- if (strcmp(code,"-.--.") == 0) printascii(40);
- if (strcmp(code,"-.--.-") == 0) printascii(41);
- if (strcmp(code,".-...") == 0) printascii(95);
- if (strcmp(code,"...-..-") == 0) printascii(36);
- if (strcmp(code,"...-.-") == 0) printascii(62);
- if (strcmp(code,".-.-.") == 0) printascii(60);
- if (strcmp(code,"...-.") == 0) printascii(126);
- if (strcmp(code,".-.-") == 0) printascii(3);
- if (strcmp(code,"---.") == 0) printascii(4);
- if (strcmp(code,".--.-") == 0) printascii(6);
- halt=true;
- }
- void sprintascii(int asciinumber){
- Serial.println(char(asciinumber));
- }
- void printascii(int asciinumber){
- sprintascii(asciinumber);
- int fail = 0;
- if (rows == 4 and colums == 16)fail = -4;
- if (lcdindex > colums-1){
- lcdindex = 0;
- if (rows==4){
- for (int i = 0; i <= colums-1 ; i++){
- lcd.setCursor(i,rows-3);
- lcd.write(line2[i]);
- line2[i]=line1[i];
- }
- }
- for (int i = 0; i <= colums-1 ; i++){
- lcd.setCursor(i+fail,rows-2);
- lcd.write(line1[i]);
- lcd.setCursor(i+fail,rows-1);
- lcd.write(32);
- }
- }
- line1[lcdindex]=asciinumber;
- lcd.setCursor(lcdindex+fail,rows-1);
- lcd.write(asciinumber);
- lcdindex += 1;
- }
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- // For V1.1 us this threshold
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 250) return btnUP;
- if (adc_key_in < 450) return btnDOWN;
- if (adc_key_in < 650) return btnLEFT;
- if (adc_key_in < 850) return btnSELECT;
- // For V1.0 comment the other threshold and use the one below:
- /*
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- */
- return btnNONE; // when all others fail, return this...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement