Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
VE1DX
Published © GPL3+

Interrupt Servie Routines using an Arduino 2560 Mega

How to send audio and visual Morse Code with simple button pushes!

IntermediateFull instructions provided3,713
Interrupt Servie Routines using an Arduino 2560 Mega

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×4
5 mm LED: Red
5 mm LED: Red
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

CW Keyer with Interrupts

Code

Morse Code Interrupt Driven Sketch

Arduino
Simply download to Arduino Mega with the IDE
/*
 *   Simple Arduino CW program to emulate a Memory Keyer 
 *   with sound and a flashing LED Has 4 pre-defined memories.
 *   Text is shown on a 0.91" OLED display. We are using an 
 *   Arduino Mega 2560 because we need 4 hardware interrupts
 *   
 *   Paul M Dunphy, VE1DX
 *   
 *   March 2020
 */
 
#include "U8glib.h"
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);

char *myMem[] = {
                 "CQ CQ VE1DX TEST",  
                 "DE VE1DX 5NN 05",
                 "TU DE VE1DX TEST",  
                 "AGN ?"
                 };
char mess[100];                

const char* MorseTable[] = {
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  // space, !, ", #, $, %, &, '
  NULL, "-.-.--", ".-..-.", ".----.","...-.-", NULL, NULL,NULL,
  // ( ) * + , - . /
  "-.--.", "-.--.-", NULL, ".-.-.", "--..--", "-....-", ".-.-.-", "-..-.",
  // 0 1 2 3 4 5 6 7
  "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...",
  // 8 9 : ; < = > ?
  "---..", "----.", "---...", "-.-.-.", NULL, "-...-", NULL, "..--..",
  // @ A B C D E F G
  ".--.-.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
  // H I J K L M N O
  "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
  // P Q R S T U V W
  ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
  // X Y Z [ \ ] ^ _
  "-..-", "-.--", "--..", NULL, NULL, NULL, NULL, "..--.-",
  // ' a b c d e f g
  NULL, ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
  // h i j k l m n o
  "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
  // p q r s t u v w
  ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
  // x y z { | } ~ DEL
  "-..-", "-.--", "--..", NULL, NULL, NULL, NULL, NULL,
};

int dotLength = 40;
int dashLength = dotLength * 3;
int CW_pin = A0;
int flashPin = 13;
int freq = 650;
int Pin2 = 2;
int Pin3 = 3;
int Pin18 = 18;
int Pin19 = 19;
volatile int mess_number = 0;
volatile int buttonState = 0;

volatile boolean pressed = false;


void clearOLED(){
    u8g.firstPage(); 
    do {
    } while( u8g.nextPage() );
}



void prompt(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 22, "Press 1-4");
}



void showText(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 22, mess);
}



void setup()
  {
    clearOLED();    
    memset(mess,0,strlen(mess));
    pinMode(flashPin, OUTPUT);
    pinMode(CW_pin, OUTPUT);
    pinMode(Pin2, INPUT_PULLUP);
    pinMode(Pin3, INPUT_PULLUP);
    pinMode(Pin18, INPUT_PULLUP);
    pinMode(Pin19, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(Pin2), ISR_2, RISING);
    attachInterrupt(digitalPinToInterrupt(Pin3), ISR_3, RISING);
    attachInterrupt(digitalPinToInterrupt(Pin18), ISR_18, RISING);
    attachInterrupt(digitalPinToInterrupt(Pin19), ISR_19, RISING);
  }



void loop() 
  {
  int i = 0; 
  char ch;
  
  u8g.firstPage(); 
  do {
       prompt();
     } while( u8g.nextPage() );

  
  if (pressed == true) 
    { 
      strcpy(mess, myMem[mess_number-1]);
      
      u8g.firstPage(); 
      do {
         showText();
         } while( u8g.nextPage() );

      ch = mess[i];
      while (ch != NULL )
        {
          i++;
          makeDashDot(MorseTable[ch]);
          delay(dotLength * 2);
          ch = mess[i];
         }
       pressed = false;
       i = 0; 
     }

  }
  


void makeDashDot(const char * morseCode)
{
  int i = 0;
  while (morseCode[i] != 0)
  {
    if (morseCode[i] == '.') {
      dot();
    } else if (morseCode[i] == '-') {
      dash();
    }
    i++;
  }
}



void dot()
{
  digitalWrite(flashPin, HIGH);
  tone(CW_pin, freq);
  delay(dotLength);
  digitalWrite(flashPin, LOW);
  noTone(CW_pin);
  delay(dotLength);
}



void dash()
{
  digitalWrite(flashPin, HIGH);
  tone(CW_pin, freq);
  delay(dashLength);
  digitalWrite(flashPin, LOW);
  noTone(CW_pin);
  delay(dotLength);
}


void ISR_2()          
{    
   noInterrupts();   
   buttonState = digitalRead(Pin2);                                                             
   mess_number = 1;
   pressed = true;
   interrupts();
}



void ISR_3()          
{       
   noInterrupts();    
   buttonState = digitalRead(Pin3);                                                           
   mess_number = 2;
   pressed = true;
   interrupts();
}



void ISR_18()          
{     
   noInterrupts();  
   buttonState = digitalRead(Pin18);                                                               
   mess_number = 3;
   pressed = true;
   interrupts();   
}



void ISR_19()          
{              
   noInterrupts();       
   buttonState = digitalRead(Pin19);                                                 
   mess_number = 4;
   pressed = true;
   interrupts();
}

Credits

VE1DX
2 projects • 6 followers
Contact

Comments

Please log in or sign up to comment.