Interrupt Servie Routines using an Arduino 2560 Mega
How to send audio and visual Morse Code with simple button pushes!
Components and supplies
1
5 mm LED: Red
1
Arduino Mega 2560
1
Jumper wires (generic)
1
Resistor 330 ohm
1
Solderless Breadboard Full Size
4
Resistor 10k ohm
Project description
Code
Morse Code Interrupt Driven Sketch
arduino
Simply download to Arduino Mega with the IDE
1/* 2 * Simple Arduino CW program to emulate a Memory Keyer 3 * with sound and a flashing LED Has 4 pre-defined memories. 4 * Text is shown on a 0.91" OLED display. We are using an 5 * Arduino Mega 2560 because we need 4 hardware interrupts 6 * 7 * Paul M Dunphy, VE1DX 8 * 9 * March 2020 10 */ 11 12#include "U8glib.h" 13U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); 14 15char *myMem[] = { 16 "CQ CQ VE1DX TEST", 17 "DE VE1DX 5NN 05", 18 "TU DE VE1DX TEST", 19 "AGN ?" 20 }; 21char mess[100]; 22 23const char* MorseTable[] = { 24 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 25 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 26 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 27 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 28 // space, !, ", #, $, %, &, ' 29 NULL, "-.-.--", ".-..-.", ".----.","...-.-", NULL, NULL,NULL, 30 // ( ) * + , - . / 31 "-.--.", "-.--.-", NULL, ".-.-.", "--..--", "-....-", ".-.-.-", "-..-.", 32 // 0 1 2 3 4 5 6 7 33 "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", 34 // 8 9 : ; < = > ? 35 "---..", "----.", "---...", "-.-.-.", NULL, "-...-", NULL, "..--..", 36 // @ A B C D E F G 37 ".--.-.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", 38 // H I J K L M N O 39 "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", 40 // P Q R S T U V W 41 ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", 42 // X Y Z [ \] ^ _ 43 "-..-", "-.--", "--..", NULL, NULL, NULL, NULL, "..--.-", 44 // ' a b c d e f g 45 NULL, ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", 46 // h i j k l m n o 47 "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", 48 // p q r s t u v w 49 ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", 50 // x y z { | } ~ DEL 51 "-..-", "-.--", "--..", NULL, NULL, NULL, NULL, NULL, 52}; 53 54int dotLength = 40; 55int dashLength = dotLength * 3; 56int CW_pin = A0; 57int flashPin = 13; 58int freq = 650; 59int Pin2 = 2; 60int Pin3 = 3; 61int Pin18 = 18; 62int Pin19 = 19; 63volatile int mess_number = 0; 64volatile int buttonState = 0; 65 66volatile boolean pressed = false; 67 68 69void clearOLED(){ 70 u8g.firstPage(); 71 do { 72 } while( u8g.nextPage() ); 73} 74 75 76 77void prompt(void) { 78 u8g.setFont(u8g_font_unifont); 79 u8g.drawStr( 0, 22, "Press 1-4"); 80} 81 82 83 84void showText(void) { 85 u8g.setFont(u8g_font_unifont); 86 u8g.drawStr( 0, 22, mess); 87} 88 89 90 91void setup() 92 { 93 clearOLED(); 94 memset(mess,0,strlen(mess)); 95 pinMode(flashPin, OUTPUT); 96 pinMode(CW_pin, OUTPUT); 97 pinMode(Pin2, INPUT_PULLUP); 98 pinMode(Pin3, INPUT_PULLUP); 99 pinMode(Pin18, INPUT_PULLUP); 100 pinMode(Pin19, INPUT_PULLUP); 101 attachInterrupt(digitalPinToInterrupt(Pin2), ISR_2, RISING); 102 attachInterrupt(digitalPinToInterrupt(Pin3), ISR_3, RISING); 103 attachInterrupt(digitalPinToInterrupt(Pin18), ISR_18, RISING); 104 attachInterrupt(digitalPinToInterrupt(Pin19), ISR_19, RISING); 105 } 106 107 108 109void loop() 110 { 111 int i = 0; 112 char ch; 113 114 u8g.firstPage(); 115 do { 116 prompt(); 117 } while( u8g.nextPage() ); 118 119 120 if (pressed == true) 121 { 122 strcpy(mess, myMem[mess_number-1]); 123 124 u8g.firstPage(); 125 do { 126 showText(); 127 } while( u8g.nextPage() ); 128 129 ch = mess[i]; 130 while (ch != NULL ) 131 { 132 i++; 133 makeDashDot(MorseTable[ch]); 134 delay(dotLength * 2); 135 ch = mess[i]; 136 } 137 pressed = false; 138 i = 0; 139 } 140 141 } 142 143 144 145void makeDashDot(const char * morseCode) 146{ 147 int i = 0; 148 while (morseCode[i] != 0) 149 { 150 if (morseCode[i] == '.') { 151 dot(); 152 } else if (morseCode[i] == '-') { 153 dash(); 154 } 155 i++; 156 } 157} 158 159 160 161void dot() 162{ 163 digitalWrite(flashPin, HIGH); 164 tone(CW_pin, freq); 165 delay(dotLength); 166 digitalWrite(flashPin, LOW); 167 noTone(CW_pin); 168 delay(dotLength); 169} 170 171 172 173void dash() 174{ 175 digitalWrite(flashPin, HIGH); 176 tone(CW_pin, freq); 177 delay(dashLength); 178 digitalWrite(flashPin, LOW); 179 noTone(CW_pin); 180 delay(dotLength); 181} 182 183 184void ISR_2() 185{ 186 noInterrupts(); 187 buttonState = digitalRead(Pin2); 188 mess_number = 1; 189 pressed = true; 190 interrupts(); 191} 192 193 194 195void ISR_3() 196{ 197 noInterrupts(); 198 buttonState = digitalRead(Pin3); 199 mess_number = 2; 200 pressed = true; 201 interrupts(); 202} 203 204 205 206void ISR_18() 207{ 208 noInterrupts(); 209 buttonState = digitalRead(Pin18); 210 mess_number = 3; 211 pressed = true; 212 interrupts(); 213} 214 215 216 217void ISR_19() 218{ 219 noInterrupts(); 220 buttonState = digitalRead(Pin19); 221 mess_number = 4; 222 pressed = true; 223 interrupts(); 224}
Downloadable files
CW Keyer with Interrupts
CW Keyer with Interrupts

CW Keyer with Interrupts
CW Keyer with Interrupts

Comments
Only logged in users can leave comments