Morse Code Converter

Based on the array and text functions we learnt in class, I made a morse code converter for this week’s assignment. The converter has two modes. In the first mode, the user can enter up to five letters or numbers. The program will convert the input into morse code. In the second mode, the program will automatically generate five lines of Morse Code. The user then needs to enter the corresponding letter or number for each line of code. The screen will print Correct! if the user’s input is accurate.

Morse Code Mode 1Morse Code Mode 1

Morse Code Mode 2

 

Here’s my code for the program.

char[] letterArray = new char[5];
char[] patternArray = new char[5];
char letter_num = 0;
float xPos = 50;
float yPos;
char letter; 
int count = 0;
int count2 = 0;
float diam = 20;
int mode;
int a_num;
int b_num;
int c_num;
int correct = 0;

void setup() {
  size(600, 600);
}

void draw() {
  background(0);
  yPos = -30;

  if (mode == 1) {
    println("Mode 1");

    for (int j = 0; j < 5; j++) {    
      yPos += 120;
      fill(random(0, 255), random(0, 255), random(0, 255));
      textSize(100);
      letter = letterArray[j];
      text(letter, xPos, yPos);
      drawcode();
    }

    if (count == 5) {
      count = 0;
      char[] letterArray = new char[5];
    }
  }


  if (mode == 2) {
    println("Mode 2");

    for (int j = 0; j < 5; j++) { 
      yPos += 120;
      drawcode();

      fill(random(0, 255), random(0, 255), random(0, 255));
      textSize(100);
      letter = patternArray[j];
      text(letter, xPos, yPos);

      if (count == 5) {
        count = 0;
        char[] letterArray = new char[5];
      }
    }

    if (char(letterArray[4]) > 0 && char(patternArray[4]) > 0) {
      check();
    }
  }
}

void keyPressed() {
  //modes
  if (key == '-') {
    mode = 1;
  }
  if (key == '=') {
    mode = 2;
  }

  if (key == ' ') { //press space to restart
    mode = 0;
    letter_num = 0;
    a_num = 0;
    count = 0;
    count2 = 0;
    correct = 0;
    for (int a = 0; a < 5; a++) {
      letterArray[a] = 0;
    } 
    for (int b = 0; b < 5; b++) {
      patternArray[b] = 0;
    }
    background(0);
  }

  if (mode == 1) {
    if (key != 61 && key != 45) {
      letter_num = key; //char
      letterArray[count] = letter_num;
      count += 1;
    }
  }

  if (mode == 2) {

    if (key == CODED) {
      a_num = round(random(97, 122)); //generate code of a random letter
      b_num = round(random(48, 57)); //generate code of a random number
      c_num = round(random(0, 1));
      if (c_num == 0) {
        letterArray[count] = char(a_num);
      }
      if (c_num == 1) {
        letterArray[count] = char(b_num);
      }
      count += 1;
    }

    if (key != CODED && key != 61 && key != 45) {
      letter_num = key;
      patternArray[count2] = letter_num;
      count2 += 1;


      if (count2 == 5) {
        count2 = 0;
        char[] patternArray = new char[5];
      }
    }
  }
}

Functions:

