Programming error

Hi,
I had a program who compiled OK yesterday, but when I compile it, I get this error:

AutoV2.ino: In function 'void loop()':
AutoV2.ino:183:24: error: 'motor' was not declared in this scope
AutoV2.ino:186:25: error: 'motor' was not declared in this scope
AutoV2.ino:186:27: error: expected ';' before '}' token
AutoV2.ino:188:24: error: 'motor' was not declared in this scope
AutoV2.ino:191:25: error: 'motor' was not declared in this scope
AutoV2.ino:216:1: error: a function-definition is not allowed here before '{' token
AutoV2.ino:402:1: error: expected '}' at end of input
Error de compilación

And here is the code:

#include <Servo.h>


// Arduino pins for the shift register
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8-bit bus after the 74HC595 shift register 
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
//#define SERVO1_PWM 10
//#define SERVO2_PWM 9

// Codes for the motor function.
#define ADELANTE 1
#define ATRAS 2
#define DERECHA 1
#define IZQUIERDA 2
#define FRENAR 3
#define LIBRE 4


// Declare classes for Servo connectors of the MotorShield.
//Servo servo_1;
//Servo servo_2;


long distancia;
long tiempo;


void setup(){
  Serial.begin(9600);
  pinMode(A0, OUTPUT); /*activación del pin 9 como salida: para el pulso ultrasónico*/
  pinMode(10, INPUT); /*activación del pin 8 como entrada: tiempo del rebote del ultrasonido*/
  pinMode(2, OUTPUT);
  Serial.println("Simple Motor Shield sketch");
  
}


