Pages: [1]   Go Down
Author Topic: Im getting an error: expected unqualified-id before "{" token, can someone help?  (Read 589 times)
0 Members and 1 Guest are viewing this topic.
Central Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Im getting an error: expected unqualified-id before "{" token, can someone help?

Code follows

#include <Servo.h>

{
 
Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

void setup() 

 servoLeft.attach(9);  // attaches the servo on pin 9 to the servo object
 servoRight.attach(10);
 

int potpin0 = 0;
int potpin1 = 1; // analog pin used to connect the potentiometer
int val1;
int val0; // variable to read the value from the analog pin
 


{
  servoLeft.attach(9);  // attaches the servo on pin 9 to the servo object
  servoRight.attach(10);
 
 }
 


  val1 = analogRead(potpin0);
  val0 = analogRead(potpin1);  // reads the value of the potentiometer (value between 0 and 1023)
  val0 = map(val0, 0, 1023, 0, 179);
  val1 = map(val1, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
  servoLeft.write(val0);
  servoRight.write(val1);    // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

// Motion routines for forward, reverse, turns, and stop

{
  servoLeft.write(val0);
  servoRight.write(val1);
  delay(20);
}
Logged


Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Everything needs to be inside either the "setup" or "loop" functions. The way you define a function (including setup and loop) is:

Code:
return_type function_name(arguments) {
  code
}

So, for setup() where it doesn't take any arguments and doesn't return anything, you'd say
Code:
void setup() {
  // Whatever you want to run once
}

If, in the arduino IDE, you go to tools -> auto-format, that will indent everything for you so you can make sure you have a } for each {. However, you have to make sure they're in the correct spots yourself (for example, not having a { at the beginning of the program and putting it after setup() )
Logged

Central Florida
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I'm still partially ignorant.

After setting up once, then I write a "loop" for the rest? How? I thought I formatted it right. Did I miss an "if" "then"?
Logged


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

Code:
{
 
Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo
Randomly splattering curly braces around is not the way to write code,
Logged

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

Yep.. Looks like you got a little carried away with the whole  {   } concept... I think you meant to write something like this.


#include <Servo.h>
 
Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

int potpin0 = 0;
int potpin1 = 1; // analog pin used to connect the potentiometer
int val1;
int val0; // variable to read the value from the analog pin
 
void setup()
{
  servoLeft.attach(9);  // attaches the servo on pin 9 to the servo object
  servoRight.attach(10);
 
 }
 

void loop()
{
  val1 = analogRead(potpin0);
  val0 = analogRead(potpin1);  // reads the value of the potentiometer (value between 0 and 1023)
  val0 = map(val0, 0, 1023, 0, 179);
  val1 = map(val1, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
  servoLeft.write(val0);
  servoRight.write(val1);    // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

// Motion routines for forward, reverse, turns, and stop
/*example code and is not needed and is commented out
{
  servoLeft.write(val0);
  servoRight.write(val1);
  delay(20);
}*/
Logged

Pages: [1]   Go Up
Jump to: