Decoding problems

Hello there,

I'm struggling with a code.
It should decode morse code.
The code works on a light sensor. When i sent light signals to the sensor, it decodes the letters.
(in serial monitor)
The problem is that it stops with recognizing after one letter.
And so far only the T.
Which is fine. (because for me its not so much about the letters but more about the signals)
I would like to make a servo motor work when:
3x T is being recognized.
Can someone please help me with this?

Thank you in advance

#include <Servo.h>
#define THRESHOLD (44)
#define MAX_SAMPLES (5)
Servo myservo;
#define BAUD (100.0)
#define WAIT (100.0)
#define AVG_LONG (BAUD*5.0/WAIT)
#define AVG_SHORT (BAUD*1.0/WAIT)
#define AVG_NEWWORD (BAUD*7.0/WAIT)
#define MINIMUM (AVG_SHORT/4.0)
int pos = 0;
#define MAX_PATTERN (64)

#define NUM_CODES (54)
                      // 0 10 20 30 40 50
                      // 0123456789012345678901234567890123456789012345678901234
static const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?'!/()&:;=+-_\"$@";
                      // each code represents a character.
                      // 1 represents a dot and 0 represents a dash.
                      // the order of these codes matches the order of the characters in 
                      // *letters.
static const char *codes[NUM_CODES] = {
"10",                 // A, codes[0]
"0111",               // B, codes[1]
"0101",               // C
"011",                // D
"1",                  // E
"1101",               // F
"001",                // G
"1111",               // H
"11",                 // I
"1000",               // J
"010",                // K
"1011",               // L
"00",                 // M
"01",                 // N
"000",                // O
"1001",               // P
"0010",               // Q
"101",                // R
"111",                // S
"0",                  // T
"110",                // U
"1110",               // V
"100",                // w
"0110",               // x
"0100",               // y
"0011",               // z
"00000",              // 0
"10000",              // 1
"11000",              // 2
"11100",              // 3
"11110",              // 4
"11111",              // 5
"01111",              // 6
"00111",              // 7
"00011",              // 8
"00001",              // 9
"101010",             // .
"001100",             // ,
"110011",             // ?
"100001",             // '
"010100",             // !
"01101",              // /
"01001",              // (
"010010",             // )
"10111",              // &
"000111",             // :
"010101",             // ;
"01110",              // =
"10101",              // +
"01110",              // -
"110010",             // _
"101101",             // "
"1110110",            // $
"100101",             // @, codes[54]
};

int top=0;

int samples[MAX_SAMPLES];
int si=0;
int mi=0;
int total=0;

int c=0;
int is_on=0;

char pattern[MAX_PATTERN];
int pi=0;

void setup() {
   myservo.attach(9);
Serial.begin(57600);

for(int i=0;i<MAX_SAMPLES;++i) {
samples[i]=0;
}
for(int i=0;i<MAX_PATTERN;++i) {
pattern[i]=0;
}
}

void loop() {
int volume=analogRead(0);

total -= samples[si];
samples[si] = volume;
total += samples[si];
if( mi < MAX_SAMPLES ) mi++;
si = (si+1) % MAX_SAMPLES;
int average = total / mi;

if( top < average ) top = average;

int x = 10.0 * (float)(average-THRESHOLD)/(float)(top-THRESHOLD);
if(x<0) x=0;
if(x>10) x=10;

if(x>1) {
                                      // noise!
if(is_on==0) {
                                      // noise has just started.
if( c > MINIMUM ) {
                                      // Was the silence a new word or a new letter?
if( c > (AVG_NEWWORD+AVG_SHORT)/2.0 ) {
pattern[pi]=0;
findLetter();
                                      // new word, extra \n
Serial.println();
                                      // start counting - and . all over again.
pi=0;
} else if( c > (AVG_LONG+AVG_SHORT)/2.0 ) {
pattern[pi]=0;
findLetter();
                                      // start counting - and . all over again.
pi=0;
}

}
                                      // remember noise started
is_on=1;
c=0;
}
} else {
                                      // silence!
if(is_on==1) {
                                      // silence is new
if( c > MINIMUM ) {
                                      // Was the noise a long or a short?
if( c > (AVG_LONG + AVG_SHORT)/2.0 ) {
                                      // long
Serial.print('-');
pattern[pi++]='0';
} else {
                                      // short
Serial.print('.');
pattern[pi++]='1';
}
}
                                      // remember silence started
is_on=0;
c=0;
}
}

c++;

delay(WAIT);
}

                          // pattern contains the received longs and shorts,
                          // saved as 1s and 0s. Find the matching code in the list
                          // then find the matching printable character.
                          // print '?' if nothing is found
void findLetter() {
int i,j;
                          // go through all the codes
for(i=0;i<NUM_CODES;i++) {
                          // check if code[i] matches pattern exactly.
if(strlen(pattern) == strlen(codes[i]) && strcmp(pattern,codes[i])==0) {
                          // match!
Serial.print(' ');
Serial.println(letters[i]);
Serial.print("GELUKT!");

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1500);                       // waits 15ms for the servo to reach the position
  }
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
}
}
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1500);                       // waits 15ms for the servo to reach the position
  }

180 steps at 1.5 seconds per step will take 4 and half minutes to move. How is that going to make anyone happy?

After each letter? Hopefully you plan to send only extremely short messages.

What do your Serial.print() statements tell you is happening?

@Elsa123:
Your decode algorithm is in need of some work. You have hard-coded 100 BAUD as the speed, so you are going to have to take the light signal from an LDR or phototransistor and work with that at the 10 characters per second speed.

So, you are going to have to accept characters, mark (dit) and space (dah) until you get to a word space which is 7x the "dit" time. Then you do the decode... you can have 1 character such as a single dit ==E or a single dash == T up to 6 characters in the decode sequence.

{
  WPM        = value;
  DITmS      = 1200 / WPM;
  DAHmS      = 3 * 1200 / WPM;
  // character break is 3 counts of quiet where dah is 3 counts of tone
  // wordSpace  = 7 * 1200 / WPM;
  wordBreak  = 7 * DITmS;    // changed from wordSpace*2/3; Key UP time in mS for WORDBREAK (space)
  Elements   = MaxElement;   // International Morse is 5 characters but ProSigns are 6 characters
  halfDIT    = DITmS/2;      // Minimum mS that Key must be UP (quiet) before MM assignment to dot/dash
  quarterDIT = DITmS/4;      // Minimum accepted value in mS for a DIT element (sloppy)
  halfDAH    = DAHmS/2;      // Maximum accepted value in mS for a DIT element (sloppy)
  DITDAH     = DITmS + DAHmS;// Maximum accepted value in mS for a DAH element (sloppy)
  DiDiDi     = DITmS * 3;    // Minimum mS that Key must be up to decode a character via MM
}

Now .... if you are just wanting something to have a limited number of decode capabilities, you should consider IR ... the IR sensor and the IR numeric remote control can be purchased together inexpensively:
http://www.miniinthebox.com/ir-receiver-module-wireless-remote-control-kit-for-arduino-1-x-cr2025_p683872.html

There are a number of published Arduino algorithms for Morse Code, such as mine: in this project

As a side note, I have used a phototransistor with a flashlight to create the contact closure for the software, so the phototransistor should would well as if it were a Morse "key".
Ray