Go Down

Topic: Micro servo 9g program code not working (Sweep for 2) (Read 980 times) previous topic - next topic

Dan_L3gend

Jun 18, 2019, 06:37 pm Last Edit: Jun 18, 2019, 06:59 pm by Dan_L3gend
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!

Idahowalker

Code: [Select]

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

Dan_L3gend


Dan_L3gend

#3
Jun 18, 2019, 07:09 pm Last Edit: Jun 18, 2019, 07:14 pm by Dan_L3gend
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!

vinceherman

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?

Dan_L3gend

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

AWOL

#6
Jun 18, 2019, 09:16 pm Last Edit: Jun 18, 2019, 09:17 pm by AWOL
Code: [Select]
Servo myservo  // Right there.

Please remember to use code tags when posting code.

slipstick

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

Dan_L3gend

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

vinceherman

#9
Jun 18, 2019, 10:19 pm Last Edit: Jun 18, 2019, 10:21 pm by vinceherman
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.

Idahowalker

@Idahowalker for what should that go to?
Find where you have:
Code: [Select]

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

And change it to
Code: [Select]

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?

Dan_L3gend

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

vinceherman

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.

slipstick

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

Dan_L3gend

#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
 }
}

Go Up