void drawcode() {
  noStroke();

  for (int p = 0; p < 5; p++) {
    int value = int(letterArray[p]);
    float y_ = -30+(p+1)*120-diam;
    float x_ = xPos+100;
    float rec_wid = 20;
    float rec_len = 70;
    float gap = 20;
    float radius = diam/2;

    fill(random(0, 255), random(0, 255), random(0, 255));
    //fill(255);

    if (value == 97 || value == 65) { //A
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
    }

    if (value == 98 || value == 66) { //B
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      ellipse(x_+rec_len+gap*2+radius*3, y_, diam, diam);
      ellipse(x_+rec_len+gap*3+radius*5, y_, diam, diam);
    }

    if (value == 99 || value == 67) { //C
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      rect(x_+rec_len+gap*2+radius*2, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len*2+gap*3+radius*3, y_, diam, diam);
    }

    if (value == 100 || value == 68) { //D
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      ellipse(x_+rec_len+gap*2+radius*3, y_, diam, diam);
    }

    if (value == 101 || value == 69) { //E
      ellipse(x_, y_, diam, diam);
    }

    if (value == 102 || value == 70) { //F
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      rect(x_+gap*2+radius*3, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*3+radius*4+rec_len, y_, diam, diam);
    }

    if (value == 103 || value == 71) { //G
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*2+rec_len*2+radius, y_, diam, diam);
    }

    if (value == 104 || value == 72) { //H
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
      ellipse(x_+gap*3+radius*6, y_, diam, diam);
    }

    if (value == 105 || value == 73) { //I
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
    }

    if (value == 106 || value == 74) { //J
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*2+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*3+rec_len*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 107 || value == 75) { //K
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      rect(x_+rec_len+gap*2+radius*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 108 || value == 76) { //L
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      ellipse(x_+radius*2+gap*2+rec_len, y_, diam, diam);
      ellipse(x_+radius*4+gap*3+rec_len, y_, diam, diam);
    }

    if (value == 109 || value == 77) { //M
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
    }

    if (value == 110 || value == 78) { //N
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      ellipse(x_+rec_len+gap*2+radius*3, y_, diam, diam);
    }

    if (value == 111 || value == 79) { //O
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+gap*2+rec_len*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 112 || value == 80) { //P
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*2+rec_len, y_-radius, rec_len, rec_wid);
      ellipse(x_+radius*2+gap*3+rec_len*2, y_, diam, diam);
    }

    if (value == 113 || value == 81) { //Q
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*2+rec_len*2+radius, y_, diam, diam);
      rect(x_+gap*3+rec_len*2+radius*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 114 || value == 82) { //R
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      ellipse(x_+radius*2+gap*2+rec_len, y_, diam, diam);
    }

    if (value == 115 || value == 83) { //S
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
    }

    if (value == 116 || value == 84) { //T
      rect(x_, y_-radius, rec_len, rec_wid);
    }

    if (value == 117 || value == 85) { //U
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      rect(x_+gap*2+radius*3, y_-radius, rec_len, rec_wid);
    }

    if (value == 118 || value == 86) { //V
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
      rect(x_+gap*3+radius*5, y_-radius, rec_len, rec_wid);
    }

    if (value == 119 || value == 87) { //W
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*2+rec_len, y_-radius, rec_len, rec_wid);
    }

    if (value == 120 || value == 88) { //X
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      ellipse(x_+rec_len+gap*2+radius*3, y_, diam, diam);
      rect(x_+rec_len+gap*3+radius*4, y_-radius, rec_len, rec_wid);
    }

    if (value == 121 || value == 89) { //Y
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      rect(x_+rec_len+gap*2+radius*2, y_-radius, rec_len, rec_wid);
      rect(x_+rec_len*2+gap*3+radius*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 122 || value == 90) { //Z
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*2+rec_len*2+radius, y_, diam, diam);
      ellipse(x_+gap*3+rec_len*2+radius*3, y_, diam, diam);
    }

    if (value == 48) { //0
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+gap*2+rec_len*2, y_-radius, rec_len, rec_wid);
      rect(x_+gap*3+rec_len*3, y_-radius, rec_len, rec_wid);
      rect(x_+gap*4+rec_len*4, y_-radius, rec_len, rec_wid);
    }

    if (value == 49) { //1
      ellipse(x_, y_, diam, diam);
      rect(x_+radius+gap, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*2+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*3+rec_len*2, y_-radius, rec_len, rec_wid);
      rect(x_+radius+gap*4+rec_len*3, y_-radius, rec_len, rec_wid);
    }

    if (value == 50) { //2
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      rect(x_+gap*2+radius*3, y_-radius, rec_len, rec_wid);
      rect(x_+gap*3+radius*3+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+gap*4+radius*3+rec_len*2, y_-radius, rec_len, rec_wid);
    }

    if (value == 51) { //3
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
      rect(x_+gap*3+radius*5, y_-radius, rec_len, rec_wid);
      rect(x_+gap*4+radius*5+rec_len, y_-radius, rec_len, rec_wid);
    }

    if (value == 52) { //4
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
      ellipse(x_+gap*3+radius*6, y_, diam, diam);
      rect(x_+gap*4+radius*7, y_-radius, rec_len, rec_wid);
    }

    if (value == 53) { //5
      ellipse(x_, y_, diam, diam);
      ellipse(x_+gap+radius*2, y_, diam, diam);
      ellipse(x_+gap*2+radius*4, y_, diam, diam);
      ellipse(x_+gap*3+radius*6, y_, diam, diam);
      ellipse(x_+gap*4+radius*8, y_, diam, diam);
    }

    if (value == 54) { //6
      rect(x_, y_-radius, rec_len, rec_wid);
      ellipse(x_+rec_len+gap+radius, y_, diam, diam);
      ellipse(x_+rec_len+gap*2+radius*3, y_, diam, diam);
      ellipse(x_+rec_len+gap*3+radius*5, y_, diam, diam);
      ellipse(x_+rec_len+gap*4+radius*7, y_, diam, diam);
    }

    if (value == 55) { //7
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*2+rec_len*2+radius, y_, diam, diam);
      ellipse(x_+gap*3+rec_len*2+radius*3, y_, diam, diam);
      ellipse(x_+gap*4+rec_len*2+radius*5, y_, diam, diam);
    }

    if (value == 56) { //8
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+gap*2+rec_len*2, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*3+rec_len*3+radius, y_, diam, diam);
      ellipse(x_+gap*4+rec_len*3+radius*3, y_, diam, diam);
    }

    if (value == 57) { //9
      rect(x_, y_-radius, rec_len, rec_wid);
      rect(x_+gap+rec_len, y_-radius, rec_len, rec_wid);
      rect(x_+gap*2+rec_len*2, y_-radius, rec_len, rec_wid);
      rect(x_+gap*3+rec_len*3, y_-radius, rec_len, rec_wid);
      ellipse(x_+gap*4+rec_len*4+radius, y_, diam, diam);
    }
  }
}

void check() {
  int correct = 0;
  
  for (int i = 0; i < 5; i++) {
    if (letterArray[i] == patternArray[i]) {
      correct += 1;
    }
  }
  
  if (correct == 5){
    background(0);
    fill(255);
    textSize(50);
    text("Correct!",100,250);
    xPos = random(0,width);
    yPos = random(0,height);
    drawcode();
  }
}

 

Leave a Reply