Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char alphabetReference[30] = {' ', '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'}; //Alphabetical Reference Table
- char *morseTable[30] = {"dddd", "sl", "lsss", "lsls", "lss", "s", "ssls", "lls", "ssss", "ss", "slll", "lsl", "slss", "ll", "ls", "lll", "slls", "llsl", "sls", "sss", "l", "ssl", "sssl", "sll", "lssl", "lsll", "llss"}; //Morsecode Reference Table, each code corresponds to the same index in the Alphabetical Reference table.
- int sDelay = 200; //Delaytime of the short signal, "."
- int lDelay = 600; //Delaytime of the long signal, "-"
- int staticPin = 13; //Pin, where to output the signals.
- char morseString[] = "SOS"; //String of characters to morse.
- void illuminate(int time, int pin) {
- //illuminates the pin for the given amount of time.
- digitalWrite(pin, HIGH);
- delay(time);
- digitalWrite(pin, LOW);
- delay(sDelay);
- }
- void morse(int ref, boolean justPrint) {
- //parses the instruction for the corresponding character index, passed with "ref", and then calls the illuminate() function.
- if (!justPrint) {
- Serial.print("\nMorsing \"");
- Serial.print(alphabetReference[ref]);
- Serial.println("\"...");
- }
- char *strtmp = morseTable[ref];//passes the string on index "ref" in the morseTable Array.
- for (int j = 0; j < strlen(strtmp); j++) {
- //executes the code, by ripping it from the string, character by character.
- if (strtmp[j] == 's') {
- Serial.print(". ");
- if (!justPrint) {
- illuminate(sDelay, staticPin);
- }
- } else if (strtmp[j] == 'l') {
- Serial.print("- ");
- if (!justPrint) {
- illuminate(lDelay, staticPin);
- }
- } else if (strtmp[j] == 'd') {
- Serial.print("_ ");
- if (!justPrint) {
- delay(sDelay);
- }
- } else {
- Serial.print("Illegal Operation \"");
- Serial.print(strtmp[j]);
- Serial.print("\" for character \"");
- Serial.print(alphabetReference[ref]);
- Serial.println("\"");
- }
- }
- if (!justPrint) {
- delay(sDelay * 2);
- }
- }
- void setup() {
- Serial.begin(9600);
- pinMode(staticPin, OUTPUT);
- Serial.println("Character Mapping:");
- for (int i = 0; i < strlen(alphabetReference); i++) {
- //prints out all characters and morse code counterparts
- Serial.print(alphabetReference[i]);
- Serial.print("\t");
- morse(i, true);
- Serial.println();
- }
- }
- void loop() {
- Serial.println("\n-------------------------------------------");
- Serial.print("Preparing to morse \"");
- Serial.print(morseString);
- Serial.println("\"");
- for (int i = 0; i < strlen(morseString); i++) {
- //splits the string into single chars and passes them individually.
- for (int j = 0; j < strlen(alphabetReference); j++) {
- if (toupper(morseString[i]) == toupper(alphabetReference[j])) {
- morse(j, false);
- }
- }
- }
- morse(0, false);
- }
Advertisement
Add Comment
Please, Sign In to add comment