Go Down

Topic: NEWBIE Servo control!! (Read 661 times) previous topic - next topic

slugbut

Newbie Alert!!

I have two servos, which I can control via sweep using my nano.

What I would like to do though is....

Have Servo No1 sweep
Sweep 2 or 3 times and then PAUSE

Then have

Servo No2 sweep, PAUSE

and for the routine to loop.

Is this possible?

I've been searching everywhere but obviously not in the right places!!

Any help would be gratefully received.

Thanks,

Jon

vinceherman

You got sweep to work and you want new, different behavior.  Have you tried writing code for that?  Did you get sweep to work for both servos?

Post code.  Use code tags </>

We can help you from there.

Cactusface

#2
Jul 24, 2017, 04:46 pm Last Edit: Jul 24, 2017, 04:47 pm by Cactusface
Hi,
    If you can get sweep to work once, then just repeat in both directions, wait then do the same with servo 2, it really is so simple! We all have to start somewhere so give it a try. Yes you will make mistakes, but it's the only way to learn.....  If this is what you want to do.

Lets see your code and circuit, etc if you want more help.

Regards

Mel.
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

slugbut

#3
Jul 24, 2017, 06:35 pm Last Edit: Jul 24, 2017, 09:18 pm by slugbut
Thanks so much for your help.
Here is the code I've been playing with. I know very simple, but I still cant get it to work!!
Servo2 isnt working and I want a delay between servo1 and servo2 but everytime I play with the delay the servo gets slower. Is there a different command I should be using?
Thanks again in advance.

Code: [Select]
<#include <Servo.h>

Servo servo1;// moves eyes
Servo servo2;// moves eyelids

int pos1;      // angle of servo1
int pos2;      // angle of servo2

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


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

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

vinceherman

It looks like you missed using the code tags.   You can go back to your post, hit Modify, highlight the source and click the code tag button in the upper left that looks like this  </>

It looks like you have the void loop() defined twice.  Don't do that.  Delete the second definition.

slugbut

#5
Jul 24, 2017, 09:19 pm Last Edit: Jul 24, 2017, 09:25 pm by slugbut
Thanks,

I was using Firefox and it didnt give me the option!
Working with Chrome now and all the features are there.

I've removed the second void loop buut now it states I have:
/tmp/780431598/2_servos/2_servos.ino:36:1: error: expected '}' at end of input
exit status 1

Thanks again,

Jon

Code: [Select]
<#include <Servo.h>

Servo servo1;// moves eyes
Servo servo2;// moves eyelids

int pos1;      // angle of servo1
int pos2;      // angle of servo2

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


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

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

slipstick

No you still have two void loop() entries, line 16 and line 26. And your include on line 1 doesn't compile because it's got a spare < character in front of it. I can't see any attempt to get servo1 to sweep several times and there is nothing that puts a pause between servo1 sweeping and servo2 starting or a pause after servo2 finishes.

If you fix the include, replace line 26 with something like delay(5000); and sort the } braces out then at least you might get servo1 sweeping, 5 second delay then servo2 sweeping.

Steve


Go Up