void loop() {
digitalWrite(A0,LOW); /* Por cuestión de estabilización del sensor*/
  delayMicroseconds(5);
  digitalWrite(A0, HIGH); /* envío del pulso ultrasónico*/
  delayMicroseconds(10);
  tiempo=pulseIn(10, HIGH); /* Función para medir la longitud del pulso entrante. Mide el tiempo que transcurrido entre el envío
  del pulso ultrasónico y cuando el sensor recibe el rebote, es decir: desde que el pin 12 empieza a recibir el rebote, HIGH, hasta que
  deja de hacerlo, LOW, la longitud del pulso entrante*/
  distancia= int(0.017*tiempo); /*fórmula para calcular la distancia obteniendo un valor entero*/
  /*Monitorización en centímetros por el monitor serial*/
  Serial.println("Distancia ");
  Serial.println(distancia);
  Serial.println(" cm");
  delay(100);
  if (distancia < 15) {
    digitalWrite(2, HIGH);
  }
    else{ 
    digitalWrite(2, LOW);
  }
{

  // Suppose a DC motor is connected to M1_A(+) and M1_B(-)
  // Let it run full speed forward and half speed backward.
  // If 'BRAKE' or 'RELEASE' is used, the 'speed' parameter
  // is ignored.
  
  // cambiando el numero de motor 1,2,3 o 4 moves uno de los 4 motores conectados
if (distancia < 100) {
  motor(2, ADELANTE, 70);
}
  else {
  motor(2, ADELANTE, 255) }
if (distancia < 5) {
  motor(1, DERECHA, 255);
}
else {
  motor(2, ADELANTE, 255);
}
  
}


// Initializing
// ------------
// There is no initialization function.
//
// The shiftWrite() has an automatic initializing.
// The PWM outputs are floating during startup, 
// that's okay for the Motor Shield, it stays off.
// Using analogWrite() without pinMode() is valid.
//


// ---------------------------------
// motor
//
// Select the motor (1-4), the command, 
// and the speed (0-255).
// The commands are: FORWARD, BACKWARD, BRAKE, RELEASE.
//
void motor(int nMotor, int command, int speed) {
int motorA, motorB;
  if (nMotor >= 1 && nMotor <= 4)
  {  
    switch (nMotor)
    {
    case 1:
      motorA   = MOTOR1_A;
      motorB   = MOTOR1_B;
      break;
    case 2:
      motorA   = MOTOR2_A;
      motorB   = MOTOR2_B;
      break;
    case 3:
      motorA   = MOTOR3_A;
      motorB   = MOTOR3_B;
      break;
    case 4:
      motorA   = MOTOR4_A;
      motorB   = MOTOR4_B;
      break;
    default:
      break;
    }

    switch (command)
    {
    case ADELANTE:
      motor_output (motorA, HIGH, speed);
      motor_output (motorB, LOW, -1);     // -1: no PWM set
      break;
    case ATRAS:
      motor_output (motorA, LOW, speed);
      motor_output (motorB, HIGH, -1);    // -1: no PWM set
      break;
    case FRENAR:
      // The AdaFruit library didn't implement a brake.
      // The L293D motor driver ic doesn't have a good
      // brake anyway.
      // It uses transistors inside, and not mosfets.
      // Some use a software break, by using a short
      // reverse voltage.
      // This brake will try to brake, by enabling 
      // the output and by pulling both outputs to ground.
      // But it isn't a good break.
      motor_output (motorA, LOW, 255); // 255: fully on.
      motor_output (motorB, LOW, -1);  // -1: no PWM set
      break;
    case LIBRE:
      motor_output (motorA, LOW, 0);  // 0: output floating.
      motor_output (motorB, LOW, -1); // -1: no PWM set
      break;
    default:
      break;
    }
  }
}


// ---------------------------------
// motor_output
//
// The function motor_ouput uses the motor driver to
// drive normal outputs like lights, relays, solenoids, 
// DC motors (but not in reverse).
//
// It is also used as an internal helper function 
// for the motor() function.
//
// The high_low variable should be set 'HIGH' 
// to drive lights, etc.
// It can be set 'LOW', to switch it off, 
// but also a 'speed' of 0 will switch it off.
//
// The 'speed' sets the PWM for 0...255, and is for 
// both pins of the motor output.
//   For example, if motor 3 side 'A' is used to for a
//   dimmed light at 50% (speed is 128), also the 
//   motor 3 side 'B' output will be dimmed for 50%.
// Set to 0 for completelty off (high impedance).
// Set to 255 for fully on.
// Special settings for the PWM speed:
//    Set to -1 for not setting the PWM at all.
//
void motor_output (int output, int high_low, int speed)
{
  int motorPWM;

  switch (output)
  {
  case MOTOR1_A:
  case MOTOR1_B:
    motorPWM = MOTOR1_PWM;
    break;
  case MOTOR2_A:
  case MOTOR2_B:
    motorPWM = MOTOR2_PWM;
    break;
  case MOTOR3_A:
  case MOTOR3_B:
    motorPWM = MOTOR3_PWM;
    break;
  case MOTOR4_A:
  case MOTOR4_B:
    motorPWM = MOTOR4_PWM;
    break;
  default:
    // Use speed as error flag, -3333 = invalid output.
    speed = -3333;
    break;
  }

  if (speed != -3333)
  {
    // Set the direction with the shift register 
    // on the MotorShield, even if the speed = -1.
    // In that case the direction will be set, but
    // not the PWM.
    shiftWrite(output, high_low);

    // set PWM only if it is valid
    if (speed >= 0 && speed <= 255)    
    {
      analogWrite(motorPWM, speed);
    }
  }
}


// ---------------------------------
// shiftWrite
//
// The parameters are just like digitalWrite().
//
// The output is the pin 0...7 (the pin behind 
// the shift register).
// The second parameter is HIGH or LOW.
//
// There is no initialization function.
// Initialization is automatically done at the first
// time it is used.
//

void shiftWrite(int output, int high_low)
{
  static int latch_copy;
  static int shift_register_initialized = false;

  // Do the initialization on the fly, 
  // at the first time it is used.
  if (!shift_register_initialized)
  {
    // Set pins for shift register to output
    pinMode(MOTORLATCH, OUTPUT);
    pinMode(MOTORENABLE, OUTPUT);
    pinMode(MOTORDATA, OUTPUT);
    pinMode(MOTORCLK, OUTPUT);

    // Set pins for shift register to default value (low);
    digitalWrite(MOTORDATA, LOW);
    digitalWrite(MOTORLATCH, LOW);
    digitalWrite(MOTORCLK, LOW);
    // Enable the shift register, set Enable pin Low.
    digitalWrite(MOTORENABLE, LOW);

    // start with all outputs (of the shift register) low
    latch_copy = 0;

    shift_register_initialized = true;
  }

  // The defines HIGH and LOW are 1 and 0.
  // So this is valid.
  bitWrite(latch_copy, output, high_low);

  // Use the default Arduino 'shiftOut()' function to
  // shift the bits with the MOTORCLK as clock pulse.
  // The 74HC595 shiftregister wants the MSB first.
  // After that, generate a latch pulse with MOTORLATCH.
  shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, HIGH);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, LOW);
}
}

I don’t understand the error, because the defines are in the beggining of the code.

