0
    #include <Servo.h>
Servo tilt, pan;

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int x, y;

// Second joystick pin numbers

const int SW_pinn = 4;
const int X_pinn = 3;
const int Y_pinn = 5;

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200); // setting serial baud rate tp 115200
  tilt.attach(7);
}

void loop() {
  x = X_pin; // defining the variable 
  y = Y_pin; 
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  x = map(analogRead(X_pin), 0, 1023, 900, 2100); // scale it to use with the servo b/w 900 usec to 2100 usec
  y = map(analogRead(Y_pin), 0, 1023, 900, 2100);
  tilt.write(x); // sets the servo position according to the scaled value 
  pan.write(y);
  delay(5);

}

  x = X_pinn; // defining the variable 
  y = Y_pinn; 
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pinn));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pinn));
  Serial.print("\n\n");
  x = map(analogRead(X_pinn), 0, 1023, 900, 2100); // scale it to use with the servo b/w 900 usec to 2100 usec
  y = map(analogRead(Y_pinn), 0, 1023, 900, 2100);
  tilt.write(x); // sets the servo position according to the scaled value 
  pan.write(y);
  delay(5);
}

When I compile this code in the Arduino IDE it gives me the error

   arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/166416428 -hardware arduino-builder/hardware -hardware arduino-builder/packages/cores -tools arduino-builder/tools -tools arduino-builder/packages/tools -built-in-libraries arduino-builder/latest -libraries /tmp/358566849/pinned -libraries /tmp/358566849/custom -fqbn arduino:avr:uno -build-cache /tmp -verbose=false /tmp/358566849/sketch_may12a

Multiple libraries were found for "Servo.h"

Used: /home/admin/builder/arduino-builder/latest/Servo-1.1.2

Not used: /home/admin/builder/arduino-builder/latest/Printoo_Library-1.0.2

/tmp/358566849/sketch_may12a/sketch_may12a.ino:40:3: error: 'x' does not name a type

x = X_pinn; // defining the variable 

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:41:3: error: 'y' does not name a type

y = Y_pinn; 

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:42:3: error: 'Serial' does not name a type

Serial.print("X-axis: ");

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:43:3: error: 'Serial' does not name a type

Serial.print(analogRead(X_pinn));

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:44:3: error: 'Serial' does not name a type

Serial.print("\n");

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:45:3: error: 'Serial' does not name a type

Serial.print("Y-axis: ");

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:46:3: error: 'Serial' does not name a type

Serial.println(analogRead(Y_pinn));

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:47:3: error: 'Serial' does not name a type

Serial.print("\n\n");

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:48:3: error: 'x' does not name a type

x = map(analogRead(X_pinn), 0, 1023, 900, 2100); // scale it to use with the servo b/w 900 usec to 2100 usec

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:49:3: error: 'y' does not name a type

y = map(analogRead(Y_pinn), 0, 1023, 900, 2100);

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:50:3: error: 'tilt' does not name a type

tilt.write(x); // sets the servo position according to the scaled value 

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:51:3: error: 'pan' does not name a type

pan.write(y);

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:52:8: error: expected constructor, destructor, or type conversion before '(' token

delay(5);

^

/tmp/358566849/sketch_may12a/sketch_may12a.ino:53:1: error: expected declaration before '}' token

}

^

exit status 1

In short, it saying that x does not name a type in this part of the code

x = X_pinn; // defining the variable 
  y = Y_pinn; 
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pinn));

Even though I defined it as a variable at the beginning... Any ideas?

  • 2
    You seem to have a chunk of your code twice. – Majenko May 12 '17 at 10:46
  • The error is due to that the code is outside a function block e.g. loop(). – Mikael Patel May 12 '17 at 11:59
1

If that is really your code then the errors are caused by one error. As Majenko says lines 58 through to 71 should not be there. It could be a cut and paste error when you wrote the post, but if it isn't that will cause a problem.

Some tips regarding your style that might help spot these faults in future.

  1. Don't give variables a larger scope than they need. See x and y, these don't need to be global variables. You only use them within the loop() and you don't store the value between loops. In fact lines (42 and 43) aren't needed which means you could define them as const int values on line 50 and 51.
  2. Braces {} - I know people like to write braces in the java style now, because it saves a line of text and all those 2 bytes will eventually add up, but If you had always put braces on line on there own you would have seen the error because line 57 doesn't have an opening brace.
  3. Encapsulate related data - I appreciate its only a little scrap program, but I would have placed the pin information in a class. This would have let you have better variable names.

const PinInfo joystick1 (2, 0, 1);

const PinInfo joystick2 (4, 3, 5);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.