-1

I keep getting 'myservo' does not name a type as an error message, I have already looked other solved questions like this one but I can´t find the mistake. I would be very glad for your help.

/*********************************************************
 * Bot control
 * By soleil_zero
 * Based on code by
 * MSc. Fausto M. Lagos S.
 * @piratax007
 * V2.0.0 - 2019
 * GNU GPLV3+
 * 
 * Hardware:
 * Arduino Mega2560
 * Adafruit Motor Shield V1.2
 * Bluetooth module H6
 * 1 Servo
 * 2 DC Motor
 *********************************************************/

// Librería para Adafruit Motor Shield V1.2 y Servo
#include "AFMotor.h"
#include "Servo.h"

// Motores conectados en los puertos 1 y 2
AF_DCMotor leftMotor(1, MOTOR12_64KHZ);
AF_DCMotor rightMotor(2, MOTOR12_64KHZ);

Servo myservo;
myservo.attach(9);

// Variable para el paso de mensajes vía Bluetooth
char message;
//ángulo de rotación del servo
int angle = 0;

void setup() {
  // Velocidades en los motores
  leftMotor.setSpeed(150);
  rightMotor.setSpeed(150);

  /* Debido a que el Adafruit Motor Shield V1.2 ocupa los pines digitales 0 y 1 para la conexión del módulo HC05 
     bluetooth deben utilizarse cualquiera de los otros disponibles: 14 (TX3) y 15 (RX3) o 16 (TX2) y 17 (RX2) o
     18 (TX1) Y 19 (RX1). De acuerdo a cuál de los cuatro pines se utilicen deben modificarse las líneas
     relacionadas a la comunicación Serial e.g si se utilizan los pines 18 y 19 la instrucción será Serial1 */
  Serial3.begin(9600);
  myservo.write(angle);
}

void loop() {
  // Verificación de mensaje en el puerto Serial
  if (Serial3.available() > 0) {
    message = Serial3.read();
  }

 if (message == 'F') {//Forward
    leftMotor.run(FORWARD);
    rightMotor.run(FORWARD);
  } else if (message == 'B') {//Backwards
    leftMotor.run(BACKWARD);
    rightMotor.run(BACKWARD);
  } else if (message == 'L') {//Left
    leftMotor.run(FORWARD);
    rightMotor.run(BACKWARD);
  } else if (message == 'R') {//right
    leftMotor.run(BACKWARD);
    rightMotor.run(FORWARD);
  } else if (message == 'S') {//stop
    leftMotor.run(RELEASE);
    rightMotor.run(RELEASE);
    Serial3.print("stop");
  } 
  } else if (message == 'U') {//down
    angle = 0;
    servo1.write(angle);
  }else if (message == 'D') {//up
    angle = 90;
    servo1.write(angle);
  }
}
  • this has been asked a lot ... please do research before posting your question – jsotola Jun 10 at 22:19
1

In C and C++ languages we write statements inside functions. That myservo.attach(9); is a statement. You can't just write it in the middle of the file. It has to reside inside a function.

In your code myservo.attach(9); just sits in the middle of nowhere. You had enough sense to put leftMotor.setSpeed(150); inside setup(). Why did you suddenly decide to place myservo.attach(9); outside of any functions?

Function setup() would be an appropriate place for this statement.

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.