Need some help for a NOOB

Including my code. and link to Tinkercad so you can physically see what’s going on. Never used it much before, but it’s helped me debug a few things. I’ve been working on this code now for a few days and can’t figure out why it keeps stopping and won’t keep looping. The Tinkercad helps with visually seeing what’s wrong.

Code has notes at beginning as to what’s going on. I’d appreciate if you leave detailed comments as to what you would change and why so I can better understand. Thank you in advance. I’ll check back in a few days (it should rain this week, which means no work)

//UFO Green
/*This is for a project of mine, and I will have to add to it
eventually. It's sort of a knight rider sketch.
I have a second sketch that controls other pins (LEDS) and they need
to blink independently of this sketch.  However, what I am encountering is
the LEDS will blink, in the array (using the on board LED to add some time 
between the 12 pin LED) correctly once.  And then it has a huge delay, and stops 
all together.  I need some help. Trying to avoid using delays.  See note for while command
line 49 */

byte ledPin[] = {9, 10, 11, 12, 13, 12, 11, 10, 9}; // Create array for LED pins
int ledDelay = 200; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
    for (int x = 0; x < 13; x++)
  {
    // set all pins to output
    pinMode(ledPin[x], OUTPUT);
  }
  changeTime = millis();
}
void loop() {
  if ((millis() - changeTime) > ledDelay)
  {
    // if it has been ledDelay ms since last change
    changeLED();
    changeTime = millis();
  }
}
void changeLED()
{
  for (int x = 0; x < 12; x++)
  {
    // turn off all LED's
    digitalWrite(ledPin[x], LOW);
  }
  digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
  currentLED += direction; // increment by the direction value
  // change direction if we reach the end
  if (currentLED > 13) {
    direction = -1;
  }
  for (int x = 0; x > 13; x--)
  {
  if (currentLED < 9) {
    direction = 1;
    while (15);  //I added this hoping that it would recycle my loop iteration
                 // but it doesn't seem to want to do it more than once
  }
  }

}

You declare an array with 9 elements.

Lucky_Break:

byte ledPin[] = {9, 10, 11, 12, 13, 12, 11, 10, 9}; // Create array for LED pins

Then you access elements 0-11 of the array:

Lucky_Break:
for (int x = 0; x < 12; x++)
{
// turn off all LED’s
digitalWrite(ledPin, LOW);
}

There’s a handy trick for programmatically determining the size of an array:

sizeof(ledPin) / sizeof(ledPin[0])

This is nice because it means your code will automatically adjust to you changing the size of the array. This calculation is done at compile time, so it doesn’t impact the program size or speed.

Another place where you access outside the bounds of the ledPin array:

Lucky_Break:

  digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED

currentLED += direction; // increment by the direction value
  // change direction if we reach the end
  if (currentLED > 13) {
    direction = -1;
  }

Lucky_Break:

  for (int x = 0; x > 13; x--)

Since 0 <=13, the for loop will never run.

Lucky_Break:

    while (15);  //I added this hoping that it would recycle my loop iteration

Any non-zero value is a Boolean true, so this would result in an infinite loop.

Hi,
Ops diagram;

Have you built your project?
What do you aim to power it with?
Can you post a circuit diagram showing how you are connecting the LEDs, your picture is very confusing as to how you have connected them.
This also means you current limit resistor may be or the wrong value and you may be overloading the UNO output pins.

Thanks.. Tom.. :slight_smile:

This will efeectly stop execution for good.

  {
    if (currentLED < 9) {
      direction = 1;
     [b] while (15);[/b]  //I added this hoping that it would recycle my loop iteration