Subroutine help.

Hi guys, I’m stuck and could use some help. Here is my Error message when compiling and the code is attached with an explanation on each line. I have a little bit of exp with assembler besides that not much else.

Arduino: 1.6.6 (Windows 10), Board: “Arduino/Genuino Uno”

Software_counter_interface:8: error: expected constructor, destructor, or type conversion before ‘(’ token

getGraph(desiredPoint, encoderResolution, count) // A subroutine that allows me to pass in variables for my formula * Y = A(1-COS(C)) *. When the subroutine is called Y should be calculated.

^

Software_counter_interface:8: error: expected constructor, destructor, or type conversion before ‘(’ token

getGraph(desiredPoint, encoderResolution, count) // A subroutine that allows me to pass in variables for my formula * Y = A(1-COS(C)) *. When the subroutine is called Y should be calculated.

^

C:\Users\Michael Orsini\Documents\Arduino\Software_counter_interface\Software_counter_interface.ino: In function ‘void countUp()’:

Software_counter_interface:25: error: ‘channelaState’ was not declared in this scope

channelaState = digitalRead(2); // Read pin 2 and store it into channelaState

^

Software_counter_interface:26: error: ‘channelbState’ was not declared in this scope

channelbState = digitalRead(3); // Read pin 3 and store it into channelbState

^

C:\Users\Michael Orsini\Documents\Arduino\Software_counter_interface\Software_counter_interface.ino: In function ‘void countDown()’:

Software_counter_interface:44: error: ‘channelaState’ was not declared in this scope

channelaState = digitalRead(2); // Read pin 2 and store it into channelaState

^

Software_counter_interface:45: error: ‘channelbState’ was not declared in this scope

channelbState = digitalRead(3); // Read pin 3 and store it into channelbState

^

exit status 1
expected constructor, destructor, or type conversion before ‘(’ token

Software_counter_interface.ino (5.16 KB)

// Formula to use  * Y = A(1-COS(C)) *                                              Y = is the specfic point I want to find , A= for amplitude, C= 0-359

int graphPoints = 360;                                                             // Points used in the wave (C?)
int encoderResolution = 120;                                                       // Change depennding on how many PPR for the encoder ( Amplitude?)
int desiredPoint = 0;                                                              // Variable to hold Y
int getGraph = 0;                                                                  // Variable for getGraph
 
 getGraph(desiredPoint, encoderResolution, count)                                  // A subroutine that allows me to pass in variables for my formula * Y = A(1-COS(C)) *. When the subroutine is called Y should be calculated.
{
  desiredPoint = encoderResolution(1 - cos(count));                                // desiredPoint (Y) = encoderResolution(A)*(1-cos(graphPointsC))
  return desiredPoint;                                                             // allow desired point to leave the subrotuine and be stored in desiredPoint for use in other subroutines
}




float count = 0;                                                                   // Memory location to store ongoing encoder count
int channelA = 2;                                                                  // Pin connected to channel A of the encoder
int channelAstate = 0;                                                             // Variable to store channel A's state
int channelB = 3;                                                                  // Pin connected to channel B of the encoder
int channelBstate = 0;                                                             // Variable to store channel B's state

void countUp()                                                                    // Subroutine that stores encoder pules, counting forwards or up
{
  channelaState = digitalRead(2);                                                 // Read pin 2 and store it into channelaState
  channelbState = digitalRead(3);                                                 // Read pin 3 and store it into channelbState

  if ( channelbState > channelaState )                                            // Is A leading B?
  {
    count++;                                                                      // Increment the memory location
    if ( count == 240 )                                                           // Is memory location  240? since we are working with a 240 PPR encoder, this is one completed revolution.
    {
      count = 0;                                                                  // Reset the memory location
    }
    Serial.println(count);                                                        // Send the count to the screen for diag purposes.
    PORTB = count;                                                                // Send the encoder count to PORTB ( no longer nessecary ) the arduino PWM frequency will be changed to 20khz


  }
}

void countDown()                                                                 // A subroutine that stores encoder pulses, counting backwards or down
{
  channelaState = digitalRead(2);                                                // Read pin 2 and store it into channelaState
  channelbState = digitalRead(3);                                                // Read pin 3 and store it into channelbState

  if ( channelaState > channelbState )                                           // Is B leading A?
  {
    count--;                                                                     // Decrement the memory location
    if ( count < 0 )                                                             // Is the memoroy location 0? We will never rollbackwards. Set a limit to 0;
    {
      count = 0;                                                                 // Reset the location to 0;
    }
    Serial.println(count);                                                       // Send the count to the screen for diag purposes.
    PORTB = count;                                                               // Send the encoder count to PORTB ( no longer nessecary )
  }
}




void setup()
{
  pinMode(2, INPUT);                                                            // Pin 2 is set as an input.
  pinMode(3, INPUT);                                                            // Pin 3 is set as an input.

  Serial.begin(9600);                                                           // Begin serial communication with Arduino interface.
  Serial.println("TEST");                                                       // Print TEST to the screen

  attachInterrupt(digitalPinToInterrupt(2), countUp, CHANGE);                   // Enable the intterupt on pin 2. When triggered by a change of state, it will call countUp subroutine
  attachInterrupt(digitalPinToInterrupt(3), countDown, CHANGE);                 // Enable the intterupt on pin 3. When triggered by a change of state, it will call countDown subroutine

  DDRB = B11111111;                                                             // Sets PORTB as all outputs, pin 7-13.
}

void loop()

{

}
int graphPoints = 360;                                                             // Points used in the wave (C?)
int encoderResolution = 120;                                                       // Change depennding on how many PPR for the encoder ( Amplitude?)
int desiredPoint = 0;                                                              // Variable to hold Y
int getGraph = 0;                                                                  // Variable for getGraph

getGraph(desiredPoint, encoderResolution, count)                                  // A subroutine that allows me to pass in variables for my formula * Y = A(1-COS(C)) *. When the subroutine is called Y should be calculated.
{
  desiredPoint = encoderResolution(1 - cos(count));                                // desiredPoint (Y) = encoderResolution(A)*(1-cos(graphPointsC))
  return desiredPoint;                                                             // allow desired point to leave the subrotuine and be stored in desiredPoint for use in other subroutines
}

getGraph is a int variable, why you try to use it like a function? :o

That's what I want to do. I thought I needed to create a variable for EVERYTHING?

I've removed it as int getGraph. It now reads...

getGraph(those variables)

http://www.cplusplus.com/doc/tutorial/functions/

Read that and maybe it will help you understand how functions work in C++. Google "C++ functions" for a bunch more.

Undert9281: I've removed it as int getGraph. It now reads...

getGraph(those variables)

The three variables you have in the parenthesis are global variables in your program. You don't need to pass them as anything. But if you are going to pass variables into a function you have to give the types and it is always a good idea to use names for the parameters that aren't used elsewhere in your program.

You also have to supply the return type of a function when you define it and you're missing that.

Read the link I gave you for examples of what I'm talking about.

The code below takes the other posters’ comments into account and the code compiles, but I have no idea if it does what you want.

First, there are no subroutines in C. They are called functions. Every function definition must begin with a function type specifier, which is the data type you want the function to return. Your getGraph() function attempts to return desiredPoint, which you’ve defined as an int, so the type specifier int must appear before the function name. If the function is not designed to return something, use void as the type specifier, as setup() and loop() do.

Second, C is case sensitive, so channelAstate and channelaState do not reference the same variable. You make this mistake in numerous places.

Third, you pass cos() an int variable named count when it expects a float data type. The compiler is smart enough to fix such errors, but you should use a cast before count.

Finally, you have defined the arguments to getGraph() outside a function definition, which means they have global scope. Therefore, you don’t need to pass them into the function. However, global scope means anyone can access these variables from any point in the source file, which makes debugging more difficult. You would be better off to give them local scope by defining them within the function that uses them. Global scope prostitutes the variables because it allows anyone to have their way with them.

Spend some time with an free online C tutorial or invest in a good beginning C book. It will make your life a little easier.

// Formula to use  * Y = A(1-COS(C)) *                                              Y = is the specfic point I want to find , A= for amplitude, C= 0-359

int graphPoints = 360;                                                             // Points used in the wave (C?)
int encoderResolution = 120;                                                       // Change depennding on how many PPR for the encoder ( Amplitude?)
int desiredPoint = 0;                                                              // Variable to hold Y
// int getGraph = 0;          illegal                                                        // Variable for getGraph
 
// getGraph(desiredPoint, encoderResolution, count)                                  // A subroutine that allows me to pass in variables for my formula * Y = A(1-COS(C)) *. When the subroutine is called Y should be calculated.
int getGraph(int desiredPoint, int encoderResolution, float count) 
 {
//  desiredPoint = encoderResolution(1 - cos(count));                                // desiredPoint (Y) = encoderResolution(A)*(1-cos(graphPointsC))
  desiredPoint = encoderResolution + (1 - cos( (float) count));
  return desiredPoint;                                                             // allow desired point to leave the subrotuine and be stored in desiredPoint for use in other subroutines
}




float count = 0;                                                                   // Memory location to store ongoing encoder count
int channelA = 2;                                                                  // Pin connected to channel A of the encoder
int channelAstate = 0;                                                             // Variable to store channel A's state
int channelB = 3;                                                                  // Pin connected to channel B of the encoder
int channelBstate = 0;                                                             // Variable to store channel B's state

void countUp()                                                                    // Subroutine that stores encoder pules, counting forwards or up
{
 // channelaState = digitalRead(2);                                                 // Read pin 2 and store it into channelaState
 // channelbState = digitalRead(3);                                                 // Read pin 3 and store it into channelbState
  channelAstate = digitalRead(2);                                                 // Read pin 2 and store it into channelaState
  channelBstate = digitalRead(3);                                                 // Read pin 3 and store it into channelbState

//if ( channelaState > channelbState )                                            // Is A leading B?
  if ( channelBstate > channelAstate )                                            // Is A leading B?
  {
    count++;                                                                      // Increment the memory location
    if ( count == 240 )                                                           // Is memory location  240? since we are working with a 240 PPR encoder, this is one completed revolution.
    {
      count = 0;                                                                  // Reset the memory location
    }
    Serial.println(count);                                                        // Send the count to the screen for diag purposes.
    PORTB = count;                                                                // Send the encoder count to PORTB ( no longer nessecary ) the arduino PWM frequency will be changed to 20khz


  }
}

void countDown()                                                                 // A subroutine that stores encoder pulses, counting backwards or down
{
 // channelaState = digitalRead(2);                                                // Read pin 2 and store it into channelaState
//  channelbState = digitalRead(3);                                                // Read pin 3 and store it into channelbState
  channelAstate = digitalRead(2);                                                 // Read pin 2 and store it into channelaState
  channelBstate = digitalRead(3);                                                 // Read pin 3 and store it into channelbState

//  if ( channelaState > channelbState )                                           // Is B leading A?
  if ( channelBstate > channelAstate )                                            // Is A leading B?
  {
    count--;                                                                     // Decrement the memory location
    if ( count < 0 )                                                             // Is the memoroy location 0? We will never rollbackwards. Set a limit to 0;
    {
      count = 0;                                                                 // Reset the location to 0;
    }
    Serial.println(count);                                                       // Send the count to the screen for diag purposes.
    PORTB = count;                                                               // Send the encoder count to PORTB ( no longer nessecary )
  }
}




void setup()
{
  pinMode(2, INPUT);                                                            // Pin 2 is set as an input.
  pinMode(3, INPUT);                                                            // Pin 3 is set as an input.

  Serial.begin(9600);                                                           // Begin serial communication with Arduino interface.
  Serial.println("TEST");                                                       // Print TEST to the screen

  attachInterrupt(digitalPinToInterrupt(2), countUp, CHANGE);                   // Enable the intterupt on pin 2. When triggered by a change of state, it will call countUp subroutine
  attachInterrupt(digitalPinToInterrupt(3), countDown, CHANGE);                 // Enable the intterupt on pin 3. When triggered by a change of state, it will call countDown subroutine

  DDRB = B11111111;                                                             // Sets PORTB as all outputs, pin 7-13.
}

void loop()
{

}