Arduino Code Problem Appearing: Expected unqualified-id

Hi Guys

I have run into a problem with the sketch I'm currently writing for my arduino. The code was working fine until I added a struct (PATTERN) and now when I try to compile the program I get errors like "expected unqualified-id before '{' token" at all my array declarations. I'm probably just overlooking something but would really appreciate any help :)

My code can be found here.

Examples of error messages:

morse_code:83: error: expected declaration before '}' token

 };
morse_code:81: error: expected unqualified-id before ',' token

   {1,0,1,1}, //Y - 25

            ^

These errors show up on every line of my alphabet declaration :(

My code can be found here.

Why can't it be found here?

const byte alphabet[26][4] = {
  {0,1,-1,-1},

What don't you understand about unsigned numbers?

Honestly, I haven't gotten around to changing those values as they have been working up until now (even with the negative values).

I have changed the negative numbers, but the problem persists:

I would guess you are running into a scope problem in there somewhere. I can look a little deeper into it later but that's the first thing that comes to mind when I see that error.

What exactly should I be looking for if it is a scope error? I don't really understand what could go wrong with regards to scope when declaring the array globally?

Kydro: What exactly should I be looking for if it is a scope error? I don't really understand what could go wrong with regards to scope when declaring the array globally?

Perhaps if you post your code, we'll be able to see what's going on. Don't make people navigate to "PasteBin" to see it. Post it here, using code tags. (As hinted at by AWOL.)

My apologies, here is my code:

/*
  Morse Code
  Uses an LED to transmit a morse code message, controlled by multiple 
  buttons to display many different messages according to the pattern 
  of the buttons being pressed.

  modified 21 Feb 2016
  by Gerrie Crafford
 */

#include <avr/pgmspace.h>

// --------------------------------------------------------------------------------------------- \\
// ------------------------------------- Setup and variables ----------------------------------- \\
// --------------------------------------------------------------------------------------------- \\

// Types
struct PATTERN {
  uint8_t one;
  uint8_t two;
};

// CONSTS
const byte MAX_BLANK_COUNT = 2;

// Pin Variables
int gLedPin1 = 13;
int gLedPin2 = 12;

int gBtn1Pin = 10;
int gBtn2Pin = 9;

// Morse Code timing variables
int gMorseBase = 750;
int gMorseShort = gMorseBase;
int gMorseLong = gMorseBase * 3;
int gMorsePause = gMorseBase;
int gMorseNewLetter = gMorseBase * 3;
int gMorseNewWord = gMorseBase * 7;

// State variables
int gBtn1State = LOW;
int gBtn2State = LOW;

int blankCount = 0;

// Misc & control variables
boolean loopAgain = false;

byte counter = 0, blankcount = 0;
PATTERN const tempPattern = { 0, 0 };

// --------------------------------------------------------------------------------------------- \\
// ------------------------------------- Alphabet in morse ------------------------------------- \\
// --------------------------------------------------------------------------------------------- \\
const byte alphabet[26][4] = {
  {0,1,2,2}, //A - 1
  {1,0,0,0}, //B - 2
  {1,0,1,0}, //C - 3
  {1,0,0,2}, //D - 4
  {0,2,2,2}, //E - 5
  {0,0,1,0}, //F - 6
  {1,1,0,2}, //G - 7
  {0,0,0,0}, //H - 8
  {0,0,2,2}, //I - 9
  {0,1,1,1}, //J - 10
  {1,0,1,2}, //K - 11
  {0,1,0,0}, //L - 12 
  {1,1,2,2}, //M - 13
  {1,0,2,2}, //N - 14
  {1,1,1,2}, //O - 15
  {0,1,1,1}, //P - 16
  {1,1,0,1}, //Q - 17
  {0,1,0,2}, //R - 18
  {0,0,0,2}, //S - 19
  {1,2,2,2}, //T - 20
  {0,0,1,2}, //U - 21
  {0,0,0,1}, //V - 22
  {0,1,1,2}, //W - 23
  {1,0,0,1}, //X - 24
  {1,0,1,1}, //Y - 25
  {1,1,0,0} //Z - 26
};

// --------------------------------------------------------------------------------------------- \\
// ----------------------------------------- Messages ------------------------------------------ \\
// --------------------------------------------------------------------------------------------- \\
uint8_t const PROGMEM sent1[23] = {
  10,25,0,9,19,0,1,13,1,26,9,14,7,0,12,9,5,6,0,10,15,21,0
};

const uint8_t* messages[1]; // Stores addresses of first elemnt of message arrays

// --------------------------------------------------------------------------------------------- \\
// ------------------------------------------- Patterns ---------------------------------------- \\
// --------------------------------------------------------------------------------------------- \\
uint8_t const AMOUNT_PATTERNS = 1;
PATTERN const patterns[1] = {
  { 3, 3 } // {00000011} {00000011}
};


// the setup function runs once when you press reset or power the board
void setup() {
 Serial.begin(9600);

 // Init pins
 pinMode(gLedPin1, OUTPUT);
 pinMode(gLedPin2, OUTPUT);
 pinMode(gBtn1Pin, INPUT);
 pinMode(gBtn2Pin, INPUT);

 digitalWrite(gLedPin1, LOW);
 digitalWrite(gLedPin2, LOW);

 // Init message address array
 messages = { &sent1[0] };
}

// the loop function runs over and over again forever
void loop() {
  gBtn1State = digitalRead(gBtn1Pin);
  gBtn2State = digitalRead(gBtn2Pin);

  if ( (gBtn1State == HIGH) || (gBtn2State == HIGH) ) { // Begin pattern recognition process
    loopAgain = true;
    counter = 0;
  }

  while (loopAgain) {
    gBtn1State = digitalRead(gBtn1Pin);
    gBtn2State = digitalRead(gBtn2Pin);

    if (gBtn1State == HIGH) { // Set the bit in the pattern byte to 1 to show that the button was on
      tempPattern.one |= 1 << counter;
      blankCount = 0;
    }

    if (gBtn2State == HIGH) {
      tempPattern.two |= 1 << counter;
      blankCount = 0;
    }

    if ( (gBtn1State == LOW) && (gBtn2State == LOW) )
      blankCount++;

    if ( (counter >= 7) || (blankCount > MAX_BLANK_COUNT) ) { // Loop ends when counter too large or too many blanks
      loopAgain = false;
      counter = 0;
      blankCount = 0;
    }

    counter++;
    delay(1000);
  }

  if ( (tempPattern.one != 0) || (tempPattern.two != 0) ) { // Patterns have values, match them to stored patterns
    Serial.println(tempPattern.one);
    Serial.println(tempPattern.two);

    for (int j = o; j < AMOUNT_PATTERNS; j++) {
      if ( (patterns[j].one == tempPattern.one) && (patterns[j].two == tempPattern.two) ) { // Pattern matches
        printMsg(messages[j], 23);
      }
    }
    
    tempPattern.one = tempPattern.two = 0; // Clear patterns
  }
}

void flashLed(int onTime, int offTime) {
 digitalWrite(gLedPin1, HIGH);
 digitalWrite(gLedPin2, HIGH);
 delay(onTime);
 digitalWrite(gLedPin1, LOW);
 digitalWrite(gLedPin2, LOW);
 delay(offTime);
}

void printLetter(uint8_t letterNo) {
 letterNo = letterNo - 1;
 
 int i=0; 
 while (true) {
   switch (alphabet[letterNo][i]) {
     case 0:
      flashLed(gMorseShort, gMorsePause);
      break;
     case 1:
      flashLed(gMorseLong, gMorsePause);
      break;
     case 2:
      i = 4;
      break;
   }
   
   if (i >= 3) {
    delay(gMorseNewLetter);
    break; 
   } else {
    i++; 
   }
 }
}

void printMsg(const uint8_t* array_start, int sz) {  
  for (int i=0; i < sz; i++) {
    uint8_t b;
    b = pgm_read_byte(array_start + i);

    Serial.println(b);
     
    if (b == 0)
      delay(gMorseNewWord);
    else
      printLetter(b);
  }
}

Getting rid of \ helps a little.

it compiles now however I commented out a line and couldn’t find it to look at it again. One issue was you had |= which I assume means doesn’t equal I changed it to !=, then in a for loop you had a “o” instead of “0”. If you find the line I commented out let me know I will see if I can repair that portion also.

/*
  Morse Code
  Uses an LED to transmit a morse code message, controlled by multiple
  buttons to display many different messages according to the pattern
  of the buttons being pressed.
 
  modified 21 Feb 2016
  by Gerrie Crafford
 */
 
#include <avr/pgmspace.h>
 
// --------------------------------------------------------------------------------------------- \\
// ------------------------------------- Setup and variables ----------------------------------- \\
// --------------------------------------------------------------------------------------------- \\
 
// Types
struct PATTERN {
  uint8_t one;
  uint8_t two;
};
 
// CONSTS
const byte MAX_BLANK_COUNT = 2;
 
// Pin Variables
int gLedPin1 = 13;
int gLedPin2 = 12;
 
int gBtn1Pin = 10;
int gBtn2Pin = 9;
 
// Morse Code timing variables
int gMorseBase = 750;
int gMorseShort = gMorseBase;
int gMorseLong = gMorseBase * 3;
int gMorsePause = gMorseBase;
int gMorseNewLetter = gMorseBase * 3;
int gMorseNewWord = gMorseBase * 7;
 
// State variables
int gBtn1State = LOW;
int gBtn2State = LOW;
 
int blankCount = 0;
 
// Misc & control variables
boolean loopAgain = false;
 
byte counter = 0, blankcount = 0;
PATTERN const tempPattern = { 0, 0 };
 
// --------------------------------------------------------------------------------------------- \\
// ------------------------------------- Alphabet in morse ------------------------------------- \\
// --------------------------------------------------------------------------------------------- \\
const byte alphabet[26][4] = {
  {0,1,-1,-1}, //A - 1
  {1,0,0,0}, //B - 2
  {1,0,1,0}, //C - 3
  {1,0,0,-1}, //D - 4
  {0,-1,-1,-1}, //E - 5
  {0,0,1,0}, //F - 6
  {1,1,0,-1}, //G - 7
  {0,0,0,0}, //H - 8
  {0,0,-1,-1}, //I - 9
  {0,1,1,1}, //J - 10
  {1,0,1,-1}, //K - 11
  {0,1,0,0}, //L - 12
  {1,1,-1,-1}, //M - 13
  {1,0,-1,-1}, //N - 14
  {1,1,1,-1}, //O - 15
  {0,1,1,1}, //P - 16
  {1,1,0,1}, //Q - 17
  {0,1,0,-1}, //R - 18
  {0,0,0,-1}, //S - 19
  {1,-1,-1,-1}, //T - 20
  {0,0,1,-1}, //U - 21
  {0,0,0,1}, //V - 22
  {0,1,1,-1}, //W - 23
  {1,0,0,1}, //X - 24
  {1,0,1,1}, //Y - 25
  {1,1,0,0} //Z - 26
};
 
// --------------------------------------------------------------------------------------------- \\
// ----------------------------------------- Messages ------------------------------------------ \\
// --------------------------------------------------------------------------------------------- \\
uint8_t const PROGMEM sent1[23] = {
  10,25,0,9,19,0,1,13,1,26,9,14,7,0,12,9,5,6,0,10,15,21,0
};
 
const uint8_t* messages[1]; // Stores addresses of first elemnt of message arrays
 
// --------------------------------------------------------------------------------------------- \\
// ------------------------------------------- Patterns ---------------------------------------- \\
// --------------------------------------------------------------------------------------------- \\
uint8_t const AMOUNT_PATTERNS = 1;
PATTERN const patterns[1] = {
  { 3, 3 } // {00000011} {00000011}
};
 
 
// the setup function runs once when you press reset or power the board
void setup() {
 Serial.begin(9600);
 
 // Init pins
 pinMode(gLedPin1, OUTPUT);
 pinMode(gLedPin2, OUTPUT);
 pinMode(gBtn1Pin, INPUT);
 pinMode(gBtn2Pin, INPUT);
 
 digitalWrite(gLedPin1, LOW);
 digitalWrite(gLedPin2, LOW);
 
 // Init message address array
 messages = { &sent1[0] };
}
 
// the loop function runs over and over again forever
void loop() {
  gBtn1State = digitalRead(gBtn1Pin);
  gBtn2State = digitalRead(gBtn2Pin);
 
  if ( (gBtn1State == HIGH) || (gBtn2State == HIGH) ) { // Begin pattern recognition process
    loopAgain = true;
    counter = 0;
  }
 
  while (loopAgain) {
    gBtn1State = digitalRead(gBtn1Pin);
    gBtn2State = digitalRead(gBtn2Pin);
 
    if (gBtn1State == HIGH) { // Set the bit in the pattern byte to 1 to show that the button was on
      tempPattern.one != 1 << counter;
      blankCount = 0;
    }
 
    if (gBtn2State == HIGH) {
      tempPattern.two != 1 << counter;
      blankCount = 0;
    }
 
    if ( (gBtn1State == LOW) && (gBtn2State == LOW) )
      blankCount++;
 
    if ( (counter >= 7) || (blankCount > MAX_BLANK_COUNT) ) { // Loop ends when counter too large or too many blanks
      loopAgain = false;
      counter = 0;
      blankCount = 0;
    }
 
    counter++;
    delay(1000);
  }
 
  if ( (tempPattern.one != 0) || (tempPattern.two != 0) ) { // Patterns have values, match them to stored patterns
    Serial.println(tempPattern.one);
    Serial.println(tempPattern.two);
 
    for (int j = 0; j < AMOUNT_PATTERNS; j++) {
      if ( (patterns[j].one == tempPattern.one) && (patterns[j].two == tempPattern.two) ) { // Pattern matches
        printMsg(messages[j], 23);
      }
    }
   
   // tempPattern.one = tempPattern.two = 0; // Clear patterns
  }
}
 
void flashLed(int onTime, int offTime) {
 digitalWrite(gLedPin1, HIGH);
 digitalWrite(gLedPin2, HIGH);
 delay(onTime);
 digitalWrite(gLedPin1, LOW);
 digitalWrite(gLedPin2, LOW);
 delay(offTime);
}
 
void printLetter(uint8_t letterNo) {
 letterNo = letterNo - 1;
 
 int i=0;
 while (true) {
   switch (alphabet[letterNo][i]) {
     case 0:
      flashLed(gMorseShort, gMorsePause);
      break;
     case 1:
      flashLed(gMorseLong, gMorsePause);
      break;
     case -1:
      i = 4;
      break;
   }
   
   if (i >= 3) {
    delay(gMorseNewLetter);
    break;
   } else {
    i++;
   }
 }
}
 
void printMsg(const uint8_t* array_start, int sz) {  
  for (int i=0; i < sz; i++) {
    uint8_t b;
    b = pgm_read_byte(array_start + i);
 
    Serial.println(b);
     
    if (b == 0)
      delay(gMorseNewWord);
    else
      printLetter(b);
  }
}[\code]

dlloyd:
Getting rid of \ helps a little.

Indeed, that was the error. Thank you very much!! I probably would never have seen that. Is there any reason the compiler didn’t ignore those trailing backslashes?

ZBay:
it compiles now however I commented out a line and couldn’t find it to look at it again. One issue was you had |= which I assume means doesn’t equal I changed it to !=, then in a for loop you had a “o” instead of “0”. If you find the line I commented out let me know I will see if I can repair that portion also.

Yes I saw that there were a few other minor errors after I was able to correct the weird error. Thanks for your effort!

ZBay:
One issue was you had |= which I assume means doesn’t equal I changed it to !=, …

I think this was a mistake, if you mean these bits:-

    if (gBtn1State == HIGH) { // Set the bit in the pattern byte to 1 to show that the button was on
      tempPattern.one != 1 << counter;
      blankCount = 0;
    }
 
    if (gBtn2State == HIGH) {
      tempPattern.two != 1 << counter;
      blankCount = 0;
    }

They look to me like they were supposed to be |= and not !=

The value 1 is being shifted left ‘counter’ bits, then ORed into ‘tempPattern.one’ and ‘tempPattern.two’ respectively.
To take the first example, it’s the same as writing:-

tempPattern.one = tempPattern.one | (1 << counter);

Edit: And I think this is the line you commented out:-

// tempPattern.one = tempPattern.two = 0; // Clear patterns