1

Hello i am getting into Arduino and want to get my code tidy. I dont know how but thought by #include "Stepper.h" and "Servo.h" would work. The programm shows it works. But now it shows that i an expected inizializer before 'myservo' is missing. I dont know what is meant by this. Any help would be great! Here is the code: the first one is the first tab the second the second tab and so on.

int stepsPerRevolution=2048;
int motSpeed=10;
int dt=500;
int buttonPin=2;
int motDir=1;
int buttonValNew;
int buttonValOld=1;
Stepper myStepper (stepsPerRevolution, 8,10,9,11);



#include <Servo.h> //Servo
Servo myservo;
int pos =0;

#include "Servo.h"//einbezug der Dateien
#include "Stepper.h"


int menu = 0;
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
//Displays the menu
  if (menu == 0) {
    //Prints the menu to the serial port
    Serial.println("Wähle eine Eingabe aus:");
    Serial.println("-----------------");
    Serial.println("1) Starte den Stepper Motor");
    Serial.println("2) Starte den Servo Motor");
    Serial.println("3) Stoppe alles");
    Serial.println("4) Trace Boundary");
    Serial.println("5) Start Drawing");
    Serial.flush();

    while (!Serial.available()) {}  
    menu = Serial.parseInt();       // sucht für einen interger
    Serial.flush();
  }

The second where the problem is at line 8:

Servo myservo;
int pos =0;


void setup() 
  // put your setup code here, to run once:
myservo.attach(3);  // attaches the servo on pin 9 to the servo object

}

void loop() 
  // put your main code here, to run repeatedly:

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(0);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(0);                       // waits 15ms for the servo to reach the position
  }
}

And the thrid tab in the programm:

int stepsPerRevolution=2048;
int motSpeed=10;
int dt=500;


int buttonPin=2;
int motDir=1;
int buttonValNew;
int buttonValOld=1;
Stepper myStepper (stepsPerRevolution, 8,10,9,11);

void setup() 
  // put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
pinMode(buttonPin,INPUT);
digitalWrite(buttonPin,HIGH); 
}
void loop() 
  // put your main code here, to run repeatedly:
buttonValNew=digitalRead(buttonPin);
if (buttonValOld==1 && buttonValNew==0) {
  motDir=motDir*(-1);
}
myStepper.step(motDir*1);
} ```
4
  • the second and third listings are missing parts of the program structure .... refer to the example minimal sketch in arduino IDE to see what you are missing – jsotola Apr 15 at 20:45
  • In the first one you are using the stepper library before actually including it (You should do all includes at the very top of your file). In the second code you are missing the include for the servo library and you have missing { at the start of setup() and loop(). Similar in the third code: Missing include and missing { for setup() and loop(). In total: You need to include all the libraries, that you want to use in the sketch. And you must stick to the correct C/C++ syntax. You can refer to C/C++ tutorials for that. – chrisl Apr 15 at 21:00
  • Also compiler errors are quite descriptive. You can put the error message into google and you will most likely find many results for help. When the compiler throws an error it is mostly a pure C/C++ problem and has nothing to do with Arduino. I hope the above description helps, but since your question is not about Arduino but about C/C++ syntax, it is off-topic here. I'm voting to close the question. – chrisl Apr 15 at 21:02
  • 1
    Why are you including Servo.h twice? – KIIV Apr 15 at 21:31