Go Down

Topic: error code expected initializer (Read 1 time) previous topic - next topic

bmachining

I hope the brackets are improved!

certainly less errors.

Code: [Select]




#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)




void setup()

{ Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch
  const byte interruptPin = 20;

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);

  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");

  //drive to home switch
  analogWrite(LPWM_Output, 100);

  if (digitalRead (36) == LOW)
 
{ Serial.println ("Switch closed.");
  delay (1000);


  analogWrite(RPWM_Output, 100);
}

void loop()
{

if (encoder0Position != previousPosition)


  Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;

}

// interrupt function needs to do as little as possible
void onInterrupt()

// read both inputs
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB);

if (a = b )
{
  // b is leading a (counter-clockwise)
  encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
encoder0Position = encoder0Position % CPR;

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable
{ int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
}
int sensorValue = analogRead(SENSOR_PIN);

// sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)



{
  // reverse rotation
  int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}




Arduino: 1.8.7 (Windows 8.1), TD: 1.44, Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\HP\Documents\Arduino\formatted\formatted.ino: In function 'void setup()':

formatted:42:22: error: 'onInterrupt' was not declared in this scope

   attachInterrupt(3, onInterrupt, RISING);

                      ^

formatted:61:1: error: a function-definition is not allowed here before '{' token

 {

 ^

formatted:79:1: error: expected initializer before 'int'

 int a = digitalRead(ENCODER0PINA);

 ^

formatted:82:5: error: 'a' was not declared in this scope

 if (a = b )

     ^

formatted:125:1: error: expected '}' at end of input

 }

 ^

exit status 1
'onInterrupt' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

pert

You've been told multiple times to use auto format, yet you still didn't do it.

bmachining


pert

After using auto format, did you look at the indentation and compare it to your expected program structure? Did you notice the obvious problems that indentation shows?

If not, then you might read again what slipstick told you:
Use Ctrl-T or Tools/AutFormat to format the code first. Then there are a few basics you can look for on your own. Braces and all other brackets must always be balanced...you need a start { and a finish } or it won't compile. Function definitions like void loop() must always start at the beginning of the line, no spaces. If they don't then your brackets are still in a mess.

AWOL

#19
Nov 09, 2018, 12:08 pm Last Edit: Nov 09, 2018, 12:10 pm by AWOL
Code: [Select]
if (a = b )
If b is non-zero, this expression will always be true. If b is zero, then the expression is false.
Either way, a ends up with whatever value b had.

Use == to compare.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

AWOL

Code: [Select]
  if (digitalRead (36) == LOW)
 
{ Serial.println ("Switch closed.");
  delay (1000);


  analogWrite(RPWM_Output, 100);
}

void loop()
{


I'm sorry, I refuse to believe this code has been auto-formatted.
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.

DKWatson

OP's code, properly formatted,
Code: [Select]
#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)

void setup()
{
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    pinMode(RPWM_Output, OUTPUT);
    pinMode(LPWM_Output, OUTPUT);
    pinMode(36, INPUT_PULLUP); //home switch
    const byte interruptPin = 20;

    // inputs
    pinMode(ENCODER0PINA, INPUT_PULLUP);
    pinMode(ENCODER0PINB, INPUT_PULLUP);

    // interrupts
    attachInterrupt(3, onInterrupt, RISING);
    // enable diagnostic output
    Serial.begin (9600);
    Serial.println("\n\n\n");
    Serial.println("Ready.");

    //drive to home switch
    analogWrite(LPWM_Output, 100);

    if (digitalRead (36) == LOW)
    {
        Serial.println ("Switch closed.");
        delay (1000);
        analogWrite(RPWM_Output, 100);
    }

    void loop()
    {
        if (encoder0Position != previousPosition)
        Serial.print(encoder0Position, DEC);
        Serial.print("\t");
        Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
        Serial.print("\t");
        Serial.println(interruptsReceived, DEC);
        previousPosition = encoder0Position;
    }

    // interrupt function needs to do as little as possible
    void onInterrupt()

    // read both inputs
    int a = digitalRead(ENCODER0PINA);
    int b = digitalRead(ENCODER0PINB);

    if (a = b )
    {
        // b is leading a (counter-clockwise)
        encoder0Position--;
        currentDirection = COUNTER_CLOCKWISE;
    }
    else
    {
        // a is leading b (clockwise)
        encoder0Position++;
        currentDirection = CLOCKWISE;
    }

    // track 0 to 20000
    encoder0Position = encoder0Position % CPR;

    // track the number of interrupts
    interruptsReceived++;
    //read the pushbutton value into a variable
    { int sensorVal = digitalRead(36);
        //print out the value of the pushbutton
        Serial.println(sensorVal);
    }
    int sensorValue = analogRead(SENSOR_PIN);

    // sensor value is in the range 0 to 1023
    // the lower half of it we use for reverse rotation; the upper half for forward rotation
    if (sensorValue < 512)
    {
        // reverse rotation
        int reversePWM = -(sensorValue - 511) / 2;
        analogWrite(LPWM_Output, 0);
        analogWrite(RPWM_Output, reversePWM);
    }
    else
    {
        // forward rotation
        int forwardPWM = (sensorValue - 512) / 2;
        analogWrite(LPWM_Output, forwardPWM);
        analogWrite(RPWM_Output, 0);
    }



Error messages,
Code: [Select]
Arduino: 1.8.6 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Category '' in library ArduinoUnit is not valid. Setting to 'Uncategorized'
Build options changed, rebuilding all
C:\Users\Watson\Documents\Arduino\forum\forum.ino: In function 'void setup()':

forum:36:24: error: 'onInterrupt' was not declared in this scope

     attachInterrupt(3, onInterrupt, RISING);

                        ^

forum:53:5: error: a function-definition is not allowed here before '{' token

     {

     ^

forum:67:5: error: expected initializer before 'int'

     int a = digitalRead(ENCODER0PINA);

     ^

forum:70:9: error: 'a' was not declared in this scope

     if (a = b )

         ^

forum:110:5: error: expected '}' at end of input

     }

     ^

exit status 1
'onInterrupt' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


Notice how the error messages are the same, even with properly formatted code. However, if you look at properly formatted code, the mis-matched braces are still clearly evident.

Just for interest, clean up the braces, as it has been recommended from the beginning, and this is the compiler response,
Code: [Select]
Sketch uses 4850 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 282 bytes (3%) of dynamic memory, leaving 7910 bytes for local variables. Maximum is 8192 bytes.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

UKHeliBob

I have used auto format.  
Auto Format applies a consistent format to the code you have written.  It does not fix problems, but can help show what might be causing them.

As an example here is a portion of your code Auto Formatted
Code: [Select]

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch
  const byte interruptPin = 20;
  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);
  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");
  //drive to home switch
  analogWrite(LPWM_Output, 100);
  if (digitalRead (36) == LOW)
  {
    Serial.println ("Switch closed.");
    delay (1000);
    analogWrite(RPWM_Output, 100);
  }
  
  void loop()
  {
etc, etc


Functions should end with a closing brace on the left margin.  Does your code do that when Auto Formatted ?  Where does the setup() function end ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

bmachining

This is auto formatted, and clicked on copy to forum.




Code: [Select]




#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)




void setup()

{ Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch
  const byte interruptPin = 20;

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);

  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");

  //drive to home switch
  analogWrite(LPWM_Output, 100);

  if (digitalRead (36) == LOW)

    Serial.println ("Switch closed.");
  delay (1000);


  analogWrite(RPWM_Output, 100);
}

void loop()
{

  if (encoder0Position != previousPosition)


    Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;

}

// interrupt function needs to do as little as possible
void onInterrupt()

// read both inputs
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB);

if (a == b )
{
  // b is leading a (counter-clockwise)
  encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
encoder0Position = encoder0Position % CPR;

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable
{ int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
}
int sensorValue = analogRead(SENSOR_PIN);

// sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)



{
  // reverse rotation
  int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

pert

I guess you didn't read the last time, so I'll repeat myself:

After using auto format, did you look at the indentation and compare it to your expected program structure? Did you notice the obvious problems that indentation shows?

If not, then you might read again what slipstick told you:
Quote
Use Ctrl-T or Tools/AutFormat to format the code first. Then there are a few basics you can look for on your own. Braces and all other brackets must always be balanced...you need a start { and a finish } or it won't compile. Function definitions like void loop() must always start at the beginning of the line, no spaces. If they don't then your brackets are still in a mess.
Even after the auto format, the formatting of your code is very messy. It's not just about aesthetics. Messy code is difficult to understand and clearly you're having a tremendous difficulty with understanding anything. It's been a couple of days now. Don't fool yourself into thinking you're saving time by writing code that looks like a pile of garbage. This sloppy approach to writing code is only going to cause you a lot of frustration and wasted time.

Code: [Select]
{ Serial.begin(9600);
Don't ever put code on the same line following a brace. This should look like:
Code: [Select]
{
  Serial.begin(9600);


Code: [Select]
 if (digitalRead (36) == LOW)

    Serial.println ("Switch closed.");

Always use braces, even if they only contain one statement. It's worth that tiny bit of extra effort to make your code more self documenting and resistant to bugs.

Code: [Select]
if (sensorValue < 512)



{

Don't leave random blank lines in your code. It's fine to use them intentionally to break your code into logical sections but there is absolutely no valid reason for the three blank lines there.

DKWatson

I think it's evident that OP is not interested in following advice, rather argue in favour of not. I've wasted enough of my time on this one. Even as I got his code working, he'll only see that now if he does it himself.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

bmachining

Im following the advice DK.  I have changed brackets to be balanced etc but there must be some unwritten rules with them.
 

pert

I have changed brackets to be balanced etc
Post the updated code.

I have changed brackets to be balanced etc but there must be some unwritten rules with them.
Certainly not. C++ is extremely well documented and this is one of the absolute basic fundamentals. It's quite simple: Any time you have a { you must also have a }. Every function starts with a { and ends with a }. Don't try to define a function inside another function.

bmachining

pert,  may be simple for those with a bit of experience.

Knowing what does need the { } and what doesnt could be part of it.

Code: [Select]

#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch

  const byte interruptPin = 20;

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);

  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");

  //drive to home switch
  analogWrite(LPWM_Output, 100);


  if (digitalRead (36) == LOW);

  Serial.println ("Switch closed.");
  delay (1000);
  analogWrite(RPWM_Output, 100);
}

void loop()
{
  if (encoder0Position != previousPosition)

    Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;
}


// interrupt function needs to do as little as possible
void onInterrupt()
{
  // read both inputs
  int a = digitalRead(ENCODER0PINA);
  int b = digitalRead(ENCODER0PINB);

  if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
{
  encoder0Position = encoder0Position % CPR;
}

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable

{
  int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);


  int sensorValue = analogRead(SENSOR_PIN);

  // sensor value is in the range 0 to 1023
  // the lower half of it we use for reverse rotation; the upper half for forward rotation
  if (sensorValue < 512)

    // reverse rotation
    int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}



pert

Here's your onInterrupt function:
Code: [Select]
void onInterrupt()
{
  // read both inputs
  int a = digitalRead(ENCODER0PINA);
  int b = digitalRead(ENCODER0PINB);

  if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}

You can see it starts with a { and ends with the matching }.

It's followed by all this code:
Code: [Select]
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
{
  encoder0Position = encoder0Position % CPR;
}

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable

{
  int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);


  int sensorValue = analogRead(SENSOR_PIN);

  // sensor value is in the range 0 to 1023
  // the lower half of it we use for reverse rotation; the upper half for forward rotation
  if (sensorValue < 512)

    // reverse rotation
    int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

Even without looking at braces, you can see from the indentation that code is outside of any function. You can't have code outside of a function. Clearly, that code was intended to be part of the onInterrupt function.

So what happened? You forgot to add an opening brace on this line:
Code: [Select]
 if (a == b )
Again, the indentation is trying to tell you something if you'll only look:
Code: [Select]
 if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;

Notice how this line is indented past the if:
Code: [Select]
   encoder0Position--;
but the next line is not:
Code: [Select]
 currentDirection = COUNTER_CLOCKWISE;
That is because when you don't use braces after an if, the if only affects the first statement following. But that's not what you intended. You intended for the if to control all the code up to the else.

I gave you some advice earlier but you ignored it:

Code: [Select]
 if (digitalRead (36) == LOW)

    Serial.println ("Switch closed.");

Always use braces, even if they only contain one statement. It's worth that tiny bit of extra effort to make your code more self documenting and resistant to bugs.
Yes, you can get away without the braces if there is only one statement, but what if you decide to add another line later? It's just not worth it. Always use the braces. If you had done so in this case it would have saved a lot of confusion and wasted time.

Things become even a more confusing because you added that unnecessary blank line, which I already told you not to do. Every line, every character, in your program should have a purpose. The compiler has no mercy. It's not going to say "yo, it's all good, I know what you meant". This is why you need to take an extremely precise approach to writing code.

Go Up