Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ARami7
New Contributor I
1,344 Views

How do i run two sketches at the same time?

Jump to solution

so here's my first sketch:

# include

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

void setup() {

myservo.attach(11);// put your setup code here, to run once:

myservo.write(150); // tell servo to go to position in variable 'pos'

delay(2500); // waits 15ms for the servo to reach the position

myservo.write(120); // tell servo to go to position in variable 'pos'

}

void loop() {

// put your main code here, to run repeatedly:

}

notice the 2.5 second delay? i want to run THIS while 2.5 seconds are elapsing, and not AFTER 2.5 seconds:

# include

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

void setup() {

myservo.attach(10);// put your setup code here, to run once:

myservo.write(165 ); // tell servo to go to position in variable 'pos'

delay(200); // waits 15ms for the servo to reach the position

myservo.write(120); // tell servo to go to position in variable 'pos'

}

void loop() {

// put your main code here, to run repeatedly:

}

maybe the solution is not running two sketches at the same time, but what code can launch even though there is a delay

** through SSH i run the sketch files individually with this command:

/media/servo1/sketch.elf ./sketch.elf /dev/ttyGS0

when i use this code through putty in ssh, my sketch file runs, so i wanted to launch two at the same time or re-work my code to run simultaneously even though there is a 2.5 second delay

1 Solution
FTinetti
Honored Contributor I
73 Views

Maybe I'm losing something, but if you have 2 servos and you have to move each one 2 times with specific delays (I copy your code here and delete comments because I find them confusing):

myservo.attach(11);

myservo.write(150);

delay(2500);

myservo.write(120);

and

myservo.attach(10);

myservo.write(165);

delay(200);

myservo.write(120);

then

myservo1.attach(11);

myservo2.attach(10);

myservo1.write(150);

myservo2.write(165);

delay(200);

myservo2.write(120);

delay(2300);

myservo1.write(120);

HTH,

Fernando.

View solution in original post

2 Replies
FTinetti
Honored Contributor I
74 Views

Maybe I'm losing something, but if you have 2 servos and you have to move each one 2 times with specific delays (I copy your code here and delete comments because I find them confusing):

myservo.attach(11);

myservo.write(150);

delay(2500);

myservo.write(120);

and

myservo.attach(10);

myservo.write(165);

delay(200);

myservo.write(120);

then

myservo1.attach(11);

myservo2.attach(10);

myservo1.write(150);

myservo2.write(165);

delay(200);

myservo2.write(120);

delay(2300);

myservo1.write(120);

HTH,

Fernando.

View solution in original post

Pedro_M_Intel
Employee
73 Views

Hello AdanChristo,

Running more than one sketch at the time is not advisable since, let's say you are running two sketches and they both are running at the same time, in that case, they could be competing for the same hardware resources.

So, I'd suggest you to try something similar to what Fernando suggested. Or something similar to:

# include

Servo myservo;

void setup() {

myservo.attach(11);

myservo.write(150);

myservo.attach(10); //Run this and time it

myservo.write(165 ); //then change the delay

delay(200); //bellow to 2.5s minus

myservo.write(120); //the time this takes

delay(X);

myservo.write(120);

}

void loop() {}

Peter.

Reply