You need to add a function prototype declaration for motor() near the top of the source file. Also, there are a bunch of messed up braces, too. Use Ctrl-T in the IDE to reformat your code before posting. It makes it easier for us to read. The attached code compiles, but I have no way to test it.

#include <Servo.h>


// Arduino pins for the shift register
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8-bit bus after the 74HC595 shift register
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
//#define SERVO1_PWM 10
//#define SERVO2_PWM 9

// Codes for the motor function.
#define ADELANTE 1
#define ATRAS 2
#define DERECHA 1
#define IZQUIERDA 2
#define FRENAR 3
#define LIBRE 4


// Declare classes for Servo connectors of the MotorShield.
//Servo servo_1;
//Servo servo_2;

void motor(int nMotor, int command, int speed);     // Added function prototype declaration 

long distancia;
long tiempo;


void setup() {
  Serial.begin(9600);
  pinMode(A0, OUTPUT); /*activación del pin 9 como salida: para el pulso ultrasónico*/
  pinMode(10, INPUT); /*activación del pin 8 como entrada: tiempo del rebote del ultrasonido*/
  pinMode(2, OUTPUT);
  Serial.println("Simple Motor Shield sketch");

}


void loop() {
  digitalWrite(A0, LOW); /* Por cuestión de estabilización del sensor*/
  delayMicroseconds(5);
  digitalWrite(A0, HIGH); /* envío del pulso ultrasónico*/
  delayMicroseconds(10);
  tiempo = pulseIn(10, HIGH); /* Función para medir la longitud del pulso entrante. Mide el tiempo que transcurrido entre el envío
  del pulso ultrasónico y cuando el sensor recibe el rebote, es decir: desde que el pin 12 empieza a recibir el rebote, HIGH, hasta que
  deja de hacerlo, LOW, la longitud del pulso entrante*/
  distancia = int(0.017 * tiempo); /*fórmula para calcular la distancia obteniendo un valor entero*/
  /*Monitorización en centímetros por el monitor serial*/
  Serial.println("Distancia ");
  Serial.println(distancia);
  Serial.println(" cm");
  delay(100);
  if (distancia < 15) {
    digitalWrite(2, HIGH);
  }
  else {
    digitalWrite(2, LOW);
  }
  

    // Suppose a DC motor is connected to M1_A(+) and M1_B(-)
    // Let it run full speed forward and half speed backward.
    // If 'BRAKE' or 'RELEASE' is used, the 'speed' parameter
    // is ignored.

    // cambiando el numero de motor 1,2,3 o 4 moves uno de los 4 motores conectados
    if (distancia < 100) {
      motor(2, ADELANTE, 70);
    }
    else {
      motor(2, ADELANTE, 255);
    }
    if (distancia < 5) {
      motor(1, DERECHA, 255);
    }
    else {
      motor(2, ADELANTE, 255);
    }

  }

  // ---------------------------------
  // motor
  //
  // Select the motor (1-4), the command,
  // and the speed (0-255).
  // The commands are: FORWARD, BACKWARD, BRAKE, RELEASE.
  //
  void motor(int nMotor, int command, int speed) {
    int motorA, motorB;
    if (nMotor >= 1 && nMotor <= 4)
    {
      switch (nMotor)
      {
        case 1:
          motorA   = MOTOR1_A;
          motorB   = MOTOR1_B;
          break;
        case 2:
          motorA   = MOTOR2_A;
          motorB   = MOTOR2_B;
          break;
        case 3:
          motorA   = MOTOR3_A;
          motorB   = MOTOR3_B;
          break;
        case 4:
          motorA   = MOTOR4_A;
          motorB   = MOTOR4_B;
          break;
        default:
          break;
      }

      switch (command)
      {
        case ADELANTE:
          motor_output (motorA, HIGH, speed);
          motor_output (motorB, LOW, -1);     // -1: no PWM set
          break;
        case ATRAS:
          motor_output (motorA, LOW, speed);
          motor_output (motorB, HIGH, -1);    // -1: no PWM set
          break;
        case FRENAR:
          // The AdaFruit library didn't implement a brake.
          // The L293D motor driver ic doesn't have a good
          // brake anyway.
          // It uses transistors inside, and not mosfets.
          // Some use a software break, by using a short
          // reverse voltage.
          // This brake will try to brake, by enabling
          // the output and by pulling both outputs to ground.
          // But it isn't a good break.
          motor_output (motorA, LOW, 255); // 255: fully on.
          motor_output (motorB, LOW, -1);  // -1: no PWM set
          break;
        case LIBRE:
          motor_output (motorA, LOW, 0);  // 0: output floating.
          motor_output (motorB, LOW, -1); // -1: no PWM set
          break;
        default:
          break;
      }
    }
  }


  // ---------------------------------
  // motor_output
  //
  // The function motor_ouput uses the motor driver to
  // drive normal outputs like lights, relays, solenoids,
  // DC motors (but not in reverse).
  //
  // It is also used as an internal helper function
  // for the motor() function.
  //
  // The high_low variable should be set 'HIGH'
  // to drive lights, etc.
  // It can be set 'LOW', to switch it off,
  // but also a 'speed' of 0 will switch it off.
  //
  // The 'speed' sets the PWM for 0...255, and is for
  // both pins of the motor output.
  //   For example, if motor 3 side 'A' is used to for a
  //   dimmed light at 50% (speed is 128), also the
  //   motor 3 side 'B' output will be dimmed for 50%.
  // Set to 0 for completelty off (high impedance).
  // Set to 255 for fully on.
  // Special settings for the PWM speed:
  //    Set to -1 for not setting the PWM at all.
  //
  void motor_output (int output, int high_low, int speed)
  {
    int motorPWM;

    switch (output)
    {
      case MOTOR1_A:
      case MOTOR1_B:
        motorPWM = MOTOR1_PWM;
        break;
      case MOTOR2_A:
      case MOTOR2_B:
        motorPWM = MOTOR2_PWM;
        break;
      case MOTOR3_A:
      case MOTOR3_B:
        motorPWM = MOTOR3_PWM;
        break;
      case MOTOR4_A:
      case MOTOR4_B:
        motorPWM = MOTOR4_PWM;
        break;
      default:
        // Use speed as error flag, -3333 = invalid output.
        speed = -3333;
        break;
    }

    if (speed != -3333)
    {
      // Set the direction with the shift register
      // on the MotorShield, even if the speed = -1.
      // In that case the direction will be set, but
      // not the PWM.
      shiftWrite(output, high_low);

      // set PWM only if it is valid
      if (speed >= 0 && speed <= 255)
      {
        analogWrite(motorPWM, speed);
      }
    }
  }


  // ---------------------------------
  // shiftWrite
  //
  // The parameters are just like digitalWrite().
  //
  // The output is the pin 0...7 (the pin behind
  // the shift register).
  // The second parameter is HIGH or LOW.
  //
  // There is no initialization function.
  // Initialization is automatically done at the first
  // time it is used.
  //

  void shiftWrite(int output, int high_low)
  {
    static int latch_copy;
    static int shift_register_initialized = false;

    // Do the initialization on the fly,
    // at the first time it is used.
    if (!shift_register_initialized)
    {
      // Set pins for shift register to output
      pinMode(MOTORLATCH, OUTPUT);
      pinMode(MOTORENABLE, OUTPUT);
      pinMode(MOTORDATA, OUTPUT);
      pinMode(MOTORCLK, OUTPUT);

      // Set pins for shift register to default value (low);
      digitalWrite(MOTORDATA, LOW);
      digitalWrite(MOTORLATCH, LOW);
      digitalWrite(MOTORCLK, LOW);
      // Enable the shift register, set Enable pin Low.
      digitalWrite(MOTORENABLE, LOW);

      // start with all outputs (of the shift register) low
      latch_copy = 0;

      shift_register_initialized = true;
    }

    // The defines HIGH and LOW are 1 and 0.
    // So this is valid.
    bitWrite(latch_copy, output, high_low);

    // Use the default Arduino 'shiftOut()' function to
    // shift the bits with the MOTORCLK as clock pulse.
    // The 74HC595 shiftregister wants the MSB first.
    // After that, generate a latch pulse with MOTORLATCH.
    shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
    delayMicroseconds(5);    // For safety, not really needed.
    digitalWrite(MOTORLATCH, HIGH);
    delayMicroseconds(5);    // For safety, not really needed.
    digitalWrite(MOTORLATCH, LOW);
  }

The Arduino environment automatically adds prototypes, the problem is that braces are mismatched, as witnessed by the message "error: a function-definition is not allowed here before '{' token", meaning that the motor() function isn't compiled and hence triggering the other errors.

@MarkT: I've had code--I can't remember the circumstances (I've slept since then)--where the compiler did not supply the missing prototype. I think I remember a post here with the same problem. I just fixed the braces in the original code and it did allow the code to compile.

I don't understand, it compiles OK now! I restarted the computer, and problem solved! Thanks for your help!