help with if statement

Hey guys,

I have a question concerning a morse code decoder i'm working with.
When i blink light towards my light sensor I can make morse codes, and my code translates this into letters.
I would like to make an if statement that when A-A-A is recognized.
a led blinks.
What i have so far is not working.
Could anyone help me out?

Thank you

This is the if statement i'm struggling with:

#define THRESHOLD (44)
#define MAX_SAMPLES (5)

#define BAUD (100.0)
#define WAIT (5.0)
#define AVG_LONG (BAUD*3.0/WAIT)
#define AVG_SHORT (BAUD*1.0/WAIT)
#define AVG_NEWWORD (BAUD*7.0/WAIT)
#define MINIMUM (AVG_SHORT/4.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() {
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]);
return;

if(letters[0] == 'A')
{
  if(letters[0] == 'A')
  {
    if(letters[0] == 'A')
    {
      Serial.print("GELUKT!");
    }
  }
}
}
}
Serial.print('?');
}

Check each char you see. Start by checking if it was an 'A'. If not, keep checking. If it was an 'A', you can go to the next char in your sequence (checkPosition++). Now check the seen character to '-'. If it wasn't, go back to the beginning (checkPosition = 0) (and check it to see if this is the first char you want!). If is was, go to the next and keep doing it until you checked all chars in your check sequence (checkPosition == length(checkSequence)).

Thank you so much,
but i'm very new to arduino so i don't really understand,
what this will look like

Using strings uses more code that you need to. Suppose you store the array as:

byte codes[] = {
    0b01,              // A = dit dah
    0b1000,            // B = dah dit dit dit  etc.
    0b1010,            // C
    0b100,             // D
    0b0,               // E
// ...and so on...

and your code looks for a match on the pattern in the array. Once found, add the letter 'A' to the index and you have the ASCII letter. For example, you detect dah-dit-dit (i.e., D). Your search turns up index into codes[] of 3. Then:

   char c = index + 'A';

Since index will be 3, c will equal 68, which is the ASCII code for 'D'. This saves you from having a string for every letter. Punctuation and numbers would have to be treated as special cases. You should be able to shorten the letters[] array by removing the alphabet section of the array. If you add the numbers and punctuation to the codes[] array, you should be able to use the index to find the ASCII codes, too. For example, if the numbers come immediately after the alphabet, '0' would be at index = 26 (alpha runs from 0-25). Then something like (untested):

char c;

if (index < 26) {
   c = index + 'A';             // It's a letter
} else { 
   if (index < 36) {
      c = (index - 26) + '0';   // It's a number
   } else {
                                // it's punctuation
   }
}

econjack:
Using strings uses more code that you need to. Suppose you store the array as:

byte codes[] = {

0b01,              // A = dit dah
   0b1000,            // B = dah dit dit dit  etc.
   0b1010,            // C
   0b100,             // D
   0b0,               // E
// ...and so on...

That won't allow you to distinguish 0b1 (E) from 0b01 (N) or 0b001 (G). They all have the value 0b00000001. If you use simple binary you also need a way to determine the code length. The code length goes up to 7 there is not enough room left in the byte to store the length as a number. A leading 1 could be added and any 0's before the first 1, and the first 1, ignored. It saves space but increases complexity. Moving the constant data to PROGMEM would make the issue fairly moot.

You're right, John. I forgot the sentinel. See post #9 at:

http://forum.arduino.cc/index.php?topic=260795.0