so im doing a project where i need to move two servos with the push of a button. when the button is pushed both of the servos should turn 90 degrees but each of them in the opposit direction. im in a real hurry and i am not able to code and this would be the only time i would need coding. the only thing i have is a arduino nano. i would appreciate if some one could do this for me or explain it to me. ive almost watched every youtube video and read a lot of the stuff in this forum but im not able to code it . please help
(deleted)
(deleted)
(deleted)
Can't you find a starting point (or even a near-ready sketch) in the examples that come with the Servo library?
yes i manged to get e servo continius rotating 90 degrees but i dont know how to get a button involved and ow to get a second servo that turns in the opposit direction
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
void loop() {
for (pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(2);
}
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
thats how far i got
Just change the angle for the other one. One goes from 0 to 90, the other from 90 to 0. Or 90-pos
.
For buttons, lots of examples around as well.
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
void loop() {
for (pos = 0; pos <= 90; pos += 1) {
myservo.write(pos);
delay(2);
}
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
#include <Servo.h>
Servo myservo2;
int pos = 0;
void setup() {
myservo2.attach(10);
void loop() {
for (pos = 0; pos <= -90; pos += 1) {
myservo2.write(pos);
delay(10);
}
for (pos = -90; pos >= 0; pos -= 1) {
myservo2.write(pos);
delay(10);
}
}
so is that correct? and about the button i saw the code online but i dont know how to build it inside my code
It's not a good idea wasting people's time with code you haven't even bothered trying to compile. You can't have two setup() and two loop() functions in one program.
OTOH if you put the "Servo myservo2" immediately below the original one and the second .attach() in the original setup() then that would be a start. Then you can just put the myservo2.write()s immediately below the original writes with a different value...and wvmarle has already told you what that value needs to be.
But at least try compiling and testing it before you come back again.
Steve
... and use code tags when posting code. As explained in the sticky.
ok thanks steve really appreaciate that ill try what you said
#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0;
void setup() {
myservo.attach(9);
myservo2.attach(10);
void loop() {
for (pos = 0; pos <= 90; pos += 1) {
myservo.write(pos);
delay(2);
}
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
void loop() {
for (pos = 0; pos <= 90-; pos += 1) {
myservo2.write(pos);
delay(10);
}
for (pos = 90-; pos >= 0; pos -= 1) {
myservo2.write(pos);
delay(10);
}
}
is this better now i was letting it verify but it tells me that something is wrong in the void set up function
Use code tags, as shown in the "how to use this forum" post.
Otherwise the forum software eats some of your code and we can't be sure what we see is exactly what you wrote.
#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0;
void setup() {
myservo.attach(9);
myservo2.attach(10);
void loop() {
for (pos = 0; pos <= 90; pos += 1) {
myservo.write(pos);
delay(2);
}
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
void loop() {
for (pos = 0; pos <= 90-; pos += 1) {
myservo2.write(pos);
delay(10);
}
for (pos = 90-; pos >= 0; pos -= 1) {
myservo2.write(pos);
delay(10);
}
}
so like you said i repostet it
im sorry i didnt mean void setup i meant"void loop"
Which part of "you can't have two loop()s in one program" did you have trouble understanding?
Steve
void setup() {
Where is the } that matches this { ?
i dont know where that matching one is
Kilian03:
"a function-definition is not allowed here before '{' token" thats what its saying and if there cant be two loops am i supposed to cancel one and done or what am i supposed to do
No that's not what the error message says. It also gives you the details of where "here" is. That's useful information.
All the lines of code that you want to keep repeating have to be in the one and only loop() function. So you might have more luck if the take the stuff from inside your second loop() and add it inside the first loop. BTW a basic loop() function looks like:
void loop()
{
// all the code that repeats over and over again goes in here
}
The {} brackets are important. So if you say you don't know where one of them is...then have a careful look at the code that you're posting. It's either there somewhere or you're getting compile errors that you aren't bothering to share with us.
Steve