Monday, November 9, 2009

First Arduino Sketch - USB/LED morse code generator

I recently got a Duemilanove - of course I wanted to try it out, but all my "bits" are in storage while the basement is renovated.  So, what is something interesting that only uses the built-in LED and the USB serial interface?     Decided to try a morse code generator with adjustable delays to match the proper WPM timing.


//what to do when you only have the builtin led and usb and all your other bits are in storage
//morse code of course!
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

int incomingByte = 0;
char buffer[1025];
int buffercount=0;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 240;           // interval at which to blink (milliseconds)
int wpm = 5; //stabdard is how many time paris is sent in a min.  Paris is 50 dots durations using standard timing

void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
  buffer[1024]='\0'; 
}

void dot()
{
    digitalWrite(ledPin,HIGH);
    delay(interval);
    digitalWrite(ledPin,LOW);
    delay(interval);
}
void dash()
{
    digitalWrite(ledPin,HIGH);
    delay(interval*3);
    digitalWrite(ledPin,LOW);
    delay(interval);
}


void dotdash(char *s)
{
  Serial.println(s);
  for(int i=0;i<5;i++)  //5 as morse has 1-5 ._ per letter
  {
    if(s[i]=='\0')
    {
      break;
    }
    else if(s[i]=='.')
    {
      dot();
    }
    else if(s[i]=='-')
    {
      dash();
    }
  }
  delay(interval*3); // intra-char delay
}

void morse(char letter)
{
  Serial.print(letter, BYTE);
  switch(letter)
  {
    case 'A': dotdash(".-"); break;
    case 'B': dotdash("-..."); break;
    case 'C': dotdash("-.-."); break;
    case 'D': dotdash("-.."); break;
    case 'E': dotdash("."); break;
    case 'F': dotdash("..-."); break;
    case 'G': dotdash("--."); break;
    case 'H': dotdash("...."); break;
    case 'I': dotdash(".."); break;
    case 'J': dotdash(".---"); break;
    case 'K': dotdash("-.-"); break;
    case 'L': dotdash(".-.."); break;
    case 'M': dotdash("--"); break;
    case 'N': dotdash("-."); break;
    case 'O': dotdash("---"); break;
    case 'P': dotdash(".--."); break;
    case 'Q': dotdash("--.-"); break;
    case 'R': dotdash(".-."); break;
    case 'S': dotdash("..."); break;
    case 'T': dotdash("-"); break;
    case 'U': dotdash("..-"); break;
    case 'V': dotdash("...-"); break;
    case 'W': dotdash(".--"); break;
    case 'X': dotdash("-..-"); break;
    case 'Y': dotdash("-.--"); break;
    case 'Z': dotdash("--.."); break;
    case '0': dotdash("-----"); break;
    case '1': dotdash(".----"); break;
    case '2': dotdash("..---"); break;
    case '3': dotdash("...--"); break;
    case '4': dotdash("....-"); break;
    case '5': dotdash("....."); break;
    case '6': dotdash("-...."); break;
    case '7': dotdash("--..."); break;
    case '8': dotdash("---.."); break;
    case '9': dotdash("----."); break;
    case '+': wpm++; interval=60000/(wpm*50);  Serial.print("WPM: "); Serial.print(wpm,DEC); Serial.print("/"); Serial.println(interval, DEC); break;
    case '-': wpm--;  if(wpm<1) wpm=1;; interval=60000/(wpm*50);  Serial.print("WPM: "); Serial.print(wpm,DEC); Serial.print("/"); Serial.println(interval, DEC); break;
    case ' ': Serial.println("[end of word]"); delay(interval*7);break;
    default: break;   
  }
}


char toupper(char l)
{
  if(l >= 97 && l <= 122)
  {
    return l-32;
  }
  else return l;
}

void loop()
{
  while(Serial.available() > 0 && buffercount < 1024) {
    incomingByte = Serial.read();
    buffer[buffercount++]=toupper(incomingByte);
    buffer[buffercount]='\0';
  }
  
  if (buffercount > 0) {
    for(int i=0;i<1024;i++)
    {
      if(buffer[i]=='\0')
      {
        buffer[0]='\0'; // reset buffer
        buffercount=0;
        break; //end for loop
      }
      else
      {
        morse(buffer[i]);
      }
    }
  }

}

No comments:

Post a Comment