Go Down

Topic: LED Loop Issue (Read 375 times) previous topic - next topic

strangeitalianbarber

I am running 6 LEDs all working correctly I just can't get the sequence I want down. I'm trying to make the initial start up sequence LEDs 2+3, 1+4, then 0+5 so if they are on line  they would start from the inside out. I only want the start up sequence in the when it starts up then to pause for 250 milliseconds then all 6 LEDs turn on and stay on at once. The issue I'm having is after it "starts up", it waits the 250 milliseconds then all 6 turn on then they start going into a loop where they will all turn on and off. I just want them to stay lit. What is wrong with the code? Thanks.
Code: [Select]
int ledPins[] = {2,3,4,5,6,7};


void setup()
{
 
  for(int i = 0; i < 6; i++){
      pinMode(ledPins[i],OUTPUT);
  }

}


void loop()
{
  STARTUP();
  RUNNING();
}



void STARTUP(){
 
  int delayTime = 250;
 
 
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
 
  delay(delayTime);
 
  digitalWrite(ledPins[2], LOW);
  digitalWrite(ledPins[3], LOW);
  digitalWrite(ledPins[1], LOW);
  digitalWrite(ledPins[4], LOW);
  digitalWrite(ledPins[0], LOW);
  digitalWrite(ledPins[5], LOW);
 
  delay(delayTime);
 
 
}

void RUNNING(){
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
}

PaulS

STARTUP is called, and each pair of LEDs is turned on, with a pause in between. Then, all are turned off after a pause. They are immediately turned back on because STARTUP ends and RUNNING is called.

RUNNING ends, with the LEDS all on, and you turn pairs (that are on) on again. Then, turn all LEDs off, and start the process again.

Quote
I just want them to stay lit. What is wrong with the code?

The fact that you call STARTUP again, in loop().

Move STARTUP() to setup().

Though why you want to turn the pins on over an over again is mystery.

There is a convention that all capital letter names are reserved for #define'd or const variables. Can you think of a single Arduino or C function that is all upper case?

strangeitalianbarber

I've migrated the "Startup" to "Setup". They all stay lit, although there is no "Startup" sequence on the LEDs. It waits a few seconds, then all 6 LEDs come on at once. Thanks for the capitalization tip.

The following is what it reads now:

Code: [Select]
int ledPins[] = {2,3,4,5,6,7};


void setup()
{
  Startup();
  for(int i = 0; i < 6; i++){
      pinMode(ledPins[i],OUTPUT);
  }

}


void loop()
{
  Running();
}



void Startup(){
 
  int delayTime = 250;
 
 
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
 
  delay(delayTime);
 
  digitalWrite(ledPins[2], LOW);
  digitalWrite(ledPins[3], LOW);
  digitalWrite(ledPins[1], LOW);
  digitalWrite(ledPins[4], LOW);
  digitalWrite(ledPins[0], LOW);
  digitalWrite(ledPins[5], LOW);
 
  delay(delayTime);
 
 
}

void Running(){
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
}

PeterH

It might make more sense to initialise the pin modes before you start writing to them.
I only provide help via the forum - please do not contact me for private consultancy.

strangeitalianbarber

I guess I could move the whole thing. Anyways thanks for the help. It works the way I want now. What could I do to this to clean it up if any?

Code: [Select]
int ledPins[] = {2,3,4,5,6,7};


void setup()
{
  pinMode(ledPins[0],OUTPUT);
  pinMode(ledPins[1],OUTPUT);
  pinMode(ledPins[2],OUTPUT);
  pinMode(ledPins[3],OUTPUT);
  pinMode(ledPins[4],OUTPUT);
  pinMode(ledPins[5],OUTPUT);
  pinMode(ledPins[6],OUTPUT);
  pinMode(ledPins[7],OUTPUT);   
        int delayTime = 250;
 
 
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  delay(delayTime);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
 
  delay(delayTime);
 
  digitalWrite(ledPins[2], LOW);
  digitalWrite(ledPins[3], LOW);
  digitalWrite(ledPins[1], LOW);
  digitalWrite(ledPins[4], LOW);
  digitalWrite(ledPins[0], LOW);
  digitalWrite(ledPins[5], LOW);
 
  delay(delayTime);
  }




void loop()
{
  Running();
}


void Running(){
  digitalWrite(ledPins[2], HIGH);
  digitalWrite(ledPins[3], HIGH);
  digitalWrite(ledPins[1], HIGH);
  digitalWrite(ledPins[4], HIGH);
  digitalWrite(ledPins[0], HIGH);
  digitalWrite(ledPins[5], HIGH);
}

Nick Gammon

Read up on "for" loops? Change:

Code: [Select]

 pinMode(ledPins[0],OUTPUT);
 pinMode(ledPins[1],OUTPUT);
 pinMode(ledPins[2],OUTPUT);
 pinMode(ledPins[3],OUTPUT);
 pinMode(ledPins[4],OUTPUT);
 pinMode(ledPins[5],OUTPUT);
 pinMode(ledPins[6],OUTPUT);
 pinMode(ledPins[7],OUTPUT);  


To:

Code: [Select]

 for (int i = 0; i < 8; i++)
   pinMode (ledPins[i], OUTPUT);  
http://www.gammon.com.au/electronics

PaulS

OP had a for loop, but was setting the pins to OUTPUT in that loop, AFTER call the Startup() function. The call to Startup() needed to be moved after the for loop to define the mode of the pins.

holmes4

Now look at blink without delay.

Mark

Go Up