# Butterfly Effect

This post has nothing to do with the chaos theory of the "butterfly effect" or the movie but a simple butterfly made with servos!

I have the code below that allows for two servos, side by side, to "flap" like wings so to speak between 0 and 40 degress (the servo horn/arm has been connected at a certain angle so each arm mirrors one another)

``````#include <Servo.h>

Servo servo1;
Servo servo2;

int pos1;
int pos2;

void setup()
{
servo1.attach(10);
servo2.attach(8);
}

void loop()
{
delay(1000);
// Scene 1:
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
delay (1000);
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(15);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(15);
}

}
``````

This is for one butterfly, but I would like 3 going at once (so 6 servos) but don't want them all doing the same thing at once (perhaps the second butterfly does the same thing but starts 1 second after the first butterfly etc). I've had a look at using millis but I'm pretty stumped at how to go about it.

Any help would be much appreciated!

zakgatara:
This post has nothing to do with the chaos theory of the "butterfly effect" or the movie but a simple butterfly made with servos!

I have the code below that allows for two servos, side by side, to "flap" like wings so to speak between 0 and 40 degress (the servo horn/arm has been connected at a certain angle so each arm mirrors one another)

``````#include <Servo.h>
``````

Servo servo1;
Servo servo2;

int pos1;
int pos2;

void setup()
{
servo1.attach(10);
servo2.attach(8);
}

void loop()
{
delay(1000);
// Scene 1:
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(5);
}
delay (1000);
for (int i=0; i<40; i += 1) {
pos1 = i;
pos2 = 40 - i;

servo1.write(pos1);
servo2.write(pos2);
delay(15);
}
for (int i=40; i>=0; i -= 1) {
pos1 = i;
pos2 = 40 - i;
servo1.write(pos1);
servo2.write(pos2);
delay(15);
}

}

``````

This is for one butterfly, but I would like 3 going at once (so 6 servos) but don't want them all doing the same thing at once (perhaps the second butterfly does the same thing but starts 1 second after the first butterfly etc). I've had a look at using millis but I'm pretty stumped at how to go about it.

Any help would be much appreciated!
``````

What if you create functions like

``````void butterfly1Flap(){
//all servo stuff
}

void butterfly2Flap(){
//all servo stuff
}

void butterfly3Flap(){
//all servo stuff
}
``````

and call these inside the loop like

``````void loop(){
butterfly1Flap()
delay(xx);
butterfly2Flap()
delay(xx);
butterfly3Flap()
delay(xx);
}
``````

Won't it run through each function entirely before starting the next one? I will try now, hopefully it works!

First, I suggest you simplify things by using a single servo to flap both wings - butterly wings move in sync with each other.

Second, for the sort of thing you want there should not be a delay() function anywhere in your program. Have a look at how millis() is used to manage timing in Several Things at a Time

That demo also includes a servo example.

If you want to move several servos at the same time you need to interleave their movements. For example

``````if (currentMillis - previousServoMillis >= servoInterval) {
// its time for another move
previousServoMillis += servoInterval;

servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative
servo2Position = servo2Position + servo2Degrees; // servo2Degrees might be negative
// etc
``````

...R