Im confused of this anyone please help it shows a function-definition is not allowed here before '{' token

const int swPin = 12;
const int VrxPin = A5;
const int VryPin = A0;

// Set the initial variable values
int xDirection = 0;
int yDirection = 0;
int switchState = 1;

void setup() {
  Serial.begin(9600);

  pinMode(swPin, INPUT);

  // The joystick button is always HIGH, unless the button is pressed
  digitalWrite(swPin, HIGH);
}

void loop() {

  // Read the analog joystick values
  xDirection = analogRead(VryPin);
  yDirection = analogRead(VrxPin);

  // Read the push button state
  switchState = digitalRead(swPin);

  // Print the values read to the serial monitor
  Serial.print("Switch:  ");
  Serial.println(switchState);

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

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

  Serial.println("");

  // Because the switch is HIGH when it is NOT pressed we have to invert the read values
  // We use the exclamation mark in front of the variable name which converts HIGH in LOW and vice versa
  if (!switchState) {
    Serial.println("Switch pressed");
  }

  // We do not exacty use 512 as the center of the joystick position, since the analogue value can
  // fluctuate. The center is approx. 500, therefore we use a margin of 20.

  // Determine the joystick direction on the X-axis
  if (xDirection < 480) {
    Serial.println("Left");
  } else if (xDirection > 520) {
    Serial.println("Right");
  }

  // Determine the joystick direction on the Y-axis
  if (yDirection < 480) {
    Serial.println("Down");
  } else if (yDirection > 520) {
    Serial.println("Up");
  }

  delay(500); 

Code above is just missing a } to end loop

Then the original post seems to duplicate the code again, this with the ending } included:

const int swPin = 12;
  const int VrxPin = A5;
  const int VryPin = A0;

  // Set the initial variable values
  int xDirection = 0;
  int yDirection = 0;
  int switchState = 1;

  void setup() {
    Serial.begin(9600);

    pinMode(swPin, INPUT);

    // The joystick button is always HIGH, unless the button is pressed
    digitalWrite(swPin, HIGH);
  }

  void loop() {

    // Read the analog joystick values
    xDirection = analogRead(VryPin);
    yDirection = analogRead(VrxPin);

    // Read the push button state
    switchState = digitalRead(swPin);

    // Print the values read to the serial monitor
    Serial.print("Switch:  ");
    Serial.println(switchState);

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

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

    Serial.println("");

    // Because the switch is HIGH when it is NOT pressed we have to invert the read values
    // We use the exclamation mark in front of the variable name which converts HIGH in LOW and vice versa
    if (!switchState) {
      Serial.println("Switch pressed");
    }

    // We do not exacty use 512 as the center of the joystick position, since the analogue value can
    // fluctuate. The center is approx. 500, therefore we use a margin of 20.

    // Determine the joystick direction on the X-axis
    if (xDirection < 480) {
      Serial.println("Left");
    } else if (xDirection > 520) {
      Serial.println("Right");
    }

    // Determine the joystick direction on the Y-axis
    if (yDirection < 480) {
      Serial.println("Down");
    } else if (yDirection > 520) {
      Serial.println("Up");
    }

    delay(500);
  }

(Code broken up by Moderator)

You seem to have 2 setup()s and 2 loop()s which will never work. Maybe the code is just posted incorrectly.

Either way when you have an error message don't paraphrase it, post the COMPLETE message which includes lots of useful information including which line the problem is on.

Steve

Post complete code in code tags, post complete compile log (verbose mode) in code tags please.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.