Pages: [1]   Go Down
Author Topic: an error "expected unqualified-id before string constant" when compiling  (Read 4452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my project is:
a force is applied on a peizo sensor, a voltage will be produced. Now my code can judge what's a real hit (quick touch and then release). When a hit is applied on the sensor, the Pin 13 will produce a high level. at the same time I want to call the SineWave generator function. Actually the SineWave generator code is from Richard Cappels. It can work alone. But once it is put in my code, an error "expected unqualified-id before string constant" will happen.
my Code is:
Code:

 #include <avr/interrupt.h>
 #include <stdlib.h>

int led =13;
const int N=100;
float voltageValue[N];
float voltage[N];
int PressStatus;//Pressing flag;

//define Sinewave
 char sinetable [32];
 int  j ;
 
 void ioinit (void)
 {
   //Initialize output ports
   PORTD = B11111111;
   DDRD  = B11111111;

 }

 void timer_setup(){
   TCCR2A = 0;
   TCNT2=455;    //455 outputs 1.007khz
   TCCR2B = B00000010;
   //Timer2 Overflow Interrupt Enable
   TIMSK2 = 1<<TOIE2;
 }
 

void setup(){
   ioinit();
   arraysetup();
   cli();
   timer_setup();
   j = 0;
   sei();

  //initialize serial communication at 4800bit per second;
Serial.begin(4800);
pinMode (led,OUTPUT);
}



void arraysetup(void){
   sinetable[0]=127;  // Put 32 step 8 bit sine table into array.
   sinetable[1]=152;
   sinetable[2]=176;
   sinetable[3]=198;
   sinetable[4]=217;
   sinetable[5]=233;
   sinetable[6]=245;
   sinetable[7]=252;
   sinetable[8]=254;
   sinetable[9]=252;
   sinetable[10]=245;
   sinetable[11]=233;
   sinetable[12]=217;
   sinetable[13]=198;
   sinetable[14]=176;
   sinetable[15]=152;
   sinetable[16]=128;
   sinetable[17]=103;
   sinetable[18]=79;
   sinetable[19]=57;
   sinetable[20]=38;
   sinetable[21]=22;
   sinetable[22]=10;
   sinetable[23]=3;
   sinetable[24]=0;
   sinetable[25]=3;
   sinetable[26]=10;
   sinetable[27]=22;
   sinetable[28]=38;
   sinetable[29]=57;
   sinetable[30]=79;
   sinetable[31]=103;
 }

void loop(){
 
 int i;
 for(i=0;i<N;i++){
  //read the input on analog pin A0;
voltageValue[i] = analogRead(A0);
voltage[i] = voltageValue[i] * (5.0 / 1023.0);

if (i>0&&voltage[i]-voltage[i-1]>=0.1){
// Serial.println("voltage = ");
PressStatus=1;
digitalWrite(led,HIGH);
//call SineWave generator function
ISR(TIMER2_OVF_vect) {

   PORTD=(sinetable[j++]);
   TCNT2=455;
   if(j==32){
     j=0;
   }

 }
}
else {
PressStatus=0;
digitalWrite(led,LOW);
}

Serial.println(voltage[i]);
//Serial.println(Press);
}

}


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Consistent indentation will help you spot your problem.
Serial i/o in an interrupt service routine is never a good idea.
The compiler will initialise your sine table for you when you declare it.
« Last Edit: October 04, 2012, 03:25:32 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL,
What shall I do next?
Thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check your indentation.
This flavour of C++ doesn't allow function declarations inside other functions.

Here someone else suggested you concentrate on indentation.
« Last Edit: October 04, 2012, 03:37:39 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am so sorry, I really don't know what is the indentation. Pls help me out or give me more hints.
Thanks a lot.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Indentation is the visual structure of the code.
The compiler ignores it completely, but it helps humans follow the structure.
There are many different styles, but consistency is key.
If you find yourself typing a {, then type the matching } with exactly the same number of spaces in front of it.
Nested blocks have successively more spaces in front of them.
I'm pretty sure there's a Wikipedia page on the different styles.

I'm posting from my phone, so I can't easily post examples.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not posting from a phone so:

http://en.wikipedia.org/wiki/Indent_style

The Arduino IDE has an auto format feature (ctrl-t). Learn it, love it.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The Arduino IDE has an auto format feature (ctrl-t).
Embrace and love it, but it won't work if the code is badly formed.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL,
I have Checked my indentation. the error is still reported. the function "ISR(TIMER2_OVF_vect)" can not been called?
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 74
Posts: 2226
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Check your indentation.
This flavour of C++ doesn't allow function declarations inside other functions.

You aren't supposed to check your indentation, more re-do it so you can read your own code easier.

Maybe you are unsure because they are macros, but ISR(){ //... } is a function definition and must go at file scope like loop(), rather than in loop() itself ( nested ).
Code:
//Should not be declared inside a function
ISR(TIMER2_OVF_vect) {

   PORTD=(sinetable[j++]);
   TCNT2=455;
   if(j==32){
     j=0;
   }
 }
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi pYro_65,
Even I declare the function outside the loop, the error still happen.
my code:
Code:
#include <avr/interrupt.h>
#include <stdlib.h>

int led =13;
const int N=100;
float voltageValue[N];
float voltage[N];
int PressStatus;//Pressing flag;

//define Sinewave
char sinetable [32];
int  j ;

void ioinit (void)
{
  //Initialize output ports
  PORTD = B11111111;
  DDRD  = B11111111;

}

void timer_setup(){
  TCCR2A = 0;
  TCNT2=455;    //455 outputs 1.007khz
  TCCR2B = B00000010;
  //Timer2 Overflow Interrupt Enable
  TIMSK2 = 1<<TOIE2;
}


void setup(){
  ioinit();
  arraysetup();
  cli();
  timer_setup();
  j = 0;
  sei();

  //initialize serial communication at 4800bit per second;
  Serial.begin(4800);
  pinMode (led,OUTPUT);
}



void arraysetup(void){
  sinetable[0]=127;  // Put 32 step 8 bit sine table into array.
  sinetable[1]=152;
  sinetable[2]=176;
  sinetable[3]=198;
  sinetable[4]=217;
  sinetable[5]=233;
  sinetable[6]=245;
  sinetable[7]=252;
  sinetable[8]=254;
  sinetable[9]=252;
  sinetable[10]=245;
  sinetable[11]=233;
  sinetable[12]=217;
  sinetable[13]=198;
  sinetable[14]=176;
  sinetable[15]=152;
  sinetable[16]=128;
  sinetable[17]=103;
  sinetable[18]=79;
  sinetable[19]=57;
  sinetable[20]=38;
  sinetable[21]=22;
  sinetable[22]=10;
  sinetable[23]=3;
  sinetable[24]=0;
  sinetable[25]=3;
  sinetable[26]=10;
  sinetable[27]=22;
  sinetable[28]=38;
  sinetable[29]=57;
  sinetable[30]=79;
  sinetable[31]=103;
}

void loop(){

  int i;
  for(i=0;i<N;i++){
    //read the input on analog pin A0;
    voltageValue[i] = analogRead(A0);
    voltage[i] = voltageValue[i] * (5.0 / 1023.0);

    if (i>0&&voltage[i]-voltage[i-1]>=0.1){
      // Serial.println("voltage = ");
      PressStatus=1;
      digitalWrite(led,HIGH);

    }
    else {
      PressStatus=0;
      digitalWrite(led,LOW);
    }

  [b]  //call SineWave generator function[/b]
    ISR(TIMER2_OVF_vect) {
      if (PressStatus==1){
        PORTD=(sinetable[j++]);
        TCNT2=455;
        if(j==32){
          j=0;
        }
      }
    }
    Serial.println(voltage[i]);
    //Serial.println(Press);
  }


}



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Even I declare the function outside the loop, the error still happen.
my code:
No, completely outside of the function "loop()"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL,
Thank you so much!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int N=100;
float voltageValue[N];
float voltage[N];
That's 802 bytes of memory gone, right there. Do you really need to store 200 floats? The "voltage" read is an int. Why not, if you really need floats, store the ints read, and produce the floats as needed?

The voltageValue array should definitely be int, since that is what analogRead() returns.

Frankly, I can't see that you need to save more than two readings, since it is only the current and previous readings that matter.

Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2930
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the subject of Indentation and CRTL T (autoformat) I Love that tool for these two messages ""autoformat???: too many right curly braces or too many left curly braces.
Much of my education here came from CutNpaste... because I was uncomfortable writing sketches I would find one close and then find or write the necessary glue code to make it work. Very frequently before I learned to count the braces properly and place them in the correct order this tool saved my butt on a daily (almost) basis. There is another nice feature in the IDE and that is finding matching curly braces. Place the cursor on the open side of any curly brace and the IDE will draw a box around the matching curly brace... No box is an error and very easy to fix. I've noticed that I don't need to use it as often as I once did But that is because I spend two hours a day reading Prata's C++ Primer 5th ed. I have the 6th ed but it refers to the C11 ISO definition and the IDE more or less conforms to the C99 ISO definition. I used to say that my total knowledge of C & C++ could be written legibly on the head of a pin, now I need a small finishing nail..

Bob
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Pages: [1]   Go Up
Jump to: