Pages: [1]   Go Down
Author Topic: LED Loop Issue  (Read 334 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It might make more sense to initialise the pin modes before you start writing to them.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18730
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read up on "for" loops? Change:

Code:
 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:
 for (int i = 0; i < 8; i++)
    pinMode (ledPins[i], OUTPUT);  
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 51
Posts: 2276
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now look at blink without delay.

Mark
Logged

Pages: [1]   Go Up
Jump to: