I am using this code to loop through 5 LEDs. I would like to pause for 1 sec. when I reach the highest LED before cycling down back to the lowest. Is this possible?
int timer = 80; // The higher the number, the slower the timing.
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = 6; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = 6; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
if (thisPin == 6)
{
delay(1000);
}
}
}
patduino:
Unless I misunderstand the question, it should be as simple as adding a single delay statement between the for loops:
delay(1000);
That's what I was going to suggest.... seems too easy though 8) Have we missed something?
EDIT: Ah, wait, if he means pause with the last LED still on, the the other suggestions will do the trick I guess- putting the test inside the first loop and changing the delay time for the last LED
arduinokov:
It's working as a delay, but the pause it with the LED's off, I would like it to pause with them all on.
With them all on?- now you're changing the rules
You could do that with 2 more for loops, in between the 2 you have.
Do your first loop
Then loop to turn them all on again but not switch them off one by one; then delay, and loop again to switch them all off
Then carry on as before
If I use the following code, the LED's light up sequentially and stay on. May I ask how I can get them to pause when they are all on and then sequentially go off again?
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
a) put a delay after the part you have just posted, to pause with them all on, then
b) repeat the part you have, but counting down instead of up, and going low not high
Does your latest sketch look a little like this one?
int timer = 250;
int pausetime = 1000;
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
}
delay(pausetime); // pause between for loops
// loop from the highest pin to the lowest:
for (int thisPin = 7; thisPin > 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, LOW);
delay(timer);
}
// return to the top of the void loop
}
Actually, I switched to this code (I just need a longer pause after they are all on):
//LED Pin Variables
int ledPins[] = {2,3,4,5,6}; //An array to hold the pin each LED is connected to
void setup()
{
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 7; i++){ //this is a loop and will repeat eight times
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
} //the code this replaces is below
}
void loop() // run over and over again
{
oneAfterAnotherNoLoop(); //this will turn on each LED one by one then turn each off
}
void oneAfterAnotherNoLoop(){
int delayTime = 50; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
digitalWrite(ledPins[0], HIGH); //Turns on LED #0 (connected to pin 2 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[1], HIGH); //Turns on LED #1 (connected to pin 3 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[2], HIGH); //Turns on LED #2 (connected to pin 4 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[3], HIGH); //Turns on LED #3 (connected to pin 5 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[4], HIGH); //Turns on LED #4 (connected to pin 6 )
delay(delayTime); //waits delayTime milliseconds
//Turns Each LED Off
digitalWrite(ledPins[4], LOW); //Turns on LED #0 (connected to pin 6 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[3], LOW); //Turns on LED #1 (connected to pin 5 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[2], LOW); //Turns on LED #2 (connected to pin 4 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[1], LOW); //Turns on LED #3 (connected to pin 3 )
delay(delayTime); //waits delayTime milliseconds
digitalWrite(ledPins[0], LOW); //Turns on LED #4 (connected to pin 2 )
delay(delayTime); //waits delayTime milliseconds
}
Why did you change from the loop ? What you have now is perhaps more easily understood but it is clumsy.
To add more delay with them all on just add a longer delay before you turn the LEDs off
digitalWrite(ledPins[4], HIGH); //Turns on LED #4 (connected to pin 6 )
delay(delayTime); //waits delayTime milliseconds
delay(1000); //wait one second with the LEDs on
//Turns Each LED Off
digitalWrite(ledPins[4], LOW); //Turns on LED #0 (connected to pin 6 )
delay(delayTime); //waits delayTime milliseconds
Wrong comments are worse than no comments at all as they lead you astray and waste time.
Use of some kind of symbol naming standard will help reduce the need for comments.
Reducing the need for comments reduces the amount of work needed to modify code making it quicker and easier to have the comments reflect the actual current code and get on with development.
Code formatting, including white space, will also help to reduce coding errors making code easier and faster to to write and debug.
What those standards are can be a source of disagreement but something is almost always better than nothing ...
#define ARRAY_COUNT(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
const uint8_t pinLED_0 = 2;
const uint8_t pinLED_1 = 3;
const uint8_t pinLED_2 = 4;
const uint8_t pinLED_3 = 5;
const uint8_t pinLED_4 = 6;
// set for common cathode, reverse for common anode
const uint8_t LED_OFF = LOW;
const uint8_t LED_ON = HIGH;
int pinsLED[] = { pinLED_0, pinLED_1, pinLED_2, pinLED_3, pinLED_4 };
void setup()
{
// this is a loop and will repeat for each entry in the array 'pinsLED'
for ( int i = 0; i < ARRAY_COUNT(pinsLED); i++ )
{
// use this to set each pin in the pinsLED array to OUTPUT
pinMode(pinsLED[i], OUTPUT);
}
}
// run over and over again
void loop()
{
// this will turn on each LED one by one then turn each off
oneAfterAnotherNoLoop();
}
void oneAfterAnotherNoLoop()
{
// time (in milliseconds) to pause between LED state changes
// smaller numbers lead to shorter time delays
const unsigned long delayTime = 50UL;
// light's on
digitalWrite(pinLED_0, LED_ON); delay(delayTime);
digitalWrite(pinLED_1, LED_ON); delay(delayTime);
digitalWrite(pinLED_2, LED_ON); delay(delayTime);
digitalWrite(pinLED_3, LED_ON); delay(delayTime);
digitalWrite(pinLED_4, LED_ON); delay(delayTime);
// light's off
digitalWrite(pinLED_4, LED_OFF); delay(delayTime);
digitalWrite(pinLED_3, LED_OFF); delay(delayTime);
digitalWrite(pinLED_2, LED_OFF); delay(delayTime);
digitalWrite(pinLED_1, LED_OFF); delay(delayTime);
digitalWrite(pinLED_0, LED_OFF); delay(delayTime);
}