this code does not compile? - unqualified id before ")" token

Hello great and powerful advisors,

I am now on project 9 of my SIK guide. The point of the example is to change the position of a servo using a flex sensor. Here is the code:

/*
SparkFun Inventor's Kit
Example sketch 09

FLEX SENSOR

  Use the "flex sensor" to change the position of a servo
  
  In the previous sketch, we learned how to command a servo to
  mode to different positions. In this sketch, we'll introduce
  a new sensor, and use it to control the servo.
  
  A flex sensor is a plastic strip with a conductive coating.
  When the strip is straight, the coating will be a certain
  resistance. When the strip is bent, the particles in the coating
  get further apart, increasing the resistance. You can use this
  sensor to sense finger movement in gloves, door hinges, stuffed
  animals, etc. See http://www.sparkfun.com/tutorials/270 for
  more information.
  
Hardware connections:

  Flex sensor:

    The flex sensor is the plastic strip with black stripes.
    It senses bending away from the striped side.
    
    The flex sensor has two pins, and since it's a resistor,
    the pins are interchangable.
    
    Connect one of the pins to ANALOG IN pin 0 on the Arduino.
    Connect the same pin, through a 10K Ohm resistor (brown
    black orange) to GND.
    Connect the other pin to 5V.

  Servo:
  
    The servo has a cable attached to it with three wires.
    Because the cable ends in a socket, you can use jumper wires
    to connect between the Arduino and the servo. Just plug the
    jumper wires directly into the socket.
    
    Connect the RED wire (power) to 5 Volts (5V)
    Connect the WHITE wire (signal) to digital pin 9
    Connect the BLACK wire (ground) to ground (GND)
  
    Note that servos can use a lot of power, which can cause your
    Arduino to reset or behave erratically. If you're using large
    servos or many of them, it's best to provide them with their
    own separate 5V supply. See this Arduino Forum thread for info:
    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1239464763

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Include the servo library to add servo-control functions:

#include <Servo.h> 

// Create a servo "object", called servo1. Each servo object
// controls one servo (you can have a maximum of 12):

Servo servo1;

// Define the analog input pin to measure flex sensor position:

const int flexpin = 0; 


void setup() 
{ 
  // Use the serial monitor window to help debug our sketch:
   
  Serial.begin(9600);
  
  // Enable control of a servo on pin 9:

  servo1.attach(9);
} 


void loop() 
{ 
  int flexposition;    // Input value from the analog pin.
  int servoposition;   // Output value to the servo.

  // Read the position of the flex sensor (0 to 1023):
  
  flexposition = analogRead(flexpin);

  // Because the voltage divider circuit only returns a portion
  // of the 0-1023 range of analogRead(), we'll map() that range
  // to the servo's range of 0 to 180 degrees. The flex sensors
  // we use are usually in the 600-900 range:
  
  servoposition = map(flexposition, 600, 900, 0, 180);
  servoposition = constrain(servoposition, 0, 180);

  // Now we'll command the servo to move to that position:

  servo1.write(servoposition);

  // Because every flex sensor has a slightly different resistance,
  // the 600-900 range may not exactly cover the flex sensor's
  // output. To help tune our program, we'll use the serial port to
  // print out our values to the serial monitor window:
  
  Serial.print("sensor: ");
  Serial.print(flexposition);
  Serial.print("  servo: ");
  Serial.println(servoposition);
  
  // Note that all of the above lines are "print" except for the
  // last line which is "println". This puts everything on the
  // same line, then sends a final carriage return to move to
  // the next line.

  // After you upload the sketch, turn on the serial monitor
  // (the magnifying-glass icon to the right of the icon bar).
  // You'll be able to see the sensor values. Bend the flex sensor
  // and note its minimum and maximum values. If you replace the
  // 600 and 900 in the map() function above, you'll exactly match
  // the flex sensor's range with the servo's range.
  
  delay(20);  // wait 20ms between servo updates
}

So it works ok. I tweaked the numbers based on the serial window output.

But then I remembered example 6 in the SIK guide. It automatically set the brightness of an LED based on the reading from a photo sensitive resistor. Original code is here (all the comments have been accidentally removed):

const int sensorPin = 0;
const int ledPin = 9;
int lightLevel, high = 0, low = 1023;
void setup()
{
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  lightLevel = analogRead(sensorPin);
  manualTune();  // manually change the range from light to dark
  //autoTune();  // have the Arduino do the work for us!
  analogWrite(ledPin, lightLevel);
}
void manualTune()
{
  lightLevel = map(lightLevel, 0, 1023, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
} 
void autoTune()
{
  if (lightLevel < low)
  {
    low = lightLevel;
  }
  if (lightLevel > high)
  {
    high = lightLevel;
  }
  lightLevel = map(lightLevel, low+30, high-30, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
}

Soooo anyway. Lightbulb moment, " I’m sure I could do something similar for experiment 9". Instead of tweaking the numbers manually, I would try to write a function to do it. Here is my attempt:

#include <Servo.h> 
Servo servo1;
const int flexpin = 0;
int flexposition, servoposition, straight = 600, bent = 1000;

void setup()
{ 
  Serial.begin(9600);
  servo1.attach(9);
} 

void loop() 
{ 
  flexposition = analogRead(flexpin);
  auto();
  //servoposition = map(flexposition, 750, 900, 0, 180);
  //servoposition = constrain(servoposition, 0, 180);
  servo1.write(servoposition);
  Serial.print("sensor: ");
  Serial.print(flexposition);
  Serial.print("  servo: ");
  Serial.println(servoposition);
  delay(20);  // wait 20ms between servo updates
} 
void auto()
{
  if (flexposition < straight)
  {
    straight = flexposition;
  }
  if (flexposition > bent)
  {
    bent = flexposition;
  }
  servoposition = map(flexposition, straight ,bent ,0 ,180);
  servoposition = constrain (servoposition, 0, 180);
}

And the glorious Ctrl+R leads me to the dismal cant compile error stated in the subject!! :~ :~ Please tell me what silly mistake Im making? I’ve moved all the variables in the global parameters so that “loop” and “auto” can see them? I just need someone to point me in the right direction.

Please Wise Ones. Guide me…

The compiler's error message usually includes the line number of the offending statement. Examine that line and the line before it.

Pete

auto is a keyword. Change the name of your function.

Looks like not much different from later code & earlier code is this:

int flexposition, servoposition, straight = 600, bent = 1000;

Try declaring those on separate lines. Otherwise, hard to say without seeing the compiler error and the line it highlights.

el_supremo: The compiler's error message usually includes the line number of the offending statement. Examine that line and the line before it.

Pete

Oki doki Pete. I copied and pasted the output from the little window below the code here:

" Circuit_09_Test:5: error: expected unqualified-id before ')' token Circuit_09_Test.ino: In function 'void loop()': Circuit_09_Test:15: error: expected primary-expression before 'auto' Circuit_09_Test:15: error: expected `;' before 'auto' Circuit_09_Test.ino: At global scope: Circuit_09_Test:25: error: expected unqualified-id before ')' token

"

Do those numbers 5,15,25 etc. relate to the line numbers..?

"auto", yeah that might have stuck out with seeing the IDE screen.

Arrch:
auto is a keyword. Change the name of your function.

Oh ok. Thats an embarrising Noob mistake. Will change and keep you informed. Thanks (and apologies) for this. We never ever covered this in the previous examples. Thanks again.

CrossRoads:
Looks like not much different from later code & earlier code is this:

int flexposition, servoposition, straight = 600, bent = 1000;

Try declaring those on separate lines.
Otherwise, hard to say without seeing the compiler error and the line it highlights.

Just posted the output… I think. Copied the little window. I am trying to change the word auto now to something else, based on previous advice. Didn’t think it was an "important " word because the colour didn’t change to orange! Noob logic!

Thanks so much guys. Seriously didnt consider the “auto” thing. I had a very slight suspicion but because it didnt change colour I thought it was all good.

Suppose sometimes you need a mate in life (read the code for joke): :slight_smile: Thanks again for the speedy responses.

#include <Servo.h> 
Servo servo1;
const int flexpin = 0;
int flexposition, servoposition, straight = 600, bent = 1000;

void setup()
{ 
  Serial.begin(9600);
  servo1.attach(9);
} 

void loop() 
{ 
  flexposition = analogRead(flexpin);
  automate();
  //servoposition = map(flexposition, 750, 900, 0, 180);
  //servoposition = constrain(servoposition, 0, 180);
  servo1.write(servoposition);
  Serial.print("sensor: ");
  Serial.print(flexposition);
  Serial.print("  servo: ");
  Serial.println(servoposition);
  delay(20);  // wait 20ms between servo updates
} 
void automate()
{
  if (flexposition < straight)
  {
    straight = flexposition;
  }
  if (flexposition > bent)
  {
    bent = flexposition;
  }
  servoposition = map(flexposition, straight ,bent ,0 ,180);
  servoposition = constrain (servoposition, 0, 180);
}

On a sidenote. With powerful enough servos and a great pwersource, could we use this code to make iron mans suit. Well the hand at least. Yeah baby. #Just sayin