Help with loop code to go for 10 seconds

Hy guys i have a problem…
I wrote a code but i need to make it go for 10 seconds…I managed to put code for repeating code for 10 times but i need seconds.Code is with repeat code 10 times…
Can anyone help me with code for that?

// constants won’t change. They’re used here to
// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
// the setup function runs once when you press reset or power the board
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize digital pin 13 as an output.
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}

// the loop function runs over and over again forever
void loop()
{
int p = digitalRead(2);
if (p == HIGH)
for (int x = 0; x < 10; x++)
{
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
digitalWrite(9, LOW); // turn the LED on (HIGH is the voltage level)
delay(100);
digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(6, LOW); // turn the LED off by making the voltage LOW
digitalWrite(10, LOW); // turn the LED on (HIGH is the voltage level)
delay(100);
}
}

You definitely need to embrace the BlinkWithoutDelay approach.

I found this video very good.

I will upload video now to yt so u can see what I mean...

Youtube video of sequence

On the left are pins 9 and 10...On the right are 3,5 and 6... Now as u can see code is running but not as i liked..pins on the left should blink their code and not follow leds on the right...And i dont know how to do that...

delay(100);              // wait for a second

You will be getting rid of the delay()s soon I hope, but does the comment match the code ? I am sure that you think that it does not matter but if you are going to add comments please make them sensible, unless, of course, you meant the delay() to be for 1 second.

delay(500); im sorry i forgot to change comment....How do you mean get rid of delay? 3,5 and 6 are chasing light 500ms and 9 and 10 leds should blink code regardless of 3,5 and 6...I u know what I mean.....

ShogunCRO: How do you mean get rid of delay?

Read reply #1 and implement BlinkWithoutDelay

delay(500); im sorry i forgot to change comment...

Still wrong

How do you mean get rid of delay?

See reply #1

delay(x) means "stop doing anything else for x milliseconds" which prevents you timing an action at the same time, which is what you want to do. You need to implement timing for the flashing LEDs that does not prevent the processor from doing something else.

Save the millis() value when you start timing for 10 seconds. Save the millis() value at the time that the start action happens, such as turning on an LED. Then, each time through loop(), check whether the required LED on period has elapsed by subtracting the LED on start time from the millis() value now. If the period has elapsed then act accordingly, maybe by changing the state of 1 or more LEDs and save the start time for the current LED activity. If not, then go round loop() again, perhaps taking other actions such as seeing whether 10 seconds has elapsed using the same millis() timing technique.

Iw tried this code from here http://www.instructables.com/id/Beginning-Arduino-delay-without-delay/step2/The-Code/ trying to change it but iw just managed to light up 1 led on pin 6..... This is harder than i think :/

It's not hard but its sometimes seems like a hard way to do something simple. Try this

unsigned long overallStartTime;
unsigned long blinkStartTime;
unsigned long overallPeriod = 10000;
unsigned long blinkPeriod = 500;
unsigned long currentTime;
const byte ledPin = 13;
boolean blinking = true;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  overallStartTime = millis();
  blinkStartTime = overallStartTime;
  digitalWrite(ledPin, HIGH);
}

void loop()
{
  currentTime = millis();
  if (currentTime - blinkStartTime >= blinkPeriod && blinking)  //LED blink time check
  {
    Serial.println("blink");
    digitalWrite(ledPin, !digitalRead(ledPin));  //change state of LED
    blinkStartTime = blinkStartTime + blinkPeriod;  //save the new blink start time
  }

  if (currentTime - overallStartTime >= overallPeriod && blinking)  //overall time check
  {
    blinking = false;
    Serial.println("Time's up.  Stopped blinking");
  }
}

It should show you how to blink one LED for a period of time then you can think about adding more. Get ready to learn about state machines...

I was learning a bout state machines but not that far…with that code i managed to blink led on pin 3 for 10sec 500ms…And then led keeps on the whole time…
But i came across of this code testing on 3 led-s…but again sequence out of order in some time 2 led blink at same time…am lost like pizza in space

// Which pins are connected to which LED
const byte LED1 = 3;
const byte LED2 = 5;
const byte LED3 = 6;

// Assigning delays.
const unsigned long LED1_interval = 400;
const unsigned long LED2_interval = 500;
const unsigned long LED3_interval = 600;

// Declaring the variables holding the timer values for each LED.
unsigned long LED1_timer;
unsigned long LED2_timer;
unsigned long LED3_timer;

// Setting 3 digital pins as output pins and resetting timer
void setup ()
{
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
LED1_timer = millis ();
LED2_timer = millis ();
LED3_timer = millis ();
} // end of setup

//LED1 loop that turns it ON if it is OFF and vice versa
void toggle_LED1 ()
{
if (digitalRead (LED1) == LOW)
digitalWrite (LED1, HIGH);
else
digitalWrite (LED1, LOW);

// remember when we toggled it
LED1_timer = millis ();
} // end of toggleLED_1

//LED2 loop
void toggle_LED2 ()
{
if (digitalRead (LED2) == LOW)
digitalWrite (LED2, HIGH);
else
digitalWrite (LED2, LOW);

// remember when we toggled it
LED2_timer = millis ();
} // end of toggle_LED2

//LED 3 loop
void toggle_LED3 ()
{
if (digitalRead (LED3) == LOW)
digitalWrite (LED3, HIGH);
else
digitalWrite (LED3, LOW);

// remember when we toggled it
LED3_timer = millis ();
} // end of toggle_LED3

void loop ()
{

// Handling the blink of LED1.
if ( (millis () - LED1_timer) >= LED1_interval)
toggle_LED1 ();

// Handling the blink of LED2.
if ( (millis () - LED2_timer) >= LED2_interval)
toggle_LED2 ();

// Handling the blink of LED3.
if ( (millis () - LED3_timer) >= LED3_interval)
toggle_LED3 ();

/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */

} // end of loop

Still no progress here....Leds come out of sinc...I need to get chasing light with two separate leds on 9 and 10 pin wich blinks their own pattern...

// Which pins are connected to which LED const byte LED1 = 3; const byte LED2 = 5; const byte LED3 = 6;

// Assigning delays. const unsigned long LED1_interval = 400; const unsigned long LED2_interval = 500; const unsigned long LED3_interval = 600;

// Declaring the variables holding the timer values for each LED. unsigned long LED1_timer; unsigned long LED2_timer; unsigned long LED3_timer;

// Setting 3 digital pins as output pins and resetting timer void setup () { pinMode (LED1, OUTPUT); pinMode (LED2, OUTPUT); pinMode (LED3, OUTPUT); LED1_timer = millis (); LED2_timer = millis (); LED3_timer = millis (); } // end of setup

//LED1 loop that turns it ON if it is OFF and vice versa void toggle_LED1 () { if (digitalRead (LED1) == LOW) digitalWrite (LED1, HIGH); else digitalWrite (LED1, LOW);

// remember when we toggled it LED1_timer = millis (); } // end of toggleLED_1

//LED2 loop void toggle_LED2 () { if (digitalRead (LED2) == LOW) digitalWrite (LED2, HIGH); else digitalWrite (LED2, LOW);

// remember when we toggled it LED2_timer = millis (); } // end of toggle_LED2

//LED 3 loop void toggle_LED3 () { if (digitalRead (LED3) == LOW) digitalWrite (LED3, HIGH); else digitalWrite (LED3, LOW);

// remember when we toggled it LED3_timer = millis (); } // end of toggle_LED3

void loop () {

// Handling the blink of LED1. if ( (millis () - LED1_timer) >= LED1_interval) toggle_LED1 ();

// Handling the blink of LED2. if ( (millis () - LED2_timer) >= LED2_interval) toggle_LED2 ();

// Handling the blink of LED3. if ( (millis () - LED3_timer) >= LED3_interval) toggle_LED3 ();

/* Other code that needs to execute goes here. It will be called many thousand times per second because the above code does not wait for the LED blink interval to finish. */

} // end of loop