two for loop

please anybody tell e logic to apply to run two and more for loop to be run simultaneously at one time i mean to run all for loop at a same time...

here i was using some logic to run my small testing project

servo_motor_code__led_working_simultaneously.ino (616 Bytes)

tenzin:
please anybody tell e logic to apply to run two and more for loop to be run simultaneously at one time i mean to run all for loop at a same time...

What?

i mean to execute any loop to run at same time

#include<Servo.h>
Servo servo;
int angle =0;
void setup()
{
pinMode(8,OUTPUT);
servo.attach(9);

}

void loop()
{
servo.write(0);
int d=0,a=0;
for( angle = 0;angle<180;angle++){

servo.write(angle);
delay(10);
d+=10;
if(d%500==0){
if(a==0){
digitalWrite(8,0);
a=1;
}
else if(a==1){
digitalWrite(8,1);
a=0;
}

}
}
for( angle = 180;angle>0;angle--)
{
servo.write(angle);
delay(10);

if(d%500==0){
if(a==0){
digitalWrite(8,0);
a=1;
}
else if(a==1){
digitalWrite(8,1);
a=0;
}

}d+=10;
}
}

here what i apply some logic to do so

so can u tell me exactly to do so..?

Have a look at the demo Several Things at a Time.

The trick is only to do one step of each action at any time. Don't use FOR. Use a counter variable to keep track of where you are and allow loop() to do the iteration.

...R

thank you

You're working with the same variable (angle) in both for loops ... the angle increments from 0 to 180, then in the second for loop it decrements from 180 to 0.

You say you want to run them simultaneously. Well, if you increment, then decrement it won't work. If you manage to accomplish the "angle" sequence above, then you'll end up with the same thing in terms of response , but your code might be simpler.

If the problem is really something wrong with how responsive your code is, then the culprit is the 360 delay(10) statements that run for each loop iteration. Your loop speed is like a turtle (3.6 seconds+)

Consider using the millis() timer to replace the delays. Check out the blink without delay example and others on how to do this.