0

I'm using a Mega 2560 and Uno and want to input text to the serial monitor which can be stored in character array char in[]; I want array specifically as I'll be breaking each character to trigger different code for making a morse code.

    #include<Arduino.h>
//#include<Softwareserial.h>

int Speaker1 = 4;
int Speaker2 = 6;
int LED = 2;
int Relay = 12;
char n = '0';
char in[];
//int outPins[] = { 2, 4, 6, 8 };

void high() {

  digitalWrite(Speaker1, HIGH);
  tone(Speaker2, 800);
  digitalWrite(LED, HIGH);
  digitalWrite(Relay, HIGH);

}

void low() {

  digitalWrite(Speaker1, LOW);
  tone(Speaker2, 800);
  digitalWrite(LED, LOW);
  digitalWrite(Relay, LOW);

}

void setup() {

  Serial.begin(9600);

  pinMode(Speaker1, OUTPUT);
  pinMode(Speaker2, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(Relay, OUTPUT);

  Serial.print("Enter your desired text: ");
  //in = Serial.readString();  /*<-------------HELP NEEDED HERE */

  for (int i = 0; i < strlen(in); i++) {

    n = in[i];

    switch (n) {

      case 'A':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'B':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'C':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'D':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'E':
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'F':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'G':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'H':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'I':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'J':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'K':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'L':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'M':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'N':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'O':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'P':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'Q':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'R':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'S':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'T':
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'U':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'V':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'W':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'X':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Y':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Z':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case ' ':
        delay(700);
        break;
      case '1':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '2':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '3':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '4':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '5':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '6':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '7':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '8':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '9':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '10':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;

    }

  }

}

void loop() {

}
18
  • 2
    Have you googled it? See hackingmajenkoblog.wordpress.com/2016/02/01/… – Edgar Bonet Jan 9 '18 at 8:37
  • Are you going to send each char at once or you will write a sentence and send? – rvbarreto Jan 9 '18 at 8:38
  • Welcome to Arduino SE. Please realize that this is not a free design house, homework-answering service or an on-line technical encyclopedia, copied out to you on demand. People will help you take the next step if your question shows that you've done as much as you possibly could on your own - which your post doesn't. Please revise your question showing your work and findings so far. Or delete the question if Internet searches give you your answer anyway. – user31481 Jan 9 '18 at 8:40
  • @rvbarreto I will create a sentence and send. – Kunal Gehlot Jan 9 '18 at 8:40
  • What's your question? – user31481 Jan 9 '18 at 8:40
2

I suggested in a comment you read the blog post Reading Serial on the Arduino. In this post, Majenko shows a readline() function that does almost exactly what you want. This function is non-blocking, which, in the embedded world, is most of the time a mandatory feature. However, in one of your comments you wrote that you want a blocking function:

I need the program to "wait" like [...] scanf

Degrading a non-blocking function into a blocking one is as easy as wrapping the non-blocking function in a loop. Here is a code snippet for reading a CR-terminated string, which is similar to a blocking version of Majenko's readline():

char buffer[80];
size_t pos = 0;
for (;;) {
    int c = Serial.read();
    if (c == -1) continue;   // no input
    if (c == '\r') {         // end of line
        buffer[pos] = '\0';  // terminate the string
        break;
    }
    if (pos < sizeof buffer - 1) {
        buffer[pos++] = c;
    }
}

That being said, I could not resist the temptation to brutally simplify your program. The key to the simplification is to consider the Morse code as a data structure. Then, the code for keying a string is generic: you look up each character in the code table and key it in turn. This saves the long switch/case:

struct code_t {
    char c;
    const char *code;
};

static const code_t morse_code[] = {
    {'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', "--.." },
    {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
    {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."},
    {'9', "----."}, {'0', "-----"},
    {' ', " "    }, {'\0', NULL}  // sentinel
};

static inline void high() { digitalWrite(LED_BUILTIN, HIGH); }
static inline void low()  { digitalWrite(LED_BUILTIN, LOW);  }

// Key a string in Morse.
void key(const char *s)
{
    for (; *s; s++) {  // *s = current letter

        // Find this letter's code.
        const code_t *p;
        for (p = morse_code; p->c; p++) {
            if (p->c == toupper(*s)) break;  // found
        }
        if (!p->c) continue;  // not found -> ignore

        // Send this code's symbols.
        for (const char *code = p->code; *code; code++) {
            if (*code != ' ')
                high();
            switch (*code) {
                case ' ': delay(100); break;
                case '.': delay(100); break;
                case '-': delay(300); break;
            }
            low();
            delay(100);
        }
        delay(200);  // inter-letter spacing = 300 ms
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    // Read a string.
    char buffer[80];
    size_t pos = 0;
    for (;;) {
        int c = Serial.read();
        if (c == -1) continue;   // no input
        if (c == '\r') {         // end of line
            buffer[pos] = '\0';  // terminate the string
            break;
        }
        if (pos < sizeof buffer - 1) {
            buffer[pos++] = c;
        }
    }

    // Key it in Morse.
    key(buffer);
}

Note that this program reads CR-terminated strings from the Serial port and keys them on the builtin LED. If you want your program to work on a single string, you can call exit() at the end of loop() or, alternatively, merge loop() into setup() and provide an empty loop().

0

I don't understand exactly why your signals are much different from 'real' morse code, e.g. A is .-

Instead of calling each high and low separately like the 'a' for example:

  case 'A':
    high();
    delay(100);
    low();
    delay(100);
    high();
    delay(300);
    low();
    delay(300);
    break;

call it like this:

case 'A':
  send2(false, true); /* means .-
  break;

Than create a function send2 which sends a . and a - Something like:

void send2(bool b1, bool b2)
{
   send(b1);
   send(b2);
   delay(300); /* Extra wait in between characters */
}

For characters needing 1, 3 or 4 characters, you can make send 1, send3 and send4 accordingly.

void send(bool b1)
{
   digitalWrite(Speaker1, b1 ? HIGH : LOW);
   tone(Speaker2, b1 ? 800);
   digitalWrite(LED, HIGH);
   digitalWrite(Relay, HIGH);
   delay(b1 ? 300 : 100); /* Assuming HIGH needs delay of 300 and low of 100 */
}

void low() {

  digitalWrite(Speaker1, LOW);
  tone(Speaker2, 800); /* I would expect two different values for HIGH and LOW */
  digitalWrite(LED  , b1 ? HIGH : LOW);
  digitalWrite(Relay, b1 ? HIGH : LOW);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.