I Need A Dealay At The End Of The Loop Before Starting Sequence Again

Hi Guys... my first question to the forum yet surely not the last question that's already been asked... er... yea

Anyway... Suppose I have a string of LEDs that operate in a similar fashion to the Knight Rider or Cylon sequence... left to right or back and forth, whatever... rather than the loop beginning again immediately after cycling through the program, I need to be able to insert a delay of 10000ms or whatever at the end of the loop...

so...

Blinkety-Blinkety-Blinkety-PAUSE------Blinkety-Blinkety-Blinkety-PAUSE------Blinkety-Blinkety-Blinkety-PAUSE------etc

Here's my code as it is...

// setup function
//=======================================================================
void setup()
{
//set LEDs pin as output
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}

// variables
//=======================================================================
int LED1, LED2, LED3, LED4, LED5, LED6;
int counter;

// loop function
//=======================================================================
void loop()
{
//step 1 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED1=counter; //increase current LED value
LED6=255-counter; //decrease previous LED value
analogWrite(3,LED1); //write LED values
analogWrite(11,LED6);
delay(1); //1ms delay
}
//step 2 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED2=counter; //increase current LED value
LED1=255-counter; //decrease previous LED value
analogWrite(5,LED2); //write LED values
analogWrite(3,LED1);
delay(1); //1ms delay
}
//step 3 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED3=counter; //increase current LED value
LED2=255-counter; //decrease previous LED value
analogWrite(6,LED3); //write LED values
analogWrite(5,LED2);
delay(1); //1ms delay
}
//step 4 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED4=counter; //increase current LED value
LED3=255-counter; //decrease previous LED value
analogWrite(9,LED4); //write LED values
analogWrite(6,LED3);
delay(1); //1ms delay
}
//step 5 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED5=counter; //increase current LED value
LED4=255-counter; //decrease previous LED value
analogWrite(10,LED5); //write LED values
analogWrite(9,LED4);
delay(1); //1ms delay
}
//step 6 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED6=counter; //increase current LED value
LED5=255-counter; //decrease previous LED value
analogWrite(11,LED6); //write LED values
analogWrite(10,LED5);
delay(1); //1ms delay
}

}

So... at the end of STEP 6 - or before Step 1 starts again - I want the loop to stop running for 1 or 2 seconds...

I'm pulling my hair out guys... your help is MUCH appreciated.

   delay(1);              //1ms delay
      } 

   delay(1000);
}

Thanks for the reply... It's helpful!

...with regard to exactly WHERE to put the Delay code - here's what I did with the snippet you provided... I'm just using the tail end of the sketch here - Step 6 - to keep it small... I simply added it to the end of the code... but now the last in the string of LEDs stays of for the allotted time of 2000ms...

//step 6 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED6=counter; //increase current LED value
LED5=255-counter; //decrease previous LED value
analogWrite(11,LED6); //write LED values
analogWrite(10,LED5);
delay(1); //1ms delay
}
delay(1); //1ms delay

delay(2000);
}

So... Where should I put the Delay snippet to achieve an OFF state for that 2000ms before it lights LED 1 again?

Thank A Bunch!

...so I'm placing the Delay snippet at the end of the sketch after Step 6...


//step 6 ===============================
counter=0; //counter counting 0 - 255
while(counter<255)
{
counter+=1;
LED6=counter; //increase current LED value
LED5=255-counter; //decrease previous LED value
analogWrite(11,LED6); //write LED values
analogWrite(10,LED5);
delay(1); //1ms delay
}
{
delay(1); //1ms delay

delay(2000);
}

}


...the result is that the program hangs with LED 6 in an ON state for the 2000ms...

How do i arrange the sketch so that ALL LEDs are OFF for 2000ms before the sequence begins again?

Thuper Thanks

How do i arrange the sketch so that ALL LEDs are OFF for 2000ms before the sequence begins again?

You need to explicitly turn them all off, before the delay(2000) call.

Thanks...

so I guess I need to know how ya go about doing that, exactly..?

I know... Sheesh! what a newb! lololo sorry, we all start somewhere, ay?

You've figured out how to switch them on, so...if 1 turns it on a little and 255 turns it on as far as it is going to go, how do you think you might turn it off?

...thanks for not handing me the easy way out... :wink:

I'm experimenting with it now...

Mike

It would be easier to turn off all your leds if you had a ledPin array. Then you could turn them all off or on with a for loop.

ledPin[] = {3, 5, 6, 9, 10, 11};

for (int index = 0; index < 6; index++) analogWrite(ledPin[index], 0); // turns off all the leds

A similar loop could also be used to initialize all the pins.

 for (int index = 0; index < 6; index++) pinMode(ledPin[index],OUTPUT); // initialize all the leds

Then you would want to change your references to pin numbers with the appropriate variable.

Instead of:

analogWrite(6, LED3);

You'd use:

analogWrite(ledPin[2], LED3) // the first element in an array is 0

This will also help as now you can reference your leds with sequential numbers instead of having to remember which pins they are on. Arrays and loops are a powerful team.

You ROCK! Jimmy

I'll be experimenting with these all afternoon...

Thanks MUCH

Mike

for (int index = 0; index < 6; index++) analogWrite(ledPin[index], 0); // turns off all the leds

You could save space and use byte for the index type. 6 will fit in a byte...

Jimmy60:
It would be easier to turn off all your leds if you had a ledPin array. Then you could turn them all off or on with a for loop.
....

This will also help as now you can reference your leds with sequential numbers instead of having to remember which pins they are on. Arrays and loops are a powerful team.

Jimmy can you direct me to more info about Arrays and Loops? I'm developing a custom sequential turn signal for a vehicle and have been trying to find ways to arrange circular LED arrays and such with EagleCAD... so your comment about Arrays and Loops is one that got my attention relating to sequencing my ons and offs...

Thanks Again

Sure, the arduino reference page.

You'll find loops under control structures and arrays under variables.

Also a great resource with very good tutorials:
www.cplusplus.com

... and some bytes could be saved by doing what PaulS suggested. :smiley:

I'm developing a custom sequential turn signal for a vehicle

Hacking up your mustang?

I cant give any details, but I work on the team that designed those on the 2013's, I made a (very early) test version of the korean version whipped up on radio shack perf board around here somewhere :grin:

I'd like to see your resume' sometime...

These lights are being fabbed for a low flying light sport aircraft design that popped into my head at 3am one sleepy winter night in 2011... one of THOSE things... it's image inspires me to develop the technology to make all of its systems work together... funstuff,ay