Go Down

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

bmachining

Hi, 

I am slowly learning and working on this code for a large servo with encoder.

I am getting the error expected initializer before if at the start of the void loop().

Thanks.

Code: [Select]


#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  20000     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant
#define onInterrupt            3
// 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)

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;


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

A vague paraphrase of the error is not very helpful. When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

gfvalvo

#2
Nov 08, 2018, 01:33 am Last Edit: Nov 08, 2018, 01:34 am by gfvalvo
It would be ever so helpful if you copied the entire error message (there's a button for doing that in the IDE) and posted it here -- In Code Tags

Dooh...@pert read my mind.

DKWatson

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

pert

You need to take a close look at what the Auto Format tool's indentation of your program is trying to tell you. Does the indentation match your expected program structure?

Another useful feature of the Arduino IDE is that when you place the cursor next to one bracket, it puts a box around the matching bracket. If the cursor is next to the closing bracket and the opening bracket is off the screen then it will show the opening bracket line in a tool tip after a short delay.

You seem to randomly sprinkle braces through your code without any understanding of what they do. That approach to programming will only result in frustration and wasted time.


DKWatson

Actually, a dog's breakfast would be a better description. I just tried to make the corrections and gave up.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

bmachining

A dogs breakfast only lasts 30 seconds!

I am wanting to control a large DC servo via a potentiometer.. It has a switch for home position so i can count turns from there.
 I have a code for driving it, and one for the encoder, both work as individual codes fine.

I may be going the wrong way about combining the code, but in trying!

Not sure if i need PID control, i want the servo to move proportionally with a potentimeter, plus a couple of other simple functions, eg move to certain position on closing of a switch.







DKWatson

You need to look at your code. You've got braces scattered all over everywhere. This is what it looks like formatted,
Code: [Select]


#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  20000     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant
#define onInterrupt            3
// 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)

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;


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);
                }
            }
        }
    }

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

bmachining

It is driving a multi turn screw, so Actually a linear actuator.  encoder is on screw.

DKWatson

It's doing nothing as it is. These are the 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'
C:\Users\Watson\Documents\Arduino\forum\forum.ino: In function 'void setup()':

forum:65:9: error: expected initializer before 'if'

         if (encoder0Position != previousPosition);

         ^

forum:8:32: error: expected unqualified-id before numeric constant

 #define onInterrupt            3

                                ^

C:\Users\Watson\Documents\Arduino\forum\forum.ino:78:12: note: in expansion of macro 'onInterrupt'

     { void onInterrupt()

            ^

forum:84:13: error: 'a' was not declared in this scope

         if (a = b )

             ^

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

     }

     ^

exit status 1
expected initializer before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
all because of brace mis-match. If you're not prepared to follow advice, don't ask the questions. It's not just me that's told you the same thing.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

bmachining

#10
Nov 08, 2018, 09:58 am Last Edit: Nov 08, 2018, 10:02 am by bmachining
It's doing nothing as it is. These are the 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'
C:\Users\Watson\Documents\Arduino\forum\forum.ino: In function 'void setup()':

forum:65:9: error: expected initializer before 'if'

         if (encoder0Position != previousPosition);

         ^

forum:8:32: error: expected unqualified-id before numeric constant

 #define onInterrupt            3

                                ^

C:\Users\Watson\Documents\Arduino\forum\forum.ino:78:12: note: in expansion of macro 'onInterrupt'

     { void onInterrupt()

            ^

forum:84:13: error: 'a' was not declared in this scope

         if (a = b )

             ^

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

     }

     ^

exit status 1
expected initializer before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
all because of brace mis-match. If you're not prepared to follow advice, don't ask the questions. It's not just me that's told you the same thing.
Bit harsh dont you think, i come asking why and get shot down for not knowing? Not everyone is an expert, none were born one either.  I stated at the start i am only a beginner....


Anyone know of a working PID code for a multi turn servo?

DKWatson

This has nothing to do with levels of experience/expertise. Every response gave you advice which you seem to have ignored. You keep coming back explaining what you want to do and then complaining when you don't feel you're being treated fairly. You think we have nothing better to do? One last time. Put your code back into an editor and look at it. I even cleaned it up for you a formatted it properly (you're welcome). The errors become blatantly obvious. Your code may work, but nobody will ever know until you do what needs to be done. If you're not prepared to make that effort, how can you expect anyone else to go out of their way to help you?
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

bmachining

Ok,   Would someone mind guiding me through the errors so i can work this out?


slipstick

O.k. Post the actual code you're using now (I think we're all hoping that it has changed since the first thing you posted). Try to verify it and post the complete error listing that you get. Then someone can talk you through that.

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.

Steve

MarkT

If you read the guidelines for posting you would have learnt about indenting the code properly and
so forth.  The sticky threads about how to post are there for very good reasons.

Until the basic bracketting is sorted out its hard to progress.  You need to know how to check and
correct indenting/bracketing, its absolutely fundamental to programming.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up