Micro servo 9g program code not working (Sweep for 2)

So here is the program I adjusted for my micro servos, and what it replies is in BOLD:


#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
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(125); // waits 125ms 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(125); // waits 125ms for the servo to reach the position
}
}
#include <Servo.h>

Servo myservo // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(8); // attaches the servo on pin 8 to the servo object
}

void loop() {
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(125); // waits 125ms 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(125); // waits 125ms for the servo to reach the position
}
}

It says: "Expected initializer before 'int'" Help please!

Servo myservo;  // create servo object to control a servo;

@Idahowalker for what should that go to?

Hello, I am struggling with programming 2 servos at once. Below I have pasted a link to the program to show you what is showing up.

Please help me indicate the correct path!

Thanks!


#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
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(125); // waits 125ms 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(125); // waits 125ms for the servo to reach the position
}
}
#include <Servo.h>

Servo myservo // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(8); // attaches the servo on pin 8 to the servo object
}

void loop() {
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(125); // waits 125ms 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(125); // waits 125ms for the servo to reach the position
}
}


The portion I bolded keeps referring to: Expected initializer before 'int' ---> help please!

Hi Dan_L3gend, welcome to the forum.
Please read through the sticky post at the top of this forum named How to use this forum - please read.
It will help you with how to post code, making it more readable and avoiding the forum software from confusing code and emoticons.

Did you mean to post 2 copies of your source there? Or are you trying to make 2 servos sweep?

I am trying to make 2 servos sweep at the same time, but it is not working out well, only one is processing. ;/

Servo myservo  // Right there.

Please remember to use code tags when posting code.

You can only have one setup() and one loop() in a program. All the code has to go in them. You only need to include libraries like Servo.h once. And you can't call both servos by the same name myservo...that's just silly.

Have another go and post that using </> code tags as described in "How to use this forum".

Steve

Is it ok if you give me the code for it? I got confused

Dan_L3gend:
Is it ok if you give me the code for it? I got confused

Rather than give you code, we prefer to help you discover how to fix it.

You attempted to move 2 servos by copying the entire code set and changing the pin number in the second set.
That clearly does not work.

Start over with the sweep example. One setup. One loop.

Where it declares a servo, copy that line and declare another servo with another name.

Where it attaches the servo, copy that line and change the servo to the second servo name. Change the pin number to the pin your second servo uses.

Where it writes a position to the first servo, copy that line and change the servo to the second servo name.

Dan_L3gend:
@Idahowalker for what should that go to?

Find where you have:

Servo myservo  // create servo object to control a servo;

And change it to

Servo myservo;  // create servo object to control a servo;

Also, if you had 2 kids, would you name them Tim and Tim, so that you are calling both each time you call one?

I did every step you told @vinceherman, but for the position, it still says " expected initializer in 'int'"

Dan_L3gend:
I did every step you told @vinceherman, but for the position, it still says " expected initializer in 'int'"

Post the code that represents your best attempt. Use code tags.

So post your new code so we can see what you've done. That always works better than us trying to guess where you might have gone wrong.

Steve

#include <Servo.h>

Servo servo1; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
}

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

Servo servo2 // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
servo2.attach(8); // attaches the servo on pin 8 to the servo object
}

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

Servo servo2  // Again, right there.

Please remember to use code tags when posting code

Like this?


#include <Servo.h>

Servo servo1; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
}

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

Servo servo2; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
servo2.attach(8); // attaches the servo on pin 8 to the servo object
}

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

Like this?

What does the compiler say?

Why have you still got two setups and two loops?

Please remember to use code tags when posting code

Let me help you with finding the post on How to use this forum - please read
Read all of it. Pay attention to number 7.

In my post #7, I listed a number of steps for you to try to get this working.

The first was

Start over with the sweep example. One setup. One loop.

You did not do this. Your latest code still has 2 setup functions and 2 loop functions.

Here is the code for the sweep tutorial

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  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(15);                       // 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(15);                       // waits 15ms for the servo to reach the position
  }
}

See that it has only one setup. And only one loop.

Try running just that, and see if you can get one servo to move.
Then try the remainder of the steps I listed in post #7.

It is moving, but I'm trying to sweep 2 servos at once, which isn't working from all the steps I've followed